stepup-data 4.3.0a0__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.
- stepup/data/__init__.py +12 -0
- stepup/data/api.py +244 -0
- stepup/data/check_inventory.py +54 -0
- stepup/data/env_vars.py +19 -0
- stepup/data/inventory.py +515 -0
- stepup/data/make_inventory.py +290 -0
- stepup/data/pytest.py +73 -0
- stepup/data/sync_zenodo.py +2482 -0
- stepup/data/zenodo_vocabularies.yaml +9089 -0
- stepup/data/zip_inventory.py +124 -0
- stepup_data-4.3.0a0.dist-info/METADATA +93 -0
- stepup_data-4.3.0a0.dist-info/RECORD +16 -0
- stepup_data-4.3.0a0.dist-info/WHEEL +5 -0
- stepup_data-4.3.0a0.dist-info/entry_points.txt +8 -0
- stepup_data-4.3.0a0.dist-info/licenses/LICENSE +304 -0
- stepup_data-4.3.0a0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2024 Toon Verstraelen <Toon.Verstraelen@UGent.be>
|
|
2
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
3
|
+
"""Create an inventory file.
|
|
4
|
+
|
|
5
|
+
An inventory describes every individual file, with its size and optionally its digest,
|
|
6
|
+
in the format defined in `stepup.data.inventory`.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import sys
|
|
11
|
+
from collections.abc import Sequence
|
|
12
|
+
|
|
13
|
+
from stepup.core.api import amend, glob
|
|
14
|
+
from stepup.core.nglob import NamedGlob, has_any_wildcards
|
|
15
|
+
from stepup.core.path import Path, StrPath
|
|
16
|
+
from stepup.core.platform.oscompat import file_modes_available
|
|
17
|
+
from stepup.core.stepinfo import StepInfo, load_step_info
|
|
18
|
+
|
|
19
|
+
from .inventory import (
|
|
20
|
+
HASH_ALGORITHMS,
|
|
21
|
+
InventoryHeader,
|
|
22
|
+
compute_record,
|
|
23
|
+
format_header,
|
|
24
|
+
format_record,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
__all__ = ("NO_HASH", "main", "write_inventory")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
NO_HASH = "none"
|
|
31
|
+
"""The value of the option `--hash` that leaves out the digests."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def main(argv: Sequence[str] | None = None):
|
|
35
|
+
"""Main program."""
|
|
36
|
+
parser = argparse.ArgumentParser(
|
|
37
|
+
prog="sd-make-inventory",
|
|
38
|
+
description="Make an inventory file in JSON Lines format. "
|
|
39
|
+
"All paths and patterns are relative to the current directory. "
|
|
40
|
+
"Quote the patterns, so that the shell does not expand them.",
|
|
41
|
+
)
|
|
42
|
+
parser.add_argument(
|
|
43
|
+
"paths",
|
|
44
|
+
nargs="*",
|
|
45
|
+
help="Files to include in the inventory, in the given order. "
|
|
46
|
+
"They are never interpreted as patterns.",
|
|
47
|
+
)
|
|
48
|
+
parser.add_argument(
|
|
49
|
+
"--include",
|
|
50
|
+
action="append",
|
|
51
|
+
default=[],
|
|
52
|
+
metavar="PATTERN",
|
|
53
|
+
help="A pattern of files to include, or a literal path when it has no wildcards. "
|
|
54
|
+
"The included files come after the positional paths, sorted. "
|
|
55
|
+
"A pattern that matches no files is an error. "
|
|
56
|
+
"This option can be repeated.",
|
|
57
|
+
)
|
|
58
|
+
parser.add_argument(
|
|
59
|
+
"--include-info-inp",
|
|
60
|
+
action="append",
|
|
61
|
+
default=[],
|
|
62
|
+
metavar="INFO",
|
|
63
|
+
help="A step info file, whose input paths are included. "
|
|
64
|
+
"The included files come after the positional paths, sorted. "
|
|
65
|
+
"This option can be repeated.",
|
|
66
|
+
)
|
|
67
|
+
parser.add_argument(
|
|
68
|
+
"--include-info-out",
|
|
69
|
+
action="append",
|
|
70
|
+
default=[],
|
|
71
|
+
metavar="INFO",
|
|
72
|
+
help="A step info file, whose output paths are included. "
|
|
73
|
+
"The included files come after the positional paths, sorted. "
|
|
74
|
+
"This option can be repeated.",
|
|
75
|
+
)
|
|
76
|
+
parser.add_argument(
|
|
77
|
+
"--exclude",
|
|
78
|
+
action="append",
|
|
79
|
+
default=[],
|
|
80
|
+
metavar="PATTERN",
|
|
81
|
+
help="A pattern or a path of files to leave out, "
|
|
82
|
+
"whether they were given as positional paths or with one of the --include options. "
|
|
83
|
+
"It is matched against the names of the selected files, not against the disk. "
|
|
84
|
+
"This option can be repeated.",
|
|
85
|
+
)
|
|
86
|
+
parser.add_argument(
|
|
87
|
+
"-o",
|
|
88
|
+
"--output",
|
|
89
|
+
required=True,
|
|
90
|
+
help="The inventory file to write, with suffix .jsonl.",
|
|
91
|
+
)
|
|
92
|
+
parser.add_argument(
|
|
93
|
+
"--hash",
|
|
94
|
+
dest="hash_algorithm",
|
|
95
|
+
choices=[*HASH_ALGORITHMS, NO_HASH],
|
|
96
|
+
default="md5",
|
|
97
|
+
help="The hash algorithm for the digests of the files. "
|
|
98
|
+
f"With {NO_HASH}, no digests are recorded and the contents of the files are not read. "
|
|
99
|
+
"[default=%(default)s]",
|
|
100
|
+
)
|
|
101
|
+
parser.add_argument(
|
|
102
|
+
"--mode",
|
|
103
|
+
default=False,
|
|
104
|
+
action="store_true",
|
|
105
|
+
help="Also record the permission bits of the files.",
|
|
106
|
+
)
|
|
107
|
+
args = parser.parse_args(argv)
|
|
108
|
+
if not (args.paths or args.include or args.include_info_inp or args.include_info_out):
|
|
109
|
+
parser.error(
|
|
110
|
+
"At least one path, --include, --include-info-inp or --include-info-out is needed."
|
|
111
|
+
)
|
|
112
|
+
paths = select_paths(
|
|
113
|
+
args.paths,
|
|
114
|
+
args.include,
|
|
115
|
+
args.exclude,
|
|
116
|
+
args.output,
|
|
117
|
+
include_info_inp=args.include_info_inp,
|
|
118
|
+
include_info_out=args.include_info_out,
|
|
119
|
+
)
|
|
120
|
+
hash_algorithm = None if args.hash_algorithm == NO_HASH else args.hash_algorithm
|
|
121
|
+
write_inventory(args.output, paths, hash_algorithm=hash_algorithm, mode=args.mode)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def select_paths(
|
|
125
|
+
paths: Sequence[StrPath],
|
|
126
|
+
include: Sequence[str],
|
|
127
|
+
exclude: Sequence[str],
|
|
128
|
+
path_out: StrPath,
|
|
129
|
+
*,
|
|
130
|
+
include_info_inp: Sequence[StrPath] = (),
|
|
131
|
+
include_info_out: Sequence[StrPath] = (),
|
|
132
|
+
) -> list[Path]:
|
|
133
|
+
"""Select the files of an inventory.
|
|
134
|
+
|
|
135
|
+
The patterns in `include` are matched with `stepup.core.api.glob()`,
|
|
136
|
+
so that the current step becomes pending when their matches change.
|
|
137
|
+
Like any `glob()` match, a matched file must be static.
|
|
138
|
+
The step info files are amended as inputs of the current step before they are read,
|
|
139
|
+
so that the step reruns when the lists of files in them change.
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
paths
|
|
144
|
+
Literal paths, which come first, in the given order.
|
|
145
|
+
include
|
|
146
|
+
Patterns, or literal paths when they have no wildcards.
|
|
147
|
+
The matches and the literal paths come after `paths`, sorted.
|
|
148
|
+
exclude
|
|
149
|
+
Patterns or literal paths, matched against the normalized names of the selected files.
|
|
150
|
+
Excluded files are removed, whichever argument selected them.
|
|
151
|
+
path_out
|
|
152
|
+
The inventory to be written, which must not be selected.
|
|
153
|
+
include_info_inp, include_info_out
|
|
154
|
+
Step info files, as written by `stepup.core.stepinfo.dump_step_info()`,
|
|
155
|
+
whose `inp` or `out` paths, respectively, are included.
|
|
156
|
+
These paths are sorted together with those of `include`.
|
|
157
|
+
|
|
158
|
+
Returns
|
|
159
|
+
-------
|
|
160
|
+
selected
|
|
161
|
+
The normalized paths of the selected files.
|
|
162
|
+
These may still contain duplicates relative to the directory of the inventory.
|
|
163
|
+
|
|
164
|
+
Raises
|
|
165
|
+
------
|
|
166
|
+
ValueError
|
|
167
|
+
When an include pattern matches no files, or when the inventory itself is selected.
|
|
168
|
+
"""
|
|
169
|
+
selected = [Path(path).normpath() for path in paths]
|
|
170
|
+
included = set()
|
|
171
|
+
for entry in include:
|
|
172
|
+
if has_any_wildcards(entry):
|
|
173
|
+
matches = [path for path in glob(entry).files() if not path.endswith("/")]
|
|
174
|
+
if len(matches) == 0:
|
|
175
|
+
raise ValueError(f"The include pattern {entry} matches no files.")
|
|
176
|
+
included.update(path.normpath() for path in matches)
|
|
177
|
+
else:
|
|
178
|
+
included.add(Path(entry).normpath())
|
|
179
|
+
paths_info = sorted({Path(path).normpath() for path in [*include_info_inp, *include_info_out]})
|
|
180
|
+
if len(paths_info) > 0:
|
|
181
|
+
amend(inp=paths_info)
|
|
182
|
+
for path_info in include_info_inp:
|
|
183
|
+
included.update(read_step_info_paths(path_info, "inp"))
|
|
184
|
+
for path_info in include_info_out:
|
|
185
|
+
included.update(read_step_info_paths(path_info, "out"))
|
|
186
|
+
selected.extend(sorted(included))
|
|
187
|
+
|
|
188
|
+
excluded = set()
|
|
189
|
+
for pattern in exclude:
|
|
190
|
+
ng = NamedGlob(pattern)
|
|
191
|
+
ng.extend(selected)
|
|
192
|
+
excluded.update(ng.files())
|
|
193
|
+
selected = [path for path in selected if path not in excluded]
|
|
194
|
+
|
|
195
|
+
if Path(path_out).normpath() in selected:
|
|
196
|
+
raise ValueError(f"The inventory {path_out} cannot list itself.")
|
|
197
|
+
return selected
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def read_step_info_paths(path_info: StrPath, field: str) -> list[Path]:
|
|
201
|
+
"""Read the paths of one field of the step infos in a file.
|
|
202
|
+
|
|
203
|
+
Parameters
|
|
204
|
+
----------
|
|
205
|
+
path_info
|
|
206
|
+
A file with one step info or a list of them.
|
|
207
|
+
field
|
|
208
|
+
The field whose paths are read: `inp` or `out`.
|
|
209
|
+
|
|
210
|
+
Returns
|
|
211
|
+
-------
|
|
212
|
+
paths
|
|
213
|
+
The normalized paths, relative to the current directory unless absolute.
|
|
214
|
+
"""
|
|
215
|
+
if field not in ("inp", "out"):
|
|
216
|
+
raise ValueError(f"Only the fields inp and out can be read, got {field}")
|
|
217
|
+
infos = load_step_info(path_info)
|
|
218
|
+
if isinstance(infos, StepInfo):
|
|
219
|
+
infos = [infos]
|
|
220
|
+
return [path for info in infos for path in getattr(info.rebase("."), field)]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def write_inventory(
|
|
224
|
+
path_out: StrPath,
|
|
225
|
+
paths: Sequence[StrPath],
|
|
226
|
+
*,
|
|
227
|
+
hash_algorithm: str | None = "md5",
|
|
228
|
+
mode: bool = False,
|
|
229
|
+
do_amend: bool = True,
|
|
230
|
+
):
|
|
231
|
+
"""Write an inventory file.
|
|
232
|
+
|
|
233
|
+
Parameters
|
|
234
|
+
----------
|
|
235
|
+
path_out
|
|
236
|
+
The location of the inventory file to be written, with suffix `.jsonl`.
|
|
237
|
+
paths
|
|
238
|
+
The paths to be included in the inventory, in the order of the records.
|
|
239
|
+
These must be paths relative to the current working directory.
|
|
240
|
+
They will be written to the inventory file
|
|
241
|
+
as paths relative to the parent of the inventory file.
|
|
242
|
+
When two of them are the same after this conversion, only the first one is kept.
|
|
243
|
+
hash_algorithm
|
|
244
|
+
The name of the `hashlib` algorithm that digests the regular files,
|
|
245
|
+
or `None` to record no digests.
|
|
246
|
+
mode
|
|
247
|
+
When True, the permission bits of the regular files are recorded.
|
|
248
|
+
A warning is printed on a platform where files have no permission bits.
|
|
249
|
+
do_amend
|
|
250
|
+
When True, `paths` are amended as inputs of the current step,
|
|
251
|
+
so that the inventory is rebuilt when they change.
|
|
252
|
+
Set this to False when the step already declares these files itself,
|
|
253
|
+
because the extra amendment may then introduce a cyclic dependency.
|
|
254
|
+
"""
|
|
255
|
+
path_out = Path(path_out)
|
|
256
|
+
if not path_out.endswith(".jsonl"):
|
|
257
|
+
raise ValueError(f"The inventory file must have a `.jsonl` extension. Got {path_out}")
|
|
258
|
+
header = InventoryHeader(hash_algorithm, mode)
|
|
259
|
+
if mode and not file_modes_available():
|
|
260
|
+
print(
|
|
261
|
+
"WARNING: Files have no permission bits on this platform. "
|
|
262
|
+
"The recorded modes are derived from the read-only attribute and the file name.",
|
|
263
|
+
file=sys.stderr,
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
root = path_out.parent.normpath()
|
|
267
|
+
unique_paths = []
|
|
268
|
+
seen = set()
|
|
269
|
+
for path in paths:
|
|
270
|
+
path = Path(path)
|
|
271
|
+
relpath = path.relpath(root).normpath()
|
|
272
|
+
if relpath not in seen:
|
|
273
|
+
seen.add(relpath)
|
|
274
|
+
unique_paths.append(path)
|
|
275
|
+
|
|
276
|
+
if do_amend:
|
|
277
|
+
for path in unique_paths:
|
|
278
|
+
if path.endswith("/") or path.is_dir():
|
|
279
|
+
raise ValueError(f"Directories are not allowed in the inventory: {path}")
|
|
280
|
+
amend(inp=unique_paths)
|
|
281
|
+
|
|
282
|
+
with open(path_out, "w", encoding="utf-8", newline="\n") as fh:
|
|
283
|
+
print(format_header(header), file=fh)
|
|
284
|
+
for path in unique_paths:
|
|
285
|
+
record = compute_record(path, root, hash_algorithm, mode)
|
|
286
|
+
print(format_record(record, header), file=fh)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
if __name__ == "__main__":
|
|
290
|
+
sys.exit(main())
|
stepup/data/pytest.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2024 Toon Verstraelen <Toon.Verstraelen@UGent.be>
|
|
2
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
3
|
+
"""Utilities for testing with pytest."""
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
|
|
8
|
+
from stepup.core.path import Path
|
|
9
|
+
from stepup.core.platform.oscompat import file_modes_available
|
|
10
|
+
from stepup.core.pytest import OUTPUT_RULES as CORE_OUTPUT_RULES
|
|
11
|
+
from stepup.core.pytest import OutputRule
|
|
12
|
+
from stepup.core.pytest import run_example as run_example_core
|
|
13
|
+
from stepup.data.inventory import FileRecord, open_inventory
|
|
14
|
+
|
|
15
|
+
__all__ = ("INVENTORY_OUTPUT", "OUTPUT_RULES", "run_example")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
INVENTORY_MODE = re.compile(r'"mode":"[^"]{9}"')
|
|
19
|
+
"""The permission bits of a file in a record of an inventory."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def mask_inventory_modes(text: str) -> str:
|
|
23
|
+
"""Hide the permission bits of the files in the records of an inventory.
|
|
24
|
+
|
|
25
|
+
`mask_file_modes` in `stepup.core.pytest` does the same for the output of StepUp.
|
|
26
|
+
"""
|
|
27
|
+
return INVENTORY_MODE.sub('"mode":"*********"', text)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def mask_inventory_output(text: str) -> str:
|
|
31
|
+
"""Hide the parts of an inventory that may differ in this environment.
|
|
32
|
+
|
|
33
|
+
On a platform without permission bits of files,
|
|
34
|
+
the permission bits are hidden, see `mask_inventory_modes`.
|
|
35
|
+
"""
|
|
36
|
+
return text if file_modes_available() else mask_inventory_modes(text)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
INVENTORY_OUTPUT = OutputRule("_*inventory.jsonl", mask=mask_inventory_output)
|
|
40
|
+
"""The rule for an inventory file, as written by `make_inventory()`."""
|
|
41
|
+
|
|
42
|
+
OUTPUT_RULES = (INVENTORY_OUTPUT, *CORE_OUTPUT_RULES)
|
|
43
|
+
"""The rules that `run_example` applies by default, those of StepUp Core included."""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async def run_example(
|
|
47
|
+
srcdir: Path,
|
|
48
|
+
tmpdir: Path,
|
|
49
|
+
overwrite_expected: bool = False,
|
|
50
|
+
*,
|
|
51
|
+
rules: Sequence[OutputRule] = OUTPUT_RULES,
|
|
52
|
+
):
|
|
53
|
+
"""Run an example use case in a temporary directory and check the outputs.
|
|
54
|
+
|
|
55
|
+
See `stepup.core.pytest.run_example` for details and for the parameters.
|
|
56
|
+
After the comparison, all regular files listed in an inventory
|
|
57
|
+
`reproducibility_*inventory.jsonl` in the example must have the same size and digest.
|
|
58
|
+
"""
|
|
59
|
+
await run_example_core(Path(srcdir), Path(tmpdir), overwrite_expected, rules=rules)
|
|
60
|
+
|
|
61
|
+
# Reproducibility check
|
|
62
|
+
workdir = Path(tmpdir) / "example"
|
|
63
|
+
for path_inventory in sorted(workdir.glob("reproducibility_*inventory.jsonl")):
|
|
64
|
+
with open_inventory(path_inventory) as reader:
|
|
65
|
+
if reader.header.hash_algorithm is None:
|
|
66
|
+
raise AssertionError(f"The inventory {path_inventory} has no digests.")
|
|
67
|
+
records = [record for record in reader if isinstance(record, FileRecord)]
|
|
68
|
+
sizes = {record.size for record in records}
|
|
69
|
+
if len(sizes) != 1:
|
|
70
|
+
raise AssertionError(f"Not all file sizes in {path_inventory} are the same.")
|
|
71
|
+
digests = {record.digest for record in records}
|
|
72
|
+
if len(digests) != 1:
|
|
73
|
+
raise AssertionError(f"Not all file digests in {path_inventory} are the same.")
|