]>
dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/prepare-cxml.py
2 """Apply the versioned XML encoding patches to verified Nix sources, offline."""
6 from pathlib
import Path
10 ROOT
= Path(__file__
).resolve().parent
.parent
11 NIX_EVAL
= ['nix', 'eval', '--offline', '--option', 'eval-cache', 'false']
12 PATCHES
= {'closure-common': ['encodings.lisp', 'xstream.lisp'],
13 'cxml': ['xml/xml-parse.lisp']}
17 return subprocess
.check_output(args
, cwd
=ROOT
, text
=True).strip()
20 def source_environment():
21 system
= command(NIX_EVAL
+ ['--impure', '--raw', '--expr', 'builtins.currentSystem'])
22 return system
, json
.loads(command(NIX_EVAL
+ ['--json', '.#lib.cxmlEnvironment',
23 '--apply', 'f: f "' + system
+ '"']))
27 return hashlib
.sha256(path
.read_bytes()).hexdigest()
31 return '(:source-registry ' + ' '.join('(:tree ' + json
.dumps(str(p
) + '/') + ')'
32 for p
in paths
) + ' :ignore-inherited-configuration)'
35 def prepare(environment
, target
):
36 target
= Path(target
).resolve()
37 # Verify every pristine input before creating or changing an output.
38 for package
in environment
['manifest']:
39 source
= Path(package
['source'])
40 assert source
.is_dir(), 'Missing local source: ' + package
['name']
41 actual
= command(['nix', 'hash', 'path', '--type', 'sha256', '--base32', str(source
)])
42 assert actual
== package
['sha256'], 'SOURCE HASH MISMATCH: ' + package
['name']
43 target
.mkdir(parents
=True, exist_ok
=False)
44 provenance
= {'sources': environment
['manifest'], 'patches': [], 'effectiveSources': []}
45 for package
in environment
['manifest']:
46 name
= package
['name']
47 source
= Path(package
['source'])
50 effective
= target
/ name
51 shutil
.copytree(source
, effective
)
52 for path
in [effective
, *effective
.rglob('*')]:
53 path
.chmod(path
.stat().st_mode |
0o200)
54 before
= {str(p
.relative_to(effective
)): digest(p
)
55 for p
in effective
.rglob('*') if p
.is_file()}
56 patch
= ROOT
/ 'patches' / (name
+ '-19d0b.patch')
57 pinned_patch
= next(p
for p
in environment
['localPatches'] if p
['package'] == name
)
58 assert digest(patch
) == pinned_patch
['sha256'], 'LOCAL PATCH HASH MISMATCH: ' + name
59 applied
= subprocess
.run(['patch', '--batch', '--fuzz=0', '-p1',
60 '-d', str(effective
), '-i', str(patch
)],
61 stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
, text
=True)
62 assert applied
.returncode
== 0, applied
.stdout
63 assert 'offset' not in applied
.stdout
and 'fuzz' not in applied
.stdout
, applied
.stdout
64 after
= {str(p
.relative_to(effective
)): digest(p
)
65 for p
in effective
.rglob('*') if p
.is_file()}
66 assert before
.keys() == after
.keys(), 'Unexpected files from patch'
67 changed
= sorted(p
for p
in before
if before
[p
] != after
[p
])
68 assert changed
== sorted(PATCHES
[name
]), changed
69 provenance
['patches'].append({
70 'package': name
, 'patch': str(patch
.relative_to(ROOT
)), 'sha256': digest(patch
),
71 'changedFiles': {p
: {'before': before
[p
], 'after': after
[p
]} for p
in changed
},
72 'application': applied
.stdout
.strip()})
73 provenance
['effectiveSources'].append({'name': name
, 'source': str(effective
)})
74 provenance
['sourceRegistry'] = registry([p
['source'] for p
in provenance
['effectiveSources']])
75 (target
/ 'provenance.json').write_text(json
.dumps(provenance
, indent
=2) + '\n')
80 parser
= argparse
.ArgumentParser(description
=__doc__
)
81 parser
.add_argument('--directory', required
=True, type=Path
, help='New output directory; never overwritten')
82 parser
.add_argument('--registry', action
='store_true', help='Print only CL_SOURCE_REGISTRY')
83 args
= parser
.parse_args()
84 _
, environment
= source_environment()
85 provenance
= prepare(environment
, args
.directory
)
86 print(provenance
['sourceRegistry'] if args
.registry
else json
.dumps(provenance
, indent
=2))
89 if __name__
== '__main__':