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