tro-utils 0.5.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tro_utils/__init__.py +72 -0
- tro_utils/_version.py +24 -0
- tro_utils/certs/freetsa-root.pem +52 -0
- tro_utils/certs/freetsa-tsa.pem +100 -0
- tro_utils/cli.py +738 -0
- tro_utils/default.jinja2 +86 -0
- tro_utils/models/__init__.py +36 -0
- tro_utils/models/_base.py +68 -0
- tro_utils/models/agent.py +176 -0
- tro_utils/models/arrangement.py +378 -0
- tro_utils/models/artifact.py +101 -0
- tro_utils/models/attribute.py +51 -0
- tro_utils/models/composition.py +144 -0
- tro_utils/models/hash_value.py +101 -0
- tro_utils/models/performance.py +195 -0
- tro_utils/models/tro.py +509 -0
- tro_utils/models/trs.py +329 -0
- tro_utils/models/tsa.py +44 -0
- tro_utils/replication_package.py +179 -0
- tro_utils/static/trace-logo.png +0 -0
- tro_utils/tro_utils.py +740 -0
- tro_utils/trs_keys.py +251 -0
- tro_utils/tsa.py +149 -0
- tro_utils-0.5.4.dist-info/METADATA +222 -0
- tro_utils-0.5.4.dist-info/RECORD +29 -0
- tro_utils-0.5.4.dist-info/WHEEL +5 -0
- tro_utils-0.5.4.dist-info/entry_points.txt +2 -0
- tro_utils-0.5.4.dist-info/licenses/LICENSE +32 -0
- tro_utils-0.5.4.dist-info/top_level.txt +1 -0
tro_utils/cli.py
ADDED
|
@@ -0,0 +1,738 @@
|
|
|
1
|
+
"""Console script for tro_utils."""
|
|
2
|
+
|
|
3
|
+
import pathlib
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
import click
|
|
7
|
+
from rich.console import Console
|
|
8
|
+
from rich.table import Table
|
|
9
|
+
|
|
10
|
+
from . import TRPAttribute, __version__
|
|
11
|
+
from .models.arrangement import ArtifactArrangement
|
|
12
|
+
from .models.composition import ArtifactComposition
|
|
13
|
+
from .tro_utils import TRO, normalize_fingerprint
|
|
14
|
+
|
|
15
|
+
console = Console()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _parse_arrangement_ref(value: str) -> tuple[str, str | None]:
|
|
19
|
+
"""Parse ``ARRANGEMENT_ID`` or ``ARRANGEMENT_ID:MOUNT_PATH`` into a ``(id, path)`` tuple."""
|
|
20
|
+
arrangement_id, _, path = value.partition(":")
|
|
21
|
+
return (arrangement_id, path or None)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _parse_extra_context_value(ctx, param, value):
|
|
25
|
+
"""Parse ``--extra-context`` values into a JSON-LD context dict.
|
|
26
|
+
|
|
27
|
+
Each value must be in ``PREFIX=URI`` form (e.g. ``ex=http://example.org/``).
|
|
28
|
+
"""
|
|
29
|
+
result = {}
|
|
30
|
+
for v in value:
|
|
31
|
+
if "=" not in v:
|
|
32
|
+
raise click.BadParameter(
|
|
33
|
+
f"{v!r} is not a valid PREFIX=URI mapping.",
|
|
34
|
+
param=param,
|
|
35
|
+
ctx=ctx,
|
|
36
|
+
)
|
|
37
|
+
prefix, _, uri = v.partition("=")
|
|
38
|
+
result[prefix] = uri
|
|
39
|
+
return result
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# click.DateTime()'s defaults accept no UTC offset, so add the offset-bearing
|
|
43
|
+
# variants. Values given without one are treated as local time by the model.
|
|
44
|
+
_DATETIME_FORMATS = [
|
|
45
|
+
"%Y-%m-%d",
|
|
46
|
+
"%Y-%m-%dT%H:%M:%S",
|
|
47
|
+
"%Y-%m-%d %H:%M:%S",
|
|
48
|
+
"%Y-%m-%dT%H:%M:%S%z",
|
|
49
|
+
"%Y-%m-%d %H:%M:%S%z",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
_TEMPLATES = {
|
|
53
|
+
"default": {
|
|
54
|
+
"description": "Default pretty template by Craig Willis",
|
|
55
|
+
"filename": "default.jinja2",
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class StringOrPath(click.ParamType):
|
|
61
|
+
"""Custom parameter type to accept either a valid string or a file path."""
|
|
62
|
+
|
|
63
|
+
name = "string_or_path"
|
|
64
|
+
|
|
65
|
+
def __init__(self, templates=None):
|
|
66
|
+
if templates is None:
|
|
67
|
+
templates = {}
|
|
68
|
+
self.valid_strings = templates.keys()
|
|
69
|
+
|
|
70
|
+
def convert(self, value, param, ctx):
|
|
71
|
+
# Check if the value is in the allowed string set
|
|
72
|
+
if value in self.valid_strings:
|
|
73
|
+
return value
|
|
74
|
+
# Check if the value is a valid path
|
|
75
|
+
elif pathlib.Path(value).exists() and pathlib.Path(value).is_file():
|
|
76
|
+
return value
|
|
77
|
+
else:
|
|
78
|
+
self.fail(
|
|
79
|
+
f"'{value}' is neither a valid option ({', '.join(self.valid_strings)}) "
|
|
80
|
+
f"nor a valid file path.",
|
|
81
|
+
param,
|
|
82
|
+
ctx,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@click.group()
|
|
87
|
+
@click.version_option(__version__, "--version", "-v", prog_name="tro-utils")
|
|
88
|
+
@click.option(
|
|
89
|
+
"--declaration",
|
|
90
|
+
type=click.Path(),
|
|
91
|
+
required=False,
|
|
92
|
+
help="Path to the TRO declaration file",
|
|
93
|
+
)
|
|
94
|
+
@click.option(
|
|
95
|
+
"--profile",
|
|
96
|
+
envvar="TRS_PROFILE",
|
|
97
|
+
type=click.Path(),
|
|
98
|
+
required=False,
|
|
99
|
+
help="Path to the TRS profile file",
|
|
100
|
+
)
|
|
101
|
+
@click.option(
|
|
102
|
+
"--gpg-fingerprint",
|
|
103
|
+
envvar="GPG_FINGERPRINT",
|
|
104
|
+
type=click.STRING,
|
|
105
|
+
required=False,
|
|
106
|
+
help="GPG fingerprint for signing TRO",
|
|
107
|
+
)
|
|
108
|
+
@click.option(
|
|
109
|
+
"--gpg-passphrase",
|
|
110
|
+
envvar="GPG_PASSPHRASE",
|
|
111
|
+
type=click.STRING,
|
|
112
|
+
required=False,
|
|
113
|
+
help="GPG passphrase for signing TRO",
|
|
114
|
+
)
|
|
115
|
+
@click.option(
|
|
116
|
+
"--tro-creator",
|
|
117
|
+
envvar="TRO_CREATOR",
|
|
118
|
+
type=click.STRING,
|
|
119
|
+
required=False,
|
|
120
|
+
help="TRO creator (only used when creating a new TRO)",
|
|
121
|
+
)
|
|
122
|
+
@click.option(
|
|
123
|
+
"--tro-creator-type",
|
|
124
|
+
envvar="TRO_CREATOR_TYPE",
|
|
125
|
+
type=click.Choice(["person", "organization"]),
|
|
126
|
+
required=False,
|
|
127
|
+
default="organization",
|
|
128
|
+
show_default=True,
|
|
129
|
+
help=(
|
|
130
|
+
"Whether --tro-creator names a schema:Person or a schema:Organization "
|
|
131
|
+
"(only used when creating a new TRO)"
|
|
132
|
+
),
|
|
133
|
+
)
|
|
134
|
+
@click.option(
|
|
135
|
+
"--tro-name",
|
|
136
|
+
envvar="TRO_NAME",
|
|
137
|
+
type=click.STRING,
|
|
138
|
+
required=False,
|
|
139
|
+
help="TRO name (only used when creating a new TRO)",
|
|
140
|
+
)
|
|
141
|
+
@click.option(
|
|
142
|
+
"--tro-description",
|
|
143
|
+
envvar="TRO_DESCRIPTION",
|
|
144
|
+
type=click.STRING,
|
|
145
|
+
required=False,
|
|
146
|
+
help="TRO description (only used when creating a new TRO)",
|
|
147
|
+
)
|
|
148
|
+
@click.option(
|
|
149
|
+
"--extra-context",
|
|
150
|
+
"-c",
|
|
151
|
+
envvar="TRO_EXTRA_CONTEXT",
|
|
152
|
+
type=click.STRING,
|
|
153
|
+
required=False,
|
|
154
|
+
multiple=True,
|
|
155
|
+
callback=_parse_extra_context_value,
|
|
156
|
+
is_eager=False,
|
|
157
|
+
help=(
|
|
158
|
+
"Extra PREFIX=URI vocabulary mapping to append to @context "
|
|
159
|
+
"(e.g. ex=http://example.org/). May be repeated."
|
|
160
|
+
),
|
|
161
|
+
)
|
|
162
|
+
def cli(
|
|
163
|
+
declaration,
|
|
164
|
+
profile,
|
|
165
|
+
gpg_fingerprint,
|
|
166
|
+
gpg_passphrase,
|
|
167
|
+
tro_creator,
|
|
168
|
+
tro_creator_type,
|
|
169
|
+
tro_name,
|
|
170
|
+
tro_description,
|
|
171
|
+
extra_context,
|
|
172
|
+
):
|
|
173
|
+
pass
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _resolve_declaration(ctx, declaration):
|
|
177
|
+
"""Return the declaration given as an argument or as the global --declaration.
|
|
178
|
+
|
|
179
|
+
The verify commands predate the global option and take the declaration as
|
|
180
|
+
an argument; accept either, but not two different files.
|
|
181
|
+
"""
|
|
182
|
+
global_declaration = ctx.parent.params.get("declaration")
|
|
183
|
+
if declaration is None:
|
|
184
|
+
declaration = global_declaration
|
|
185
|
+
elif global_declaration is not None and (
|
|
186
|
+
pathlib.Path(declaration).resolve()
|
|
187
|
+
!= pathlib.Path(global_declaration).resolve()
|
|
188
|
+
):
|
|
189
|
+
raise click.UsageError(
|
|
190
|
+
f"Got two different declarations: {global_declaration!r} "
|
|
191
|
+
f"(--declaration) and {declaration!r}."
|
|
192
|
+
)
|
|
193
|
+
if declaration is None:
|
|
194
|
+
raise click.UsageError(
|
|
195
|
+
"Missing the declaration. Pass it as an argument or with the "
|
|
196
|
+
f"global option: tro-utils --declaration FILE {ctx.info_name} ..."
|
|
197
|
+
)
|
|
198
|
+
if not pathlib.Path(declaration).exists():
|
|
199
|
+
raise click.BadParameter(
|
|
200
|
+
f"Path {declaration!r} does not exist.", param_hint="'--declaration'"
|
|
201
|
+
)
|
|
202
|
+
return declaration
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
@cli.command(
|
|
206
|
+
help="Verify the TRO's timestamp (use verify-signature to check its signature)"
|
|
207
|
+
)
|
|
208
|
+
@click.argument("declaration", type=click.Path(exists=True), required=False)
|
|
209
|
+
@click.pass_context
|
|
210
|
+
def verify_timestamp(ctx, declaration):
|
|
211
|
+
declaration = _resolve_declaration(ctx, declaration)
|
|
212
|
+
tro = TRO(
|
|
213
|
+
filepath=declaration,
|
|
214
|
+
)
|
|
215
|
+
mismatch = tro.created_with_mismatch()
|
|
216
|
+
if mismatch:
|
|
217
|
+
click.secho(f"Warning: {mismatch}", fg="yellow", err=True)
|
|
218
|
+
try:
|
|
219
|
+
tro.verify_timestamp()
|
|
220
|
+
except RuntimeError as err:
|
|
221
|
+
raise click.ClickException(str(err)) from err
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _normalize_fingerprints(ctx, param, values):
|
|
225
|
+
try:
|
|
226
|
+
return tuple(normalize_fingerprint(value) for value in values)
|
|
227
|
+
except ValueError as err:
|
|
228
|
+
raise click.BadParameter(str(err)) from err
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
@cli.command(
|
|
232
|
+
name="verify-signature",
|
|
233
|
+
help="Verify the TRS signature of the TRO against your GPG keyring",
|
|
234
|
+
)
|
|
235
|
+
@click.argument("declaration", type=click.Path(exists=True), required=False)
|
|
236
|
+
@click.option(
|
|
237
|
+
"--use-embedded-key",
|
|
238
|
+
is_flag=True,
|
|
239
|
+
help="Verify with the public key embedded in the TRO instead of your "
|
|
240
|
+
"keyring. This only shows the TRO is self-consistent, not who signed it.",
|
|
241
|
+
)
|
|
242
|
+
@click.option(
|
|
243
|
+
"--gnupghome",
|
|
244
|
+
type=click.Path(exists=True, file_okay=False),
|
|
245
|
+
help="GnuPG home directory holding the keys you trust",
|
|
246
|
+
)
|
|
247
|
+
@click.option(
|
|
248
|
+
"--keyring",
|
|
249
|
+
type=click.Path(exists=True, dir_okay=False),
|
|
250
|
+
help="Key file (armored or binary), e.g. as published by the TRS, to "
|
|
251
|
+
"verify with instead of your keyring. Only the keys in it are used.",
|
|
252
|
+
)
|
|
253
|
+
@click.option(
|
|
254
|
+
"--fingerprint",
|
|
255
|
+
"fingerprints",
|
|
256
|
+
multiple=True,
|
|
257
|
+
callback=_normalize_fingerprints,
|
|
258
|
+
help="Full fingerprint the signing key must have, e.g. as published by "
|
|
259
|
+
"the TRS. Can be given more than once.",
|
|
260
|
+
)
|
|
261
|
+
@click.option(
|
|
262
|
+
"--trs-keys",
|
|
263
|
+
type=click.Path(exists=True, dir_okay=False),
|
|
264
|
+
help="TRS key list (JSON, or YAML if it ends in .yaml/.yml) saying which "
|
|
265
|
+
"keys the TRS signed with and when. The key is judged by it at the time "
|
|
266
|
+
"of the TRO's trusted timestamp, which is verified too. Uses the key "
|
|
267
|
+
"embedded in the TRO unless --keyring or --gnupghome is given.",
|
|
268
|
+
)
|
|
269
|
+
@click.pass_context
|
|
270
|
+
def verify_signature(
|
|
271
|
+
ctx, declaration, use_embedded_key, gnupghome, keyring, fingerprints, trs_keys
|
|
272
|
+
):
|
|
273
|
+
declaration = _resolve_declaration(ctx, declaration)
|
|
274
|
+
if use_embedded_key + bool(gnupghome) + bool(keyring) > 1:
|
|
275
|
+
raise click.UsageError(
|
|
276
|
+
"--use-embedded-key, --gnupghome and --keyring are mutually exclusive"
|
|
277
|
+
)
|
|
278
|
+
# TRO derives the .sig path from the stem and always reads <stem>.jsonld,
|
|
279
|
+
# so any other name would verify a different file than the one given.
|
|
280
|
+
if pathlib.Path(declaration).suffix != ".jsonld":
|
|
281
|
+
raise click.BadParameter(
|
|
282
|
+
f"expected a .jsonld declaration, got {declaration!r}",
|
|
283
|
+
param_hint="DECLARATION",
|
|
284
|
+
)
|
|
285
|
+
try:
|
|
286
|
+
tro = TRO(filepath=declaration)
|
|
287
|
+
result = tro.verify_signature(
|
|
288
|
+
use_embedded_key=use_embedded_key,
|
|
289
|
+
gnupghome=gnupghome,
|
|
290
|
+
keyring=keyring,
|
|
291
|
+
fingerprints=fingerprints,
|
|
292
|
+
trs_keys=trs_keys,
|
|
293
|
+
)
|
|
294
|
+
except (RuntimeError, ValueError, OSError) as err:
|
|
295
|
+
raise click.ClickException(str(err)) from err
|
|
296
|
+
|
|
297
|
+
if result.key_missing and result.used_embedded_key:
|
|
298
|
+
raise click.ClickException(
|
|
299
|
+
"The public key embedded in the TRO (trov:publicKey) did not sign "
|
|
300
|
+
f"it; the signing key is {result.fingerprint}."
|
|
301
|
+
)
|
|
302
|
+
if result.key_missing and result.keyring:
|
|
303
|
+
raise click.ClickException(
|
|
304
|
+
f"Signing key {result.fingerprint} is not in {result.keyring}."
|
|
305
|
+
)
|
|
306
|
+
if result.key_missing:
|
|
307
|
+
raise click.ClickException(
|
|
308
|
+
f"Signing key {result.fingerprint} is not in your keyring. Obtain "
|
|
309
|
+
"the TRS's public key from a source you trust and import it with "
|
|
310
|
+
"'gpg --import', or use --use-embedded-key to check only that the "
|
|
311
|
+
"TRO is self-consistent."
|
|
312
|
+
)
|
|
313
|
+
if not result.valid:
|
|
314
|
+
key = f" (key {result.fingerprint})" if result.fingerprint else ""
|
|
315
|
+
raise click.ClickException(
|
|
316
|
+
f"Signature verification failed: {result.status}{key}"
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
click.secho("Signature: OK", fg="green")
|
|
320
|
+
click.echo(f"Signed by: {result.username}")
|
|
321
|
+
click.echo(f"Key fingerprint: {result.fingerprint}")
|
|
322
|
+
if result.fingerprint_matches:
|
|
323
|
+
click.echo("Key confirmed by: the fingerprint you gave")
|
|
324
|
+
if result.listed_status:
|
|
325
|
+
click.echo(f"Key listed in: {trs_keys} ({result.listed_status})")
|
|
326
|
+
if result.timestamp:
|
|
327
|
+
click.echo(f"Timestamp: {result.timestamp.isoformat()}")
|
|
328
|
+
if result.keyring:
|
|
329
|
+
click.echo(f"Key found in: {result.keyring}")
|
|
330
|
+
elif not result.used_embedded_key:
|
|
331
|
+
click.echo(f"Key trust: {result.trust}")
|
|
332
|
+
if not result.trusted and result.used_embedded_key:
|
|
333
|
+
click.secho(
|
|
334
|
+
"Warning: verified with the key embedded in the TRO; the key is "
|
|
335
|
+
"attested only by the TRO itself.",
|
|
336
|
+
fg="yellow",
|
|
337
|
+
err=True,
|
|
338
|
+
)
|
|
339
|
+
elif not result.trusted:
|
|
340
|
+
click.secho(
|
|
341
|
+
"Warning: the signing key is in your keyring, but you have not "
|
|
342
|
+
"certified it. Confirm its fingerprint against a source you "
|
|
343
|
+
"trust, such as the TRS's website, then certify it with "
|
|
344
|
+
f"'gpg --lsign-key {result.fingerprint}' or pass it with "
|
|
345
|
+
"--fingerprint.",
|
|
346
|
+
fg="yellow",
|
|
347
|
+
err=True,
|
|
348
|
+
)
|
|
349
|
+
for warning in result.warnings:
|
|
350
|
+
click.secho(f"Warning: {warning}", fg="yellow", err=True)
|
|
351
|
+
if result.embedded_key_matches is False:
|
|
352
|
+
click.secho(
|
|
353
|
+
"Warning: the public key embedded in the TRO (trov:publicKey) is "
|
|
354
|
+
"not the key that signed it.",
|
|
355
|
+
fg="yellow",
|
|
356
|
+
err=True,
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
@cli.command(help="Verify the integrity of the TRO")
|
|
361
|
+
@click.argument("declaration", type=click.Path(exists=True), required=False)
|
|
362
|
+
# Optional only so that DECLARATION can be left out; the command checks it.
|
|
363
|
+
@click.argument(
|
|
364
|
+
"package", type=click.Path(exists=True), required=False, metavar="PACKAGE"
|
|
365
|
+
)
|
|
366
|
+
@click.option(
|
|
367
|
+
"--arrangement-id",
|
|
368
|
+
"-a",
|
|
369
|
+
type=click.STRING,
|
|
370
|
+
required=False,
|
|
371
|
+
help="ID of the arrangement to verify. If not provided all arrangements will be tried",
|
|
372
|
+
)
|
|
373
|
+
@click.option(
|
|
374
|
+
"--subpath",
|
|
375
|
+
"-s",
|
|
376
|
+
type=click.STRING,
|
|
377
|
+
required=False,
|
|
378
|
+
help="Subpath within the package structure, e.g. if arrangement is stored in a subdirectory",
|
|
379
|
+
)
|
|
380
|
+
@click.option(
|
|
381
|
+
"--verbose",
|
|
382
|
+
"-v",
|
|
383
|
+
is_flag=True,
|
|
384
|
+
help="Show detailed information during verification",
|
|
385
|
+
)
|
|
386
|
+
@click.pass_context
|
|
387
|
+
def verify_package(ctx, declaration, package, arrangement_id, subpath, verbose):
|
|
388
|
+
if package is None:
|
|
389
|
+
# A single argument is the package; the declaration comes from
|
|
390
|
+
# --declaration.
|
|
391
|
+
declaration, package = None, declaration
|
|
392
|
+
if package is None:
|
|
393
|
+
raise click.MissingParameter(
|
|
394
|
+
ctx=ctx, param_hint="'PACKAGE'", param_type="argument"
|
|
395
|
+
)
|
|
396
|
+
declaration = _resolve_declaration(ctx, declaration)
|
|
397
|
+
subpath = subpath if subpath else None
|
|
398
|
+
tro = TRO(
|
|
399
|
+
filepath=declaration,
|
|
400
|
+
)
|
|
401
|
+
if not arrangement_id:
|
|
402
|
+
arrangements = [a["@id"] for a in tro.list_arrangements()]
|
|
403
|
+
else:
|
|
404
|
+
arrangements = [arrangement_id]
|
|
405
|
+
|
|
406
|
+
for arrangement in arrangements:
|
|
407
|
+
msg = (
|
|
408
|
+
f"Verifying that arrangement '{arrangement}' matches package contents "
|
|
409
|
+
f"of '{package}"
|
|
410
|
+
)
|
|
411
|
+
if subpath:
|
|
412
|
+
msg += f"::{subpath}"
|
|
413
|
+
msg += "'"
|
|
414
|
+
click.echo(msg, nl=False)
|
|
415
|
+
missing, mismatched, extra, success = tro.verify_replication_package(
|
|
416
|
+
arrangement, package, subpath
|
|
417
|
+
)
|
|
418
|
+
if success:
|
|
419
|
+
click.secho(" ✓", fg="green")
|
|
420
|
+
else:
|
|
421
|
+
click.secho(" ✗", fg="red")
|
|
422
|
+
|
|
423
|
+
if extra and verbose:
|
|
424
|
+
table = Table(
|
|
425
|
+
title="[bold red]Extra Files Found[/bold red]", show_header=False
|
|
426
|
+
)
|
|
427
|
+
table.add_column("File", style="yellow")
|
|
428
|
+
for e in extra:
|
|
429
|
+
table.add_row(e)
|
|
430
|
+
console.print(table)
|
|
431
|
+
console.print()
|
|
432
|
+
|
|
433
|
+
if mismatched and verbose:
|
|
434
|
+
table = Table(title="[bold red]Mismatched Files Found[/bold red]")
|
|
435
|
+
table.add_column("File", style="cyan")
|
|
436
|
+
table.add_column("Expected Hash", style="green")
|
|
437
|
+
table.add_column("Actual Hash", style="red")
|
|
438
|
+
for filepath, expected_hash, actual_hash in mismatched:
|
|
439
|
+
expected_display = (
|
|
440
|
+
expected_hash[:16] + "..." if expected_hash else "[dim]None[/dim]"
|
|
441
|
+
)
|
|
442
|
+
actual_display = (
|
|
443
|
+
actual_hash[:16] + "..." if actual_hash else "[dim]None[/dim]"
|
|
444
|
+
)
|
|
445
|
+
table.add_row(filepath, expected_display, actual_display)
|
|
446
|
+
console.print(table)
|
|
447
|
+
console.print()
|
|
448
|
+
|
|
449
|
+
if missing and verbose:
|
|
450
|
+
table = Table(
|
|
451
|
+
title="[bold red]Missing Files Found[/bold red]", show_header=False
|
|
452
|
+
)
|
|
453
|
+
table.add_column("File", style="magenta")
|
|
454
|
+
for m in missing:
|
|
455
|
+
table.add_row(m)
|
|
456
|
+
console.print(table)
|
|
457
|
+
console.print()
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
@cli.group(help="Manage arrangements in the TRO")
|
|
461
|
+
def arrangement():
|
|
462
|
+
pass
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
@cli.group(help="Manage compositions in the TRO")
|
|
466
|
+
def composition():
|
|
467
|
+
pass
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
@cli.group(help="Manage performances in the TRO")
|
|
471
|
+
def performance():
|
|
472
|
+
pass
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
@composition.command(help="Get info about current composition")
|
|
476
|
+
@click.option("--verbose", "-v", is_flag=True, help="Show detailed information")
|
|
477
|
+
@click.pass_context
|
|
478
|
+
def info(ctx, verbose):
|
|
479
|
+
tro = _load_existing_tro(ctx.parent.parent.params.get("declaration"))
|
|
480
|
+
artifact_locs: dict = {}
|
|
481
|
+
if verbose:
|
|
482
|
+
for arr in tro._model.arrangements:
|
|
483
|
+
for loc in arr.locations:
|
|
484
|
+
artifact_locs.setdefault(loc.artifact_id, []).append(
|
|
485
|
+
{"@id": loc.location_id, "path": loc.path}
|
|
486
|
+
)
|
|
487
|
+
|
|
488
|
+
for artifact in tro._model.composition.artifacts:
|
|
489
|
+
print(artifact.artifact_id)
|
|
490
|
+
print(f" - mimeType: {artifact.mime_type}")
|
|
491
|
+
print(f" - {artifact.hash}")
|
|
492
|
+
if verbose:
|
|
493
|
+
print(" - Arrangements:")
|
|
494
|
+
for a in artifact_locs.get(artifact.artifact_id, []):
|
|
495
|
+
print(f" - {a['path']} (id={a['@id']})")
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
@arrangement.command(
|
|
499
|
+
help="Compute an arrangement snapshot from a directory and save it to a file"
|
|
500
|
+
)
|
|
501
|
+
@click.option("--comment", "-m", type=click.STRING, required=False)
|
|
502
|
+
@click.option("--ignore_dir", "-i", type=click.STRING, required=False, multiple=True)
|
|
503
|
+
@click.option(
|
|
504
|
+
"--output",
|
|
505
|
+
"-o",
|
|
506
|
+
type=click.Path(),
|
|
507
|
+
required=True,
|
|
508
|
+
help="Output snapshot file (.jsonld)",
|
|
509
|
+
)
|
|
510
|
+
@click.argument("directory", type=click.Path(exists=True))
|
|
511
|
+
def snapshot(directory, ignore_dir, comment, output):
|
|
512
|
+
composition = ArtifactComposition()
|
|
513
|
+
arr = ArtifactArrangement.from_directory(
|
|
514
|
+
directory=directory,
|
|
515
|
+
composition=composition,
|
|
516
|
+
arrangement_id="arrangement/0",
|
|
517
|
+
comment=comment,
|
|
518
|
+
ignore_dirs=list(ignore_dir) or None,
|
|
519
|
+
)
|
|
520
|
+
arr.save_snapshot(output, composition)
|
|
521
|
+
click.echo(f"Snapshot saved to {output} ({len(arr.locations)} file(s))")
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
@arrangement.command(
|
|
525
|
+
help="Add an arrangement to the TRO from a directory or a pre-computed snapshot"
|
|
526
|
+
)
|
|
527
|
+
@click.option("--comment", "-m", type=click.STRING, required=False)
|
|
528
|
+
@click.option("--ignore_dir", "-i", type=click.STRING, required=False, multiple=True)
|
|
529
|
+
@click.option(
|
|
530
|
+
"--from-snapshot",
|
|
531
|
+
type=click.Path(exists=True),
|
|
532
|
+
required=False,
|
|
533
|
+
help="Load arrangement from a pre-computed snapshot file instead of scanning a directory",
|
|
534
|
+
)
|
|
535
|
+
@click.argument("directory", type=click.Path(exists=True), required=False)
|
|
536
|
+
@click.pass_context
|
|
537
|
+
def add(ctx, directory, ignore_dir, comment, from_snapshot):
|
|
538
|
+
if from_snapshot is None and directory is None:
|
|
539
|
+
raise click.UsageError("Provide either DIRECTORY or --from-snapshot.")
|
|
540
|
+
if from_snapshot is not None and directory is not None:
|
|
541
|
+
raise click.UsageError("DIRECTORY and --from-snapshot are mutually exclusive.")
|
|
542
|
+
ctx = ctx.parent.parent
|
|
543
|
+
declaration = ctx.params.get("declaration")
|
|
544
|
+
gpg_fingerprint = ctx.params.get("gpg_fingerprint")
|
|
545
|
+
gpg_passphrase = ctx.params.get("gpg_passphrase")
|
|
546
|
+
profile = ctx.params.get("profile")
|
|
547
|
+
tro_name = ctx.params.get("tro_name")
|
|
548
|
+
tro_description = ctx.params.get("tro_description")
|
|
549
|
+
tro_creator = ctx.params.get("tro_creator")
|
|
550
|
+
tro_creator_type = ctx.params.get("tro_creator_type")
|
|
551
|
+
extra_context = ctx.params.get("extra_context") or []
|
|
552
|
+
tro = TRO(
|
|
553
|
+
filepath=declaration,
|
|
554
|
+
gpg_fingerprint=gpg_fingerprint,
|
|
555
|
+
gpg_passphrase=gpg_passphrase,
|
|
556
|
+
profile=profile,
|
|
557
|
+
tro_creator=tro_creator,
|
|
558
|
+
tro_creator_type=tro_creator_type,
|
|
559
|
+
tro_name=tro_name,
|
|
560
|
+
tro_description=tro_description,
|
|
561
|
+
extra_context=extra_context or None,
|
|
562
|
+
)
|
|
563
|
+
if from_snapshot:
|
|
564
|
+
tro.add_arrangement_from_snapshot(from_snapshot, comment=comment)
|
|
565
|
+
else:
|
|
566
|
+
tro.add_arrangement(directory, ignore_dirs=ignore_dir, comment=comment)
|
|
567
|
+
tro.save()
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def _load_existing_tro(declaration, **kwargs):
|
|
571
|
+
"""Load the declaration a command needs, refusing to invent an empty one."""
|
|
572
|
+
if declaration is None:
|
|
573
|
+
raise click.UsageError(
|
|
574
|
+
"--declaration is required. It is a global option, so it goes "
|
|
575
|
+
"before the command: tro-utils --declaration FILE COMMAND ..."
|
|
576
|
+
)
|
|
577
|
+
tro = TRO(filepath=declaration, **kwargs)
|
|
578
|
+
if not pathlib.Path(tro.tro_filename).exists():
|
|
579
|
+
raise click.ClickException(
|
|
580
|
+
f"Declaration file '{tro.tro_filename}' does not exist."
|
|
581
|
+
)
|
|
582
|
+
return tro
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
@arrangement.command(name="list", help="List available arrangements in the TRO")
|
|
586
|
+
@click.option("--verbose", "-v", is_flag=True, help="Show detailed information")
|
|
587
|
+
@click.pass_context
|
|
588
|
+
def list_arrangement(ctx, verbose):
|
|
589
|
+
tro = _load_existing_tro(ctx.parent.parent.params.get("declaration"))
|
|
590
|
+
for a in tro.list_arrangements():
|
|
591
|
+
print(f"Arrangement(id={a['@id']}): {a['rdfs:comment']}")
|
|
592
|
+
if verbose:
|
|
593
|
+
print(" - Composition:")
|
|
594
|
+
for c in a["trov:hasArtifactLocation"]:
|
|
595
|
+
print(f" - {c['trov:path']}")
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
@cli.command(help="Sign the TRO")
|
|
599
|
+
@click.pass_context
|
|
600
|
+
def sign(ctx):
|
|
601
|
+
declaration = ctx.parent.params.get("declaration")
|
|
602
|
+
gpg_fingerprint = ctx.parent.params.get("gpg_fingerprint")
|
|
603
|
+
gpg_passphrase = ctx.parent.params.get("gpg_passphrase")
|
|
604
|
+
profile = ctx.parent.params.get("profile")
|
|
605
|
+
tro = _load_existing_tro(
|
|
606
|
+
declaration,
|
|
607
|
+
gpg_fingerprint=gpg_fingerprint,
|
|
608
|
+
gpg_passphrase=gpg_passphrase,
|
|
609
|
+
profile=profile,
|
|
610
|
+
)
|
|
611
|
+
tro.request_timestamp()
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
@cli.command(help="Generate a report of the TRO", name="report")
|
|
615
|
+
@click.option(
|
|
616
|
+
"--template",
|
|
617
|
+
"-t",
|
|
618
|
+
type=StringOrPath(_TEMPLATES),
|
|
619
|
+
required=True,
|
|
620
|
+
help=f"Template file or one of the following: {', '.join(_TEMPLATES.keys())}",
|
|
621
|
+
)
|
|
622
|
+
@click.option(
|
|
623
|
+
"--output", "-o", type=click.Path(), required=True, help="Output file in Markdown"
|
|
624
|
+
)
|
|
625
|
+
@click.pass_context
|
|
626
|
+
def generate_report(ctx, template, output):
|
|
627
|
+
declaration = ctx.parent.params.get("declaration")
|
|
628
|
+
if template in _TEMPLATES:
|
|
629
|
+
template = pathlib.Path(__file__).parent / _TEMPLATES[template]["filename"]
|
|
630
|
+
template = str(template.resolve())
|
|
631
|
+
tro = _load_existing_tro(declaration)
|
|
632
|
+
tro.generate_report(template, output)
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
@performance.command(help="Add performance to the TRO", name="add")
|
|
636
|
+
@click.option(
|
|
637
|
+
"--comment",
|
|
638
|
+
"-m",
|
|
639
|
+
type=click.STRING,
|
|
640
|
+
required=False,
|
|
641
|
+
help="Description of the performance",
|
|
642
|
+
)
|
|
643
|
+
@click.option(
|
|
644
|
+
"--start",
|
|
645
|
+
"-s",
|
|
646
|
+
type=click.DateTime(formats=_DATETIME_FORMATS),
|
|
647
|
+
required=False,
|
|
648
|
+
help="Start time of the performance (local time assumed if no UTC offset given)",
|
|
649
|
+
)
|
|
650
|
+
@click.option(
|
|
651
|
+
"--end",
|
|
652
|
+
"-e",
|
|
653
|
+
type=click.DateTime(formats=_DATETIME_FORMATS),
|
|
654
|
+
required=False,
|
|
655
|
+
help="End time of the performance (local time assumed if no UTC offset given)",
|
|
656
|
+
)
|
|
657
|
+
@click.option(
|
|
658
|
+
"--attribute",
|
|
659
|
+
"-a",
|
|
660
|
+
type=click.Choice([cap.value for cap in TRPAttribute]),
|
|
661
|
+
required=False,
|
|
662
|
+
multiple=True,
|
|
663
|
+
help="Capabilities of the performance",
|
|
664
|
+
)
|
|
665
|
+
@click.option(
|
|
666
|
+
"--accessed",
|
|
667
|
+
"-A",
|
|
668
|
+
type=click.STRING,
|
|
669
|
+
required=False,
|
|
670
|
+
multiple=True,
|
|
671
|
+
help="Accessed arrangement: ARRANGEMENT_ID or ARRANGEMENT_ID:MOUNT_PATH. May be repeated.",
|
|
672
|
+
)
|
|
673
|
+
@click.option(
|
|
674
|
+
"--modified",
|
|
675
|
+
"-M",
|
|
676
|
+
type=click.STRING,
|
|
677
|
+
required=False,
|
|
678
|
+
multiple=True,
|
|
679
|
+
help="Modified arrangement: ARRANGEMENT_ID or ARRANGEMENT_ID:MOUNT_PATH. May be repeated.",
|
|
680
|
+
)
|
|
681
|
+
@click.pass_context
|
|
682
|
+
def performance_add(ctx, comment, start, end, attribute, accessed, modified):
|
|
683
|
+
ctx = ctx.parent.parent
|
|
684
|
+
declaration = ctx.params.get("declaration")
|
|
685
|
+
gpg_fingerprint = ctx.params.get("gpg_fingerprint")
|
|
686
|
+
gpg_passphrase = ctx.params.get("gpg_passphrase")
|
|
687
|
+
profile = ctx.params.get("profile")
|
|
688
|
+
extra_context = ctx.params.get("extra_context") or []
|
|
689
|
+
tro = TRO(
|
|
690
|
+
filepath=declaration,
|
|
691
|
+
gpg_fingerprint=gpg_fingerprint,
|
|
692
|
+
gpg_passphrase=gpg_passphrase,
|
|
693
|
+
profile=profile,
|
|
694
|
+
extra_context=extra_context or None,
|
|
695
|
+
)
|
|
696
|
+
tro.add_performance(
|
|
697
|
+
start,
|
|
698
|
+
end,
|
|
699
|
+
comment=comment,
|
|
700
|
+
accessed_arrangement=[_parse_arrangement_ref(v) for v in accessed] or None,
|
|
701
|
+
modified_arrangement=[_parse_arrangement_ref(v) for v in modified] or None,
|
|
702
|
+
attrs=attribute,
|
|
703
|
+
)
|
|
704
|
+
tro.save()
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
@performance.command(name="list", help="List performances recorded in the TRO")
|
|
708
|
+
@click.option("--verbose", "-v", is_flag=True, help="Show detailed information")
|
|
709
|
+
@click.pass_context
|
|
710
|
+
def list_performance(ctx, verbose):
|
|
711
|
+
tro = _load_existing_tro(ctx.parent.parent.params.get("declaration"))
|
|
712
|
+
for p in tro.list_performances():
|
|
713
|
+
print(f"Performance(id={p['@id']}): {p.get('rdfs:comment', '')}")
|
|
714
|
+
if not verbose:
|
|
715
|
+
continue
|
|
716
|
+
print(f" - Started: {p.get('trov:startedAtTime', 'n/a')}")
|
|
717
|
+
print(f" - Ended: {p.get('trov:endedAtTime', 'n/a')}")
|
|
718
|
+
for label, key in (
|
|
719
|
+
("Accessed", "trov:accessedArrangement"),
|
|
720
|
+
("Contributed to", "trov:contributedToArrangement"),
|
|
721
|
+
):
|
|
722
|
+
bindings = p.get(key, [])
|
|
723
|
+
if not bindings:
|
|
724
|
+
continue
|
|
725
|
+
print(f" - {label}:")
|
|
726
|
+
for b in bindings:
|
|
727
|
+
path = b.get("trov:boundTo")
|
|
728
|
+
suffix = f" (bound to {path})" if path else ""
|
|
729
|
+
print(f" - {b['trov:arrangement']['@id']}{suffix}")
|
|
730
|
+
attributes = p["trov:hasPerformanceAttribute"]
|
|
731
|
+
if attributes:
|
|
732
|
+
print(" - Attributes:")
|
|
733
|
+
for attr in attributes:
|
|
734
|
+
print(f" - {attr['@type']}")
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
if __name__ == "__main__":
|
|
738
|
+
sys.exit(cli()) # pragma: no cover
|