1
|
|
|
import sys |
2
|
|
|
from os import getcwd |
3
|
|
|
from os.path import join |
4
|
|
|
from pathlib import Path |
5
|
|
|
import requests |
6
|
|
|
|
7
|
|
|
import click |
8
|
|
|
|
9
|
|
|
from ocrd_utils import ( |
10
|
|
|
initLogging, |
11
|
|
|
getLogger, |
12
|
|
|
RESOURCE_LOCATIONS |
13
|
|
|
) |
14
|
|
|
from ocrd_validators import OcrdZipValidator |
15
|
|
|
|
16
|
|
|
from ..resource_manager import OcrdResourceManager |
17
|
|
|
|
18
|
|
|
def print_resources(executable, reslist, resmgr): |
19
|
|
|
print('%s' % executable) |
20
|
|
|
for resdict in reslist: |
21
|
|
|
print('- %s %s (%s)\n %s' % ( |
22
|
|
|
resdict['name'], |
23
|
|
|
'@ %s' % resmgr.resource_dir_to_location(resdict['path']) if 'path' in resdict else '', |
24
|
|
|
resdict['url'], |
25
|
|
|
resdict['description'] |
26
|
|
|
)) |
27
|
|
|
print() |
28
|
|
|
|
29
|
|
|
@click.group("resmgr") |
30
|
|
|
def resmgr_cli(): |
31
|
|
|
""" |
32
|
|
|
Managing processor resources |
33
|
|
|
""" |
34
|
|
|
initLogging() |
35
|
|
|
|
36
|
|
|
@resmgr_cli.command('list-available') |
37
|
|
|
@click.option('-e', '--executable', help='Show only resources for executable EXEC', metavar='EXEC') |
38
|
|
|
def list_available(executable=None): |
39
|
|
|
""" |
40
|
|
|
List available resources |
41
|
|
|
""" |
42
|
|
|
resmgr = OcrdResourceManager() |
43
|
|
|
for executable, reslist in resmgr.list_available(executable): |
44
|
|
|
print_resources(executable, reslist, resmgr) |
45
|
|
|
|
46
|
|
|
@resmgr_cli.command('list-installed') |
47
|
|
|
@click.option('-e', '--executable', help='Show only resources for executable EXEC', metavar='EXEC') |
48
|
|
|
def list_installed(executable=None): |
49
|
|
|
""" |
50
|
|
|
List installed resources |
51
|
|
|
""" |
52
|
|
|
resmgr = OcrdResourceManager() |
53
|
|
|
ret = [] |
54
|
|
|
for executable, reslist in resmgr.list_installed(executable): |
55
|
|
|
print_resources(executable, reslist, resmgr) |
56
|
|
|
|
57
|
|
|
@resmgr_cli.command('download') |
58
|
|
|
@click.option('-n', '--any-url', help='Allow downloading/copying unregistered resources', is_flag=True) |
59
|
|
|
@click.option('-o', '--overwrite', help='Overwrite existing resources', is_flag=True) |
60
|
|
|
@click.option('-l', '--location', help='Where to store resources', type=click.Choice(RESOURCE_LOCATIONS), default='data', show_default=True) |
61
|
|
|
@click.argument('executable', required=True) |
62
|
|
|
@click.argument('url_or_name', required=True) |
63
|
|
|
def download(any_url, overwrite, location, executable, url_or_name): |
64
|
|
|
""" |
65
|
|
|
Download resource URL_OR_NAME for processor EXECUTABLE. |
66
|
|
|
|
67
|
|
|
URL_OR_NAME can either be the ``name`` or ``url`` of a registered resource. |
68
|
|
|
|
69
|
|
|
If URL_OR_NAME is '*' (asterisk), download all known resources for this processor |
70
|
|
|
|
71
|
|
|
If ``--any-url`` is given, also accepts URL or filenames of non-registered resources for ``URL_OR_NAME``. |
72
|
|
|
""" |
73
|
|
|
log = getLogger('ocrd.cli.resmgr') |
74
|
|
|
resmgr = OcrdResourceManager() |
75
|
|
|
basedir = resmgr.location_to_resource_dir(location) |
76
|
|
|
is_url = url_or_name.startswith('https://') or url_or_name.startswith('http://') |
77
|
|
|
is_filename = Path(url_or_name).exists() |
78
|
|
|
find_kwargs = {'executable': executable} |
79
|
|
|
if url_or_name != '*': |
80
|
|
|
find_kwargs['url' if is_url else 'name'] = url_or_name |
81
|
|
|
reslist = resmgr.find_resources(**find_kwargs) |
82
|
|
|
if not reslist: |
83
|
|
|
log.info("No resources found in registry") |
84
|
|
|
if any_url and (is_url or is_filename): |
85
|
|
|
log.info("%s unregistered resource %s" % ("Downloading" if is_url else "Copying", url_or_name)) |
86
|
|
|
if is_url: |
87
|
|
|
with requests.get(url_or_name, stream=True) as r: |
88
|
|
|
content_length = int(r.headers.get('content-length')) |
89
|
|
|
else: |
90
|
|
|
url_or_name = str(Path(url_or_name).resolve()) |
91
|
|
|
content_length = Path(url_or_name).stat().st_size |
92
|
|
|
with click.progressbar(length=content_length, label="Downloading" if is_url else "Copying") as bar: |
93
|
|
|
fpath = resmgr.download( |
94
|
|
|
executable, |
95
|
|
|
url_or_name, |
96
|
|
|
overwrite=overwrite, |
97
|
|
|
basedir=basedir, |
98
|
|
|
progress_cb=lambda delta: bar.update(delta)) |
99
|
|
|
log.info("%s resource '%s' (%s) not a known resource, creating stub in %s'" % (executable, fpath.name, url_or_name, resmgr.user_list)) |
100
|
|
|
resmgr.add_to_user_database(executable, fpath, url_or_name) |
101
|
|
|
log.info("%s %s to %s" % ("Downloaded" if is_url else "Copied", url_or_name, fpath)) |
102
|
|
|
log.info("Use in parameters as '%s'" % fpath.name) |
103
|
|
|
else: |
104
|
|
|
sys.exit(1) |
105
|
|
|
else: |
106
|
|
|
for _, resdict in reslist: |
107
|
|
|
if resdict['url'] == '???': |
108
|
|
|
log.info("Cannot download user resource %s" % (resdict['name'])), |
109
|
|
|
continue |
110
|
|
|
log.info("Downloading resource %s" % resdict) |
111
|
|
|
with click.progressbar(length=resdict['size']) as bar: |
112
|
|
|
fpath = resmgr.download( |
113
|
|
|
executable, |
114
|
|
|
resdict['url'], |
115
|
|
|
name=resdict['name'], |
116
|
|
|
resource_type=resdict['type'], |
117
|
|
|
path_in_archive=resdict.get('path_in_archive', '.'), |
118
|
|
|
overwrite=overwrite, |
119
|
|
|
basedir=basedir, |
120
|
|
|
progress_cb=lambda delta: bar.update(delta) |
121
|
|
|
) |
122
|
|
|
log.info("Downloaded %s to %s" % (resdict['url'], fpath)) |
123
|
|
|
log.info("Use in parameters as '%s'" % resmgr.parameter_usage(resdict['name'], usage=resdict['parameter_usage'])) |
124
|
|
|
|
125
|
|
|
|