1
|
|
|
import os |
2
|
|
|
from os import getcwd |
3
|
|
|
from os.path import relpath, exists, join, isabs, dirname, basename, abspath |
4
|
|
|
from pathlib import Path |
5
|
|
|
from json import loads |
6
|
|
|
import sys |
7
|
|
|
from glob import glob # XXX pathlib.Path.glob does not support absolute globs |
8
|
|
|
import re |
9
|
|
|
|
10
|
|
|
import click |
11
|
|
|
|
12
|
|
|
from ocrd import Resolver, Workspace, WorkspaceValidator, WorkspaceBackupManager |
13
|
|
|
from ocrd_utils import getLogger, initLogging, pushd_popd, EXT_TO_MIME |
14
|
|
|
from . import command_with_replaced_help |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class WorkspaceCtx(): |
18
|
|
|
|
19
|
|
|
def __init__(self, directory, mets_url, mets_basename, automatic_backup): |
20
|
|
|
self.log = getLogger('ocrd.cli.workspace') |
21
|
|
|
if mets_basename and mets_url: |
22
|
|
|
raise ValueError("Use either --mets or --mets-basename, not both") |
23
|
|
|
if mets_basename and not mets_url: |
24
|
|
|
self.log.warning(DeprecationWarning("--mets-basename is deprecated. Use --mets/--directory instead")) |
25
|
|
|
mets_basename = mets_basename if mets_basename else 'mets.xml' |
26
|
|
|
if directory and mets_url: |
27
|
|
|
directory = abspath(directory) |
28
|
|
|
if not abspath(mets_url).startswith(directory): |
29
|
|
|
raise ValueError("--mets has a directory part inconsistent with --directory") |
30
|
|
|
elif not directory and mets_url: |
31
|
|
|
if mets_url.startswith('http') or mets_url.startswith('https:'): |
32
|
|
|
raise ValueError("--mets is an http(s) URL but no --directory was given") |
33
|
|
|
directory = dirname(abspath(mets_url)) or getcwd() |
34
|
|
|
elif directory and not mets_url: |
35
|
|
|
directory = abspath(directory) |
36
|
|
|
mets_url = join(directory, mets_basename) |
37
|
|
|
else: |
38
|
|
|
directory = getcwd() |
39
|
|
|
mets_url = join(directory, mets_basename) |
40
|
|
|
self.directory = directory |
41
|
|
|
self.resolver = Resolver() |
42
|
|
|
self.mets_url = mets_url |
43
|
|
|
self.automatic_backup = automatic_backup |
44
|
|
|
|
45
|
|
|
pass_workspace = click.make_pass_decorator(WorkspaceCtx) |
46
|
|
|
|
47
|
|
|
# ---------------------------------------------------------------------- |
48
|
|
|
# ocrd workspace |
49
|
|
|
# ---------------------------------------------------------------------- |
50
|
|
|
|
51
|
|
|
@click.group("workspace") |
52
|
|
|
@click.option('-d', '--directory', envvar='WORKSPACE_DIR', type=click.Path(file_okay=False), metavar='WORKSPACE_DIR', help='Changes the workspace folder location [default: METS_URL directory or .]"') |
53
|
|
|
@click.option('-M', '--mets-basename', default=None, help='METS file basename. Deprecated, use --mets/--directory') |
54
|
|
|
@click.option('-m', '--mets', default=None, help='The path/URL of the METS file [default: WORKSPACE_DIR/mets.xml]', metavar="METS_URL") |
55
|
|
|
@click.option('--backup', default=False, help="Backup mets.xml whenever it is saved.", is_flag=True) |
56
|
|
|
@click.pass_context |
57
|
|
|
def workspace_cli(ctx, directory, mets, mets_basename, backup): |
58
|
|
|
""" |
59
|
|
|
Working with workspace |
60
|
|
|
""" |
61
|
|
|
initLogging() |
62
|
|
|
ctx.obj = WorkspaceCtx(directory, mets_url=mets, mets_basename=mets_basename, automatic_backup=backup) |
63
|
|
|
|
64
|
|
|
# ---------------------------------------------------------------------- |
65
|
|
|
# ocrd workspace validate |
66
|
|
|
# ---------------------------------------------------------------------- |
67
|
|
|
|
68
|
|
|
@workspace_cli.command('validate', cls=command_with_replaced_help( |
69
|
|
|
(r' \[METS_URL\]', ''))) # XXX deprecated argument |
70
|
|
|
@pass_workspace |
71
|
|
|
@click.option('-a', '--download', is_flag=True, help="Download all files") |
72
|
|
|
@click.option('-s', '--skip', help="Tests to skip", default=[], multiple=True, type=click.Choice(['imagefilename', 'dimension', 'mets_unique_identifier', 'mets_file_group_names', 'mets_files', 'pixel_density', 'page', 'page_xsd', 'mets_xsd', 'url'])) |
73
|
|
|
@click.option('--page-textequiv-consistency', '--page-strictness', help="How strict to check PAGE multi-level textequiv consistency", type=click.Choice(['strict', 'lax', 'fix', 'off']), default='strict') |
74
|
|
|
@click.option('--page-coordinate-consistency', help="How fierce to check PAGE multi-level coordinate consistency", type=click.Choice(['poly', 'baseline', 'both', 'off']), default='poly') |
75
|
|
|
@click.argument('mets_url', default=None, required=False) |
76
|
|
|
def workspace_validate(ctx, mets_url, download, skip, page_textequiv_consistency, page_coordinate_consistency): |
77
|
|
|
""" |
78
|
|
|
Validate a workspace |
79
|
|
|
|
80
|
|
|
METS_URL can be a URL, an absolute path or a path relative to $PWD. |
81
|
|
|
If not given, use --mets accordingly. |
82
|
|
|
|
83
|
|
|
Check that the METS and its referenced file contents |
84
|
|
|
abide by the OCR-D specifications. |
85
|
|
|
""" |
86
|
|
|
LOG = getLogger('ocrd.cli.workspace.validate') |
87
|
|
|
if mets_url: |
88
|
|
|
LOG.warning(DeprecationWarning("Use 'ocrd workspace --mets METS init' instead of argument 'METS_URL' ('%s')" % mets_url)) |
89
|
|
|
else: |
90
|
|
|
mets_url = ctx.mets_url |
91
|
|
|
report = WorkspaceValidator.validate( |
92
|
|
|
ctx.resolver, |
93
|
|
|
mets_url, |
94
|
|
|
src_dir=ctx.directory, |
95
|
|
|
skip=skip, |
96
|
|
|
download=download, |
97
|
|
|
page_strictness=page_textequiv_consistency, |
98
|
|
|
page_coordinate_consistency=page_coordinate_consistency |
99
|
|
|
) |
100
|
|
|
print(report.to_xml()) |
101
|
|
|
if not report.is_valid: |
102
|
|
|
sys.exit(128) |
103
|
|
|
|
104
|
|
|
# ---------------------------------------------------------------------- |
105
|
|
|
# ocrd workspace clone |
106
|
|
|
# ---------------------------------------------------------------------- |
107
|
|
|
|
108
|
|
|
@workspace_cli.command('clone', cls=command_with_replaced_help( |
109
|
|
|
(r' \[WORKSPACE_DIR\]', ''))) # XXX deprecated argument |
110
|
|
|
@click.option('-f', '--clobber-mets', help="Overwrite existing METS file", default=False, is_flag=True) |
111
|
|
|
@click.option('-a', '--download', is_flag=True, help="Download all files and change location in METS file after cloning") |
112
|
|
|
@click.argument('mets_url') |
113
|
|
|
# XXX deprecated |
114
|
|
|
@click.argument('workspace_dir', default=None, required=False) |
115
|
|
|
@pass_workspace |
116
|
|
|
def workspace_clone(ctx, clobber_mets, download, mets_url, workspace_dir): |
117
|
|
|
""" |
118
|
|
|
Create a workspace from METS_URL and return the directory |
119
|
|
|
|
120
|
|
|
METS_URL can be a URL, an absolute path or a path relative to $PWD. |
121
|
|
|
If METS_URL is not provided, use --mets accordingly. |
122
|
|
|
METS_URL can also be an OAI-PMH GetRecord URL wrapping a METS file. |
123
|
|
|
""" |
124
|
|
|
LOG = getLogger('ocrd.cli.workspace.clone') |
125
|
|
|
if workspace_dir: |
126
|
|
|
LOG.warning(DeprecationWarning("Use 'ocrd workspace --directory DIR clone' instead of argument 'WORKSPACE_DIR' ('%s')" % workspace_dir)) |
127
|
|
|
ctx.directory = workspace_dir |
128
|
|
|
|
129
|
|
|
workspace = ctx.resolver.workspace_from_url( |
130
|
|
|
mets_url, |
131
|
|
|
dst_dir=os.path.abspath(ctx.directory), |
132
|
|
|
mets_basename=basename(ctx.mets_url), |
133
|
|
|
clobber_mets=clobber_mets, |
134
|
|
|
download=download, |
135
|
|
|
) |
136
|
|
|
workspace.save_mets() |
137
|
|
|
print(workspace.directory) |
138
|
|
|
|
139
|
|
|
# ---------------------------------------------------------------------- |
140
|
|
|
# ocrd workspace init |
141
|
|
|
# ---------------------------------------------------------------------- |
142
|
|
|
|
143
|
|
|
@workspace_cli.command('init', cls=command_with_replaced_help( |
144
|
|
|
(r' \[DIRECTORY\]', ''))) # XXX deprecated argument |
145
|
|
|
@click.option('-f', '--clobber-mets', help="Clobber mets.xml if it exists", is_flag=True, default=False) |
146
|
|
|
# XXX deprecated |
147
|
|
|
@click.argument('directory', default=None, required=False) |
148
|
|
|
@pass_workspace |
149
|
|
|
def workspace_init(ctx, clobber_mets, directory): |
150
|
|
|
""" |
151
|
|
|
Create a workspace with an empty METS file in --directory. |
152
|
|
|
|
153
|
|
|
""" |
154
|
|
|
LOG = getLogger('ocrd.cli.workspace.init') |
155
|
|
|
if directory: |
156
|
|
|
LOG.warning(DeprecationWarning("Use 'ocrd workspace --directory DIR init' instead of argument 'DIRECTORY' ('%s')" % directory)) |
157
|
|
|
ctx.directory = directory |
158
|
|
|
workspace = ctx.resolver.workspace_from_nothing( |
159
|
|
|
directory=os.path.abspath(ctx.directory), |
160
|
|
|
mets_basename=basename(ctx.mets_url), |
161
|
|
|
clobber_mets=clobber_mets |
162
|
|
|
) |
163
|
|
|
workspace.save_mets() |
164
|
|
|
print(workspace.directory) |
165
|
|
|
|
166
|
|
|
# ---------------------------------------------------------------------- |
167
|
|
|
# ocrd workspace add |
168
|
|
|
# ---------------------------------------------------------------------- |
169
|
|
|
|
170
|
|
|
@workspace_cli.command('add') |
171
|
|
|
@click.option('-G', '--file-grp', help="fileGrp USE", required=True, metavar='FILE_GRP') |
172
|
|
|
@click.option('-i', '--file-id', help="ID for the file", required=True, metavar='FILE_ID') |
173
|
|
|
@click.option('-m', '--mimetype', help="Media type of the file. Guessed from extension if not provided", required=False, metavar='TYPE') |
174
|
|
|
@click.option('-g', '--page-id', help="ID of the physical page", metavar='PAGE_ID') |
175
|
|
|
@click.option('-C', '--check-file-exists', help="Whether to ensure FNAME exists", is_flag=True, default=False) |
176
|
|
|
@click.option('--ignore', help="Do not check whether file exists.", default=False, is_flag=True) |
177
|
|
|
@click.option('--force', help="If file with ID already exists, replace it. No effect if --ignore is set.", default=False, is_flag=True) |
178
|
|
|
@click.argument('fname', required=True) |
179
|
|
|
@pass_workspace |
180
|
|
|
def workspace_add_file(ctx, file_grp, file_id, mimetype, page_id, ignore, check_file_exists, force, fname): |
181
|
|
|
""" |
182
|
|
|
Add a file or http(s) URL FNAME to METS in a workspace. |
183
|
|
|
If FNAME is not an http(s) URL and is not a workspace-local existing file, try to copy to workspace. |
184
|
|
|
""" |
185
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url), automatic_backup=ctx.automatic_backup) |
186
|
|
|
|
187
|
|
|
log = getLogger('ocrd.cli.workspace.add') |
188
|
|
|
if not mimetype: |
189
|
|
|
try: |
190
|
|
|
mimetype = EXT_TO_MIME[Path(fname).suffix] |
191
|
|
|
log.info("Guessed mimetype to be %s" % mimetype) |
192
|
|
|
except KeyError: |
193
|
|
|
log.error("Cannot guess mimetype from extension '%s' for '%s'. Set --mimetype explicitly" % (Path(fname).suffix, fname)) |
194
|
|
|
|
195
|
|
|
kwargs = {'fileGrp': file_grp, 'ID': file_id, 'mimetype': mimetype, 'pageId': page_id, 'force': force, 'ignore': ignore} |
196
|
|
|
log.debug("Adding '%s' (%s)", fname, kwargs) |
197
|
|
|
if not (fname.startswith('http://') or fname.startswith('https://')): |
198
|
|
|
if not fname.startswith(ctx.directory): |
199
|
|
|
if not isabs(fname) and exists(join(ctx.directory, fname)): |
200
|
|
|
fname = join(ctx.directory, fname) |
201
|
|
|
else: |
202
|
|
|
log.debug("File '%s' is not in workspace, copying", fname) |
203
|
|
|
try: |
204
|
|
|
fname = ctx.resolver.download_to_directory(ctx.directory, fname, subdir=file_grp) |
205
|
|
|
except FileNotFoundError: |
206
|
|
|
if check_file_exists: |
207
|
|
|
log.error("File '%s' does not exist, halt execution!" % fname) |
208
|
|
|
sys.exit(1) |
209
|
|
|
if check_file_exists and not exists(fname): |
210
|
|
|
log.error("File '%s' does not exist, halt execution!" % fname) |
211
|
|
|
sys.exit(1) |
212
|
|
|
if fname.startswith(ctx.directory): |
213
|
|
|
fname = relpath(fname, ctx.directory) |
214
|
|
|
kwargs['local_filename'] = fname |
215
|
|
|
|
216
|
|
|
kwargs['url'] = fname |
217
|
|
|
if not page_id: |
218
|
|
|
log.warning("You did not provide '--page-id/-g', so the file you added is not linked to a specific page.") |
219
|
|
|
workspace.mets.add_file(**kwargs) |
220
|
|
|
workspace.save_mets() |
221
|
|
|
|
222
|
|
|
# ---------------------------------------------------------------------- |
223
|
|
|
# ocrd workspace add-bulk |
224
|
|
|
# ---------------------------------------------------------------------- |
225
|
|
|
|
226
|
|
|
# pylint: disable=broad-except |
227
|
|
|
@workspace_cli.command('bulk-add') |
228
|
|
|
@click.option('-r', '--regex', help="Regular expression matching the FILE_GLOB filesystem paths to define named captures usable in the other parameters", required=True) |
229
|
|
|
@click.option('-m', '--mimetype', help="Media type of the file. If not provided, guess from filename", required=False) |
230
|
|
|
@click.option('-g', '--page-id', help="physical page ID of the file", required=False) |
231
|
|
|
@click.option('-i', '--file-id', help="ID of the file", required=True) |
232
|
|
|
@click.option('-u', '--url', help="local filesystem path in the workspace directory (copied from source file if different)", required=True) |
233
|
|
|
@click.option('-G', '--file-grp', help="File group USE of the file", required=True) |
234
|
|
|
@click.option('-n', '--dry-run', help="Don't actually do anything to the METS or filesystem, just preview", default=False, is_flag=True) |
235
|
|
|
@click.option('-I', '--ignore', help="Disable checking for existing file entries (faster)", default=False, is_flag=True) |
236
|
|
|
@click.option('-f', '--force', help="Replace existing file entries with the same ID (no effect when --ignore is set, too)", default=False, is_flag=True) |
237
|
|
|
@click.option('-s', '--skip', help="Skip files not matching --regex (instead of failing)", default=False, is_flag=True) |
238
|
|
|
@click.argument('file_glob', nargs=-1, required=True) |
239
|
|
|
@pass_workspace |
240
|
|
|
def workspace_cli_bulk_add(ctx, regex, mimetype, page_id, file_id, url, file_grp, dry_run, file_glob, ignore, force, skip): |
241
|
|
|
r""" |
242
|
|
|
Add files in bulk to an OCR-D workspace. |
243
|
|
|
|
244
|
|
|
FILE_GLOB can either be a shell glob expression or a list of files. |
245
|
|
|
|
246
|
|
|
--regex is applied to the absolute path of every file in FILE_GLOB and can |
247
|
|
|
define named groups that can be used in --page-id, --file-id, --mimetype, --url and |
248
|
|
|
--file-grp by referencing the named group 'grp' in the regex as '{{ grp }}'. |
249
|
|
|
|
250
|
|
|
\b |
251
|
|
|
Example: |
252
|
|
|
ocrd workspace bulk-add \\ |
253
|
|
|
--regex '^.*/(?P<fileGrp>[^/]+)/page_(?P<pageid>.*)\.(?P<ext>[^\.]*)$' \\ |
254
|
|
|
--file-id 'FILE_{{ fileGrp }}_{{ pageid }}' \\ |
255
|
|
|
--page-id 'PHYS_{{ pageid }}' \\ |
256
|
|
|
--file-grp "{{ fileGrp }}" \\ |
257
|
|
|
--url '{{ fileGrp }}/FILE_{{ pageid }}.{{ ext }}' \\ |
258
|
|
|
path/to/files/*/*.* |
259
|
|
|
|
260
|
|
|
""" |
261
|
|
|
log = getLogger('ocrd.cli.workspace.bulk-add') # pylint: disable=redefined-outer-name |
262
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url), automatic_backup=ctx.automatic_backup) |
263
|
|
|
|
264
|
|
|
try: |
265
|
|
|
pat = re.compile(regex) |
266
|
|
|
except Exception as e: |
267
|
|
|
log.error("Invalid regex: %s" % e) |
268
|
|
|
sys.exit(1) |
269
|
|
|
|
270
|
|
|
file_paths = [] |
271
|
|
|
for fglob in file_glob: |
272
|
|
|
file_paths += [Path(x).resolve() for x in glob(fglob)] |
273
|
|
|
|
274
|
|
|
for i, file_path in enumerate(file_paths): |
275
|
|
|
log.info("[%4d/%d] %s" % (i, len(file_paths), file_path)) |
276
|
|
|
|
277
|
|
|
# match regex |
278
|
|
|
m = pat.match(str(file_path)) |
279
|
|
|
if not m: |
280
|
|
|
if skip: |
281
|
|
|
continue |
282
|
|
|
log.error("File not matched by regex: '%s'" % file_path) |
283
|
|
|
sys.exit(1) |
284
|
|
|
group_dict = m.groupdict() |
285
|
|
|
|
286
|
|
|
# set up file info |
287
|
|
|
file_dict = {'url': url, 'mimetype': mimetype, 'ID': file_id, 'pageId': page_id, 'fileGrp': file_grp} |
288
|
|
|
|
289
|
|
|
# guess mime type |
290
|
|
|
if not file_dict['mimetype']: |
291
|
|
|
try: |
292
|
|
|
file_dict['mimetype'] = EXT_TO_MIME[file_path.suffix] |
293
|
|
|
except KeyError: |
294
|
|
|
log.error("Cannot guess mimetype from extension '%s' for '%s'. Set --mimetype explicitly" % (file_path.suffix, file_path)) |
295
|
|
|
|
296
|
|
|
# expand templates |
297
|
|
|
for param_name in file_dict: |
298
|
|
|
for group_name in group_dict: |
299
|
|
|
file_dict[param_name] = file_dict[param_name].replace('{{ %s }}' % group_name, group_dict[group_name]) |
300
|
|
|
|
301
|
|
|
# copy files |
302
|
|
|
if file_dict['url']: |
303
|
|
|
urlpath = Path(workspace.directory, file_dict['url']) |
304
|
|
|
if not urlpath.exists(): |
305
|
|
|
log.info("cp '%s' '%s'", file_path, urlpath) |
306
|
|
|
if not dry_run: |
307
|
|
|
if not urlpath.parent.is_dir(): |
308
|
|
|
urlpath.parent.mkdir() |
309
|
|
|
urlpath.write_bytes(file_path.read_bytes()) |
310
|
|
|
|
311
|
|
|
# Add to workspace (or not) |
312
|
|
|
fileGrp = file_dict.pop('fileGrp') |
313
|
|
|
if dry_run: |
314
|
|
|
log.info('workspace.add_file(%s)' % file_dict) |
315
|
|
|
else: |
316
|
|
|
workspace.add_file(fileGrp, ignore=ignore, force=force, **file_dict) |
317
|
|
|
|
318
|
|
|
# save changes to disk |
319
|
|
|
workspace.save_mets() |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
# ---------------------------------------------------------------------- |
323
|
|
|
# ocrd workspace find |
324
|
|
|
# ---------------------------------------------------------------------- |
325
|
|
|
|
326
|
|
|
@workspace_cli.command('find') |
327
|
|
|
@click.option('-G', '--file-grp', help="fileGrp USE", metavar='FILTER') |
328
|
|
|
@click.option('-m', '--mimetype', help="Media type to look for", metavar='FILTER') |
329
|
|
|
@click.option('-g', '--page-id', help="Page ID", metavar='FILTER') |
330
|
|
|
@click.option('-i', '--file-id', help="ID", metavar='FILTER') |
331
|
|
|
@click.option('-k', '--output-field', help="Output field. Repeat for multiple fields, will be joined with tab", |
332
|
|
|
default=['url'], |
333
|
|
|
multiple=True, |
334
|
|
|
type=click.Choice([ |
335
|
|
|
'url', |
336
|
|
|
'mimetype', |
337
|
|
|
'pageId', |
338
|
|
|
'ID', |
339
|
|
|
'fileGrp', |
340
|
|
|
'basename', |
341
|
|
|
'basename_without_extension', |
342
|
|
|
'local_filename', |
343
|
|
|
])) |
344
|
|
|
@click.option('--download', is_flag=True, help="Download found files to workspace and change location in METS file ") |
345
|
|
|
@pass_workspace |
346
|
|
|
def workspace_find(ctx, file_grp, mimetype, page_id, file_id, output_field, download): |
347
|
|
|
""" |
348
|
|
|
Find files. |
349
|
|
|
|
350
|
|
|
(If any ``FILTER`` starts with ``//``, then its remainder |
351
|
|
|
will be interpreted as a regular expression.) |
352
|
|
|
""" |
353
|
|
|
modified_mets = False |
354
|
|
|
ret = list() |
355
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url)) |
356
|
|
|
for f in workspace.mets.find_files( |
357
|
|
|
ID=file_id, |
358
|
|
|
fileGrp=file_grp, |
359
|
|
|
mimetype=mimetype, |
360
|
|
|
pageId=page_id, |
361
|
|
|
): |
362
|
|
|
if download and not f.local_filename: |
363
|
|
|
workspace.download_file(f) |
364
|
|
|
modified_mets = True |
365
|
|
|
ret.append([f.ID if field == 'pageId' else getattr(f, field) or '' |
366
|
|
|
for field in output_field]) |
367
|
|
|
if modified_mets: |
368
|
|
|
workspace.save_mets() |
369
|
|
|
if 'pageId' in output_field: |
370
|
|
|
idx = output_field.index('pageId') |
371
|
|
|
fileIds = list(map(lambda fields: fields[idx], ret)) |
|
|
|
|
372
|
|
|
pages = workspace.mets.get_physical_pages(for_fileIds=fileIds) |
373
|
|
|
for fields, page in zip(ret, pages): |
374
|
|
|
fields[idx] = page or '' |
375
|
|
|
for fields in ret: |
376
|
|
|
print('\t'.join(fields)) |
377
|
|
|
|
378
|
|
|
# ---------------------------------------------------------------------- |
379
|
|
|
# ocrd workspace remove |
380
|
|
|
# ---------------------------------------------------------------------- |
381
|
|
|
|
382
|
|
|
@workspace_cli.command('remove') |
383
|
|
|
@click.option('-k', '--keep-file', help="Do not delete file from file system", default=False, is_flag=True) |
384
|
|
|
@click.option('-f', '--force', help="Continue even if mets:file or file on file system does not exist", default=False, is_flag=True) |
385
|
|
|
@click.argument('ID', nargs=-1) |
386
|
|
|
@pass_workspace |
387
|
|
|
def workspace_remove_file(ctx, id, force, keep_file): # pylint: disable=redefined-builtin |
388
|
|
|
""" |
389
|
|
|
Delete files (given by their ID attribute ``ID``). |
390
|
|
|
|
391
|
|
|
(If any ``ID`` starts with ``//``, then its remainder |
392
|
|
|
will be interpreted as a regular expression.) |
393
|
|
|
""" |
394
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url), automatic_backup=ctx.automatic_backup) |
395
|
|
|
for i in id: |
396
|
|
|
workspace.remove_file(i, force=force, keep_file=keep_file) |
397
|
|
|
workspace.save_mets() |
398
|
|
|
|
399
|
|
|
|
400
|
|
|
# ---------------------------------------------------------------------- |
401
|
|
|
# ocrd workspace rename-group |
402
|
|
|
# ---------------------------------------------------------------------- |
403
|
|
|
|
404
|
|
|
@workspace_cli.command('rename-group') |
405
|
|
|
@click.argument('OLD', nargs=1) |
406
|
|
|
@click.argument('NEW', nargs=1) |
407
|
|
|
@pass_workspace |
408
|
|
|
def rename_group(ctx, old, new): |
409
|
|
|
""" |
410
|
|
|
Rename fileGrp (USE attribute ``NEW`` to ``OLD``). |
411
|
|
|
""" |
412
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url)) |
413
|
|
|
workspace.rename_file_group(old, new) |
414
|
|
|
workspace.save_mets() |
415
|
|
|
|
416
|
|
|
# ---------------------------------------------------------------------- |
417
|
|
|
# ocrd workspace remove-group |
418
|
|
|
# ---------------------------------------------------------------------- |
419
|
|
|
|
420
|
|
|
@workspace_cli.command('remove-group') |
421
|
|
|
@click.option('-r', '--recursive', help="Delete any files in the group before the group itself", default=False, is_flag=True) |
422
|
|
|
@click.option('-f', '--force', help="Continue removing even if group or containing files not found in METS", default=False, is_flag=True) |
423
|
|
|
@click.option('-k', '--keep-files', help="Do not delete files from file system", default=False, is_flag=True) |
424
|
|
|
@click.argument('GROUP', nargs=-1) |
425
|
|
|
@pass_workspace |
426
|
|
|
def remove_group(ctx, group, recursive, force, keep_files): |
427
|
|
|
""" |
428
|
|
|
Delete fileGrps (given by their USE attribute ``GROUP``). |
429
|
|
|
|
430
|
|
|
(If any ``GROUP`` starts with ``//``, then its remainder |
431
|
|
|
will be interpreted as a regular expression.) |
432
|
|
|
""" |
433
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url)) |
434
|
|
|
for g in group: |
435
|
|
|
workspace.remove_file_group(g, recursive=recursive, force=force, keep_files=keep_files) |
436
|
|
|
workspace.save_mets() |
437
|
|
|
|
438
|
|
|
# ---------------------------------------------------------------------- |
439
|
|
|
# ocrd workspace prune-files |
440
|
|
|
# ---------------------------------------------------------------------- |
441
|
|
|
|
442
|
|
|
@workspace_cli.command('prune-files') |
443
|
|
|
@click.option('-G', '--file-grp', help="fileGrp USE", metavar='FILTER') |
444
|
|
|
@click.option('-m', '--mimetype', help="Media type to look for", metavar='FILTER') |
445
|
|
|
@click.option('-g', '--page-id', help="Page ID", metavar='FILTER') |
446
|
|
|
@click.option('-i', '--file-id', help="ID", metavar='FILTER') |
447
|
|
|
@pass_workspace |
448
|
|
|
def prune_files(ctx, file_grp, mimetype, page_id, file_id): |
449
|
|
|
""" |
450
|
|
|
Removes mets:files that point to non-existing local files |
451
|
|
|
|
452
|
|
|
(If any ``FILTER`` starts with ``//``, then its remainder |
453
|
|
|
will be interpreted as a regular expression.) |
454
|
|
|
""" |
455
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url), automatic_backup=ctx.automatic_backup) |
456
|
|
|
with pushd_popd(workspace.directory): |
457
|
|
|
for f in workspace.mets.find_files( |
458
|
|
|
ID=file_id, |
459
|
|
|
fileGrp=file_grp, |
460
|
|
|
mimetype=mimetype, |
461
|
|
|
pageId=page_id, |
462
|
|
|
): |
463
|
|
|
try: |
464
|
|
|
if not f.local_filename or not exists(f.local_filename): |
465
|
|
|
workspace.mets.remove_file(f.ID) |
466
|
|
|
except Exception as e: |
467
|
|
|
ctx.log.exception("Error removing %f: %s", f, e) |
468
|
|
|
raise(e) |
469
|
|
|
workspace.save_mets() |
470
|
|
|
|
471
|
|
|
# ---------------------------------------------------------------------- |
472
|
|
|
# ocrd workspace list-group |
473
|
|
|
# ---------------------------------------------------------------------- |
474
|
|
|
|
475
|
|
|
@workspace_cli.command('list-group') |
476
|
|
|
@pass_workspace |
477
|
|
|
def list_groups(ctx): |
478
|
|
|
""" |
479
|
|
|
List fileGrp USE attributes |
480
|
|
|
""" |
481
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url)) |
482
|
|
|
print("\n".join(workspace.mets.file_groups)) |
483
|
|
|
|
484
|
|
|
# ---------------------------------------------------------------------- |
485
|
|
|
# ocrd workspace list-pages |
486
|
|
|
# ---------------------------------------------------------------------- |
487
|
|
|
|
488
|
|
|
@workspace_cli.command('list-page') |
489
|
|
|
@pass_workspace |
490
|
|
|
def list_pages(ctx): |
491
|
|
|
""" |
492
|
|
|
List physical page IDs |
493
|
|
|
""" |
494
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url)) |
495
|
|
|
print("\n".join(workspace.mets.physical_pages)) |
496
|
|
|
|
497
|
|
|
# ---------------------------------------------------------------------- |
498
|
|
|
# ocrd workspace get-id |
499
|
|
|
# ---------------------------------------------------------------------- |
500
|
|
|
|
501
|
|
|
@workspace_cli.command('get-id') |
502
|
|
|
@pass_workspace |
503
|
|
|
def get_id(ctx): |
504
|
|
|
""" |
505
|
|
|
Get METS id if any |
506
|
|
|
""" |
507
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url)) |
508
|
|
|
ID = workspace.mets.unique_identifier |
509
|
|
|
if ID: |
510
|
|
|
print(ID) |
511
|
|
|
|
512
|
|
|
# ---------------------------------------------------------------------- |
513
|
|
|
# ocrd workspace set-id |
514
|
|
|
# ---------------------------------------------------------------------- |
515
|
|
|
|
516
|
|
|
@workspace_cli.command('set-id') |
517
|
|
|
@click.argument('ID') |
518
|
|
|
@pass_workspace |
519
|
|
|
def set_id(ctx, id): # pylint: disable=redefined-builtin |
520
|
|
|
""" |
521
|
|
|
Set METS ID. |
522
|
|
|
|
523
|
|
|
If one of the supported identifier mechanisms is used, will set this identifier. |
524
|
|
|
|
525
|
|
|
Otherwise will create a new <mods:identifier type="purl">{{ ID }}</mods:identifier>. |
526
|
|
|
""" |
527
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url), automatic_backup=ctx.automatic_backup) |
528
|
|
|
workspace.mets.unique_identifier = id |
529
|
|
|
workspace.save_mets() |
530
|
|
|
|
531
|
|
|
# ---------------------------------------------------------------------- |
532
|
|
|
# ocrd workspace merge |
533
|
|
|
# ---------------------------------------------------------------------- |
534
|
|
|
|
535
|
|
|
@workspace_cli.command('merge') |
536
|
|
|
@click.argument('METS_PATH') |
537
|
|
|
@click.option('--copy-files', is_flag=True, help="Copy files as well", default=True) |
538
|
|
|
@click.option('--fileGrp-mapping', help="JSON object mapping src to dest fileGrp") |
539
|
|
|
@pass_workspace |
540
|
|
|
def merge(ctx, copy_files, filegrp_mapping, mets_path): # pylint: disable=redefined-builtin |
541
|
|
|
""" |
542
|
|
|
Merges this workspace with the workspace that contains ``METS_PATH`` |
543
|
|
|
""" |
544
|
|
|
mets_path = Path(mets_path) |
545
|
|
|
if filegrp_mapping: |
546
|
|
|
filegrp_mapping = loads(filegrp_mapping) |
547
|
|
|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url), automatic_backup=ctx.automatic_backup) |
548
|
|
|
other_workspace = Workspace(ctx.resolver, directory=str(mets_path.parent), mets_basename=str(mets_path.name)) |
549
|
|
|
workspace.merge(other_workspace, copy_files=copy_files, fileGrp_mapping=filegrp_mapping) |
550
|
|
|
workspace.save_mets() |
551
|
|
|
|
552
|
|
|
# ---------------------------------------------------------------------- |
553
|
|
|
# ocrd workspace backup |
554
|
|
|
# ---------------------------------------------------------------------- |
555
|
|
|
|
556
|
|
|
@workspace_cli.group('backup') |
557
|
|
|
@click.pass_context |
558
|
|
|
def workspace_backup_cli(ctx): # pylint: disable=unused-argument |
559
|
|
|
""" |
560
|
|
|
Backing and restoring workspaces - dev edition |
561
|
|
|
""" |
562
|
|
|
|
563
|
|
|
@workspace_backup_cli.command('add') |
564
|
|
|
@pass_workspace |
565
|
|
|
def workspace_backup_add(ctx): |
566
|
|
|
""" |
567
|
|
|
Create a new backup |
568
|
|
|
""" |
569
|
|
|
backup_manager = WorkspaceBackupManager(Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url), automatic_backup=ctx.automatic_backup)) |
570
|
|
|
backup_manager.add() |
571
|
|
|
|
572
|
|
|
@workspace_backup_cli.command('list') |
573
|
|
|
@pass_workspace |
574
|
|
|
def workspace_backup_list(ctx): |
575
|
|
|
""" |
576
|
|
|
List backups |
577
|
|
|
""" |
578
|
|
|
backup_manager = WorkspaceBackupManager(Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url), automatic_backup=ctx.automatic_backup)) |
579
|
|
|
for b in backup_manager.list(): |
580
|
|
|
print(b) |
581
|
|
|
|
582
|
|
|
@workspace_backup_cli.command('restore') |
583
|
|
|
@click.option('-f', '--choose-first', help="Restore first matching version if more than one", is_flag=True) |
584
|
|
|
@click.argument('bak') #, type=click.Path(dir_okay=False, readable=True, resolve_path=True)) |
585
|
|
|
@pass_workspace |
586
|
|
|
def workspace_backup_restore(ctx, choose_first, bak): |
587
|
|
|
""" |
588
|
|
|
Restore backup BAK |
589
|
|
|
""" |
590
|
|
|
backup_manager = WorkspaceBackupManager(Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url), automatic_backup=ctx.automatic_backup)) |
591
|
|
|
backup_manager.restore(bak, choose_first) |
592
|
|
|
|
593
|
|
|
@workspace_backup_cli.command('undo') |
594
|
|
|
@pass_workspace |
595
|
|
|
def workspace_backup_undo(ctx): |
596
|
|
|
""" |
597
|
|
|
Restore the last backup |
598
|
|
|
""" |
599
|
|
|
backup_manager = WorkspaceBackupManager(Workspace(ctx.resolver, directory=ctx.directory, mets_basename=basename(ctx.mets_url), automatic_backup=ctx.automatic_backup)) |
600
|
|
|
backup_manager.undo() |
601
|
|
|
|