Revision 16eaeb86
Added by koszko over 1 year ago
.gitignore | ||
---|---|---|
9 | 9 |
*.egg-info |
10 | 10 |
*.pyc |
11 | 11 |
setuptools |
12 |
src/hydrilla_builder/_version.py |
|
12 |
src/hydrilla/builder/_version.py |
.gitmodules | ||
---|---|---|
5 | 5 |
# Available under the terms of Creative Commons Zero v1.0 Universal. |
6 | 6 |
|
7 | 7 |
[submodule "src/hydrilla_builder/schemas"] |
8 |
path = src/hydrilla_builder/schemas
|
|
8 |
path = src/hydrilla/builder/schemas
|
|
9 | 9 |
url = ../hydrilla-json-schemas |
10 | 10 |
[submodule "src/test/source-package-example"] |
11 | 11 |
path = src/test/source-package-example |
LICENSES/0BSD.txt | ||
---|---|---|
1 |
Copyright (C) YEAR by AUTHOR EMAIL |
|
2 |
|
|
3 |
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. |
|
4 |
|
|
5 |
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
MANIFEST.in | ||
---|---|---|
4 | 4 |
# |
5 | 5 |
# Available under the terms of Creative Commons Zero v1.0 Universal. |
6 | 6 |
|
7 |
include src/hydrilla_builder/schemas/package_source-*.schema.json*
|
|
7 |
include src/hydrilla/builder/schemas/package_source-*.schema.json*
|
|
8 | 8 |
include src/test/source-package-example/* |
9 |
global-exclude .git .gitignore .gitmodules |
|
9 |
global-exclude .git .gitignore .gitmodules |
README.md | ||
---|---|---|
1 |
TODO... |
|
1 |
# These are the sources of Hydrilla builder, a tool to convert packages into a form serveable by Hydrilla. |
|
2 |
|
|
3 |
TODO... |
|
4 |
|
|
5 |
To build the supplied example you can do something along the lines of: |
|
6 |
``` |
|
7 |
mkdir /tmp/bananowarzez/ |
|
8 |
PYTHONPATH=src python3 -m hydrilla.builder -s src/test/source-package-example/ \ |
|
9 |
-d /tmp/bananowarzez/ |
|
10 |
# Now, list the files we just produced |
|
11 |
find /tmp/bananowarzez/ |
|
12 |
``` |
pyproject.toml | ||
---|---|---|
9 | 9 |
requires = ["setuptools>=45", "wheel", "setuptools_scm>=6.2"] |
10 | 10 |
|
11 | 11 |
[tool.setuptools_scm] |
12 |
write_to = "src/hydrilla_builder/_version.py" |
|
12 |
write_to = "src/hydrilla/builder/_version.py" |
setup.cfg | ||
---|---|---|
5 | 5 |
# Available under the terms of Creative Commons Zero v1.0 Universal. |
6 | 6 |
|
7 | 7 |
[metadata] |
8 |
name = hydrilla_builder
|
|
9 |
version = 1.0
|
|
8 |
name = hydrilla.builder
|
|
9 |
version = 0.999
|
|
10 | 10 |
author = Wojtek Kosior |
11 | 11 |
author_email = koszko@koszko.org |
12 | 12 |
description = Hydrilla package builder |
... | ... | |
50 | 50 |
|
51 | 51 |
[options.entry_points] |
52 | 52 |
console_scripts = |
53 |
hydrilla-builder = __main__:perform_build |
|
53 |
hydrilla-builder = hydrilla.builder.__main__:perform_build |
setup.py | ||
---|---|---|
7 | 7 |
|
8 | 8 |
import setuptools |
9 | 9 |
|
10 |
setuptools.setup(package_data={'hydrilla_builder': ['*.json']}) |
|
10 |
setuptools.setup() |
src/hydrilla/__init__.py | ||
---|---|---|
1 |
# SPDX-License-Identifier: 0BSD |
|
2 |
|
|
3 |
# Copyright (C) 2013-2020, PyPA |
|
4 |
|
|
5 |
# https://packaging.python.org/en/latest/guides/packaging-namespace-packages/#pkgutil-style-namespace-packages |
|
6 |
|
|
7 |
__path__ = __import__('pkgutil').extend_path(__path__, __name__) |
src/hydrilla/builder/__init__.py | ||
---|---|---|
1 |
# SPDX-License-Identifier: CC0-1.0 |
|
2 |
|
|
3 |
# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org> |
|
4 |
# |
|
5 |
# Available under the terms of Creative Commons Zero v1.0 Universal. |
|
6 |
|
|
7 |
from .build import Build |
src/hydrilla/builder/__main__.py | ||
---|---|---|
1 |
# SPDX-License-Identifier: AGPL-3.0-or-later |
|
2 |
|
|
3 |
# Command line interface of Hydrilla package builder. |
|
4 |
# |
|
5 |
# This file is part of Hydrilla |
|
6 |
# |
|
7 |
# Copyright (C) 2022 Wojtek Kosior |
|
8 |
# |
|
9 |
# This program is free software: you can redistribute it and/or modify |
|
10 |
# it under the terms of the GNU Affero General Public License as |
|
11 |
# published by the Free Software Foundation, either version 3 of the |
|
12 |
# License, or (at your option) any later version. |
|
13 |
# |
|
14 |
# This program is distributed in the hope that it will be useful, |
|
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
# GNU Affero General Public License for more details. |
|
18 |
# |
|
19 |
# You should have received a copy of the GNU Affero General Public License |
|
20 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
21 |
# |
|
22 |
# |
|
23 |
# I, Wojtek Kosior, thereby promise not to sue for violation of this |
|
24 |
# file's license. Although I request that you do not make use this code |
|
25 |
# in a proprietary program, I am not going to enforce this in court. |
|
26 |
|
|
27 |
from pathlib import Path |
|
28 |
|
|
29 |
import click |
|
30 |
|
|
31 |
from .build import Build |
|
32 |
|
|
33 |
def validate_dir_path(ctx, param, value): |
|
34 |
path = Path(value) |
|
35 |
if path.is_dir(): |
|
36 |
return path.resolve() |
|
37 |
|
|
38 |
raise click.BadParameter(f'{param.human_readable_name} must be a directory path') |
|
39 |
|
|
40 |
def validate_path(ctx, param, value): |
|
41 |
return Path(value) |
|
42 |
|
|
43 |
@click.command() |
|
44 |
@click.option('-s', '--srcdir', default='.', type=click.Path(), |
|
45 |
callback=validate_dir_path, |
|
46 |
help='Source directory to build from.') |
|
47 |
@click.option('-i', '--index-json', default='index.json', type=click.Path(), |
|
48 |
callback=validate_path, |
|
49 |
help='Path to file to be processed instead of index.json (if not absolute, resolved relative to srcdir).') |
|
50 |
@click.option('-d', '--dstdir', type=click.Path(), required=True, |
|
51 |
callback=validate_dir_path, |
|
52 |
help='Destination directory to write built package files to.') |
|
53 |
def preform_build(srcdir, index_json, dstdir): |
|
54 |
""" |
|
55 |
Build Hydrilla package from scrdir and write the resulting files under |
|
56 |
dstdir. |
|
57 |
""" |
|
58 |
build = Build(srcdir, index_json) |
|
59 |
build.write_package_files(dstdir) |
|
60 |
|
|
61 |
preform_build() |
src/hydrilla/builder/build.py | ||
---|---|---|
1 |
# SPDX-License-Identifier: AGPL-3.0-or-later |
|
2 |
|
|
3 |
# Building Hydrilla packages. |
|
4 |
# |
|
5 |
# This file is part of Hydrilla |
|
6 |
# |
|
7 |
# Copyright (C) 2022 Wojtek Kosior |
|
8 |
# |
|
9 |
# This program is free software: you can redistribute it and/or modify |
|
10 |
# it under the terms of the GNU Affero General Public License as |
|
11 |
# published by the Free Software Foundation, either version 3 of the |
|
12 |
# License, or (at your option) any later version. |
|
13 |
# |
|
14 |
# This program is distributed in the hope that it will be useful, |
|
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
# GNU Affero General Public License for more details. |
|
18 |
# |
|
19 |
# You should have received a copy of the GNU Affero General Public License |
|
20 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
21 |
# |
|
22 |
# |
|
23 |
# I, Wojtek Kosior, thereby promise not to sue for violation of this |
|
24 |
# file's license. Although I request that you do not make use this code |
|
25 |
# in a proprietary program, I am not going to enforce this in court. |
|
26 |
|
|
27 |
import json |
|
28 |
import re |
|
29 |
import zipfile |
|
30 |
from pathlib import Path |
|
31 |
from hashlib import sha256 |
|
32 |
from sys import stderr |
|
33 |
|
|
34 |
import jsonschema |
|
35 |
|
|
36 |
from .. import util |
|
37 |
|
|
38 |
here = Path(__file__).resolve().parent |
|
39 |
with open(here / 'schemas' / 'package_source-1.schema.json') as schema_file: |
|
40 |
index_json_schema = json.load(schema_file) |
|
41 |
|
|
42 |
class FileReferenceError(Exception): |
|
43 |
""" |
|
44 |
Exception used to report various problems concerning files referenced from |
|
45 |
source package's index.json. |
|
46 |
""" |
|
47 |
|
|
48 |
class ReuseError(Exception): |
|
49 |
""" |
|
50 |
Exception used to report various problems when calling the REUSE tool. |
|
51 |
""" |
|
52 |
|
|
53 |
class FileBuffer: |
|
54 |
""" |
|
55 |
Implement a file-like object that buffers data written to it. |
|
56 |
""" |
|
57 |
def __init__(self): |
|
58 |
""" |
|
59 |
Initialize FileBuffer. |
|
60 |
""" |
|
61 |
self.chunks = [] |
|
62 |
|
|
63 |
def write(self, b): |
|
64 |
""" |
|
65 |
Buffer 'b', return number of bytes buffered. |
|
66 |
|
|
67 |
'b' is expected to be an instance of 'bytes' or 'str', in which case it |
|
68 |
gets encoded as UTF-8. |
|
69 |
""" |
|
70 |
if type(b) is str: |
|
71 |
b = b.encode() |
|
72 |
self.chunks.append(b) |
|
73 |
return len(b) |
|
74 |
|
|
75 |
def flush(self): |
|
76 |
""" |
|
77 |
A no-op mock of file-like object's flush() method. |
|
78 |
""" |
|
79 |
pass |
|
80 |
|
|
81 |
def get_bytes(self): |
|
82 |
""" |
|
83 |
Return all data written so far concatenated into a single 'bytes' |
|
84 |
object. |
|
85 |
""" |
|
86 |
return b''.join(self.chunks) |
|
87 |
|
|
88 |
def generate_spdx_report(root): |
|
89 |
""" |
|
90 |
Use REUSE tool to generate an SPDX report for sources under 'root' and |
|
91 |
return the report's contents as 'bytes'. |
|
92 |
|
|
93 |
'root' shall be an instance of pathlib.Path. |
|
94 |
|
|
95 |
In case the directory tree under 'root' does not constitute a |
|
96 |
REUSE-compliant package, linting report is printed to standard output and |
|
97 |
an exception is raised. |
|
98 |
|
|
99 |
In case the reuse package is not installed, an exception is also raised. |
|
100 |
""" |
|
101 |
try: |
|
102 |
from reuse._main import main as reuse_main |
|
103 |
except ModuleNotFoundError: |
|
104 |
ReuseError("Could not import 'reuse'. Is the tool installed and visible to this Python instance?") |
|
105 |
|
|
106 |
mocked_output = FileBuffer() |
|
107 |
if reuse_main(args=['--root', str(root), 'lint'], out=mocked_output) != 0: |
|
108 |
stderr.write(mocked_output.get_bytes().decode()) |
|
109 |
raise ReuseError('Attempt to generate an SPDX report for a REUSE-incompliant package.') |
|
110 |
|
|
111 |
mocked_output = FileBuffer() |
|
112 |
if reuse_main(args=['--root', str(root), 'spdx'], out=mocked_output) != 0: |
|
113 |
stderr.write(mocked_output.get_bytes().decode()) |
|
114 |
raise ReuseError("Couldn't generate an SPDX report for package.") |
|
115 |
|
|
116 |
return mocked_output.get_bytes() |
|
117 |
|
|
118 |
class FileRef: |
|
119 |
"""Represent reference to a file in the package.""" |
|
120 |
def __init__(self, path: Path, contents: bytes): |
|
121 |
"""Initialize FileRef.""" |
|
122 |
self.include_in_distribution = False |
|
123 |
self.include_in_zipfile = True |
|
124 |
self.path = path |
|
125 |
self.contents = contents |
|
126 |
|
|
127 |
self.contents_hash = sha256(contents).digest().hex() |
|
128 |
|
|
129 |
def make_ref_dict(self, filename: str): |
|
130 |
""" |
|
131 |
Represent the file reference through a dict that can be included in JSON |
|
132 |
defintions. |
|
133 |
""" |
|
134 |
return { |
|
135 |
'file': filename, |
|
136 |
'sha256': self.contents_hash |
|
137 |
} |
|
138 |
|
|
139 |
class Build: |
|
140 |
""" |
|
141 |
Build a Hydrilla package. |
|
142 |
""" |
|
143 |
def __init__(self, srcdir, index_json_path): |
|
144 |
""" |
|
145 |
Initialize a build. All files to be included in a distribution package |
|
146 |
are loaded into memory, all data gets validated and all necessary |
|
147 |
computations (e.g. preparing of hashes) are performed. |
|
148 |
|
|
149 |
'srcdir' and 'index_json' are expected to be pathlib.Path objects. |
|
150 |
""" |
|
151 |
self.srcdir = srcdir.resolve() |
|
152 |
self.index_json_path = index_json_path |
|
153 |
self.files_by_path = {} |
|
154 |
self.resource_list = [] |
|
155 |
self.mapping_list = [] |
|
156 |
|
|
157 |
if not index_json_path.is_absolute(): |
|
158 |
self.index_json_path = (self.srcdir / self.index_json_path) |
|
159 |
|
|
160 |
self.index_json_path = self.index_json_path.resolve() |
|
161 |
|
|
162 |
with open(self.index_json_path, 'rt') as index_file: |
|
163 |
index_json_text = index_file.read() |
|
164 |
|
|
165 |
index_obj = json.loads(util.strip_json_comments(index_json_text)) |
|
166 |
|
|
167 |
self.files_by_path[self.srcdir / 'index.json'] = \ |
|
168 |
FileRef(self.srcdir / 'index.json', index_json_text.encode()) |
|
169 |
|
|
170 |
self._process_index_json(index_obj) |
|
171 |
|
|
172 |
def _process_file(self, filename: str, include_in_distribution: bool=True): |
|
173 |
""" |
|
174 |
Resolve 'filename' relative to srcdir, load it to memory (if not loaded |
|
175 |
before), compute its hash and store its information in |
|
176 |
'self.files_by_path'. |
|
177 |
|
|
178 |
'filename' shall represent a relative path using '/' as a separator. |
|
179 |
|
|
180 |
if 'include_in_distribution' is True it shall cause the file to not only |
|
181 |
be included in the source package's zipfile, but also written as one of |
|
182 |
built package's files. |
|
183 |
|
|
184 |
Return file's reference object that can be included in JSON defintions |
|
185 |
of various kinds. |
|
186 |
""" |
|
187 |
path = self.srcdir |
|
188 |
for segment in filename.split('/'): |
|
189 |
path /= segment |
|
190 |
|
|
191 |
path = path.resolve() |
|
192 |
if not path.is_relative_to(self.srcdir): |
|
193 |
raise FileReferenceError(f"Attempt to load '{filename}' which lies outside package source directory.") |
|
194 |
|
|
195 |
if str(path.relative_to(self.srcdir)) == 'index.json': |
|
196 |
raise FileReferenceError("Attempt to load 'index.json' which is a reserved filename.") |
|
197 |
|
|
198 |
file_ref = self.files_by_path.get(path) |
|
199 |
if file_ref is None: |
|
200 |
with open(path, 'rb') as file_handle: |
|
201 |
contents = file_handle.read() |
|
202 |
|
|
203 |
file_ref = FileRef(path, contents) |
|
204 |
self.files_by_path[path] = file_ref |
|
205 |
|
|
206 |
if include_in_distribution: |
|
207 |
file_ref.include_in_distribution = True |
|
208 |
|
|
209 |
return file_ref.make_ref_dict(filename) |
|
210 |
|
|
211 |
def _prepare_source_package_zip(self, root_dir_name: str): |
|
212 |
""" |
|
213 |
Create and store in memory a .zip archive containing files needed to |
|
214 |
build this source package. |
|
215 |
|
|
216 |
'root_dir_name' shall not contain any slashes ('/'). |
|
217 |
|
|
218 |
Return zipfile's sha256 sum's hexstring. |
|
219 |
""" |
|
220 |
fb = FileBuffer() |
|
221 |
root_dir_path = Path(root_dir_name) |
|
222 |
|
|
223 |
def zippath(file_path): |
|
224 |
file_path = root_dir_path / file_path.relative_to(self.srcdir) |
|
225 |
return file_path.as_posix() |
|
226 |
|
|
227 |
with zipfile.ZipFile(fb, 'w') as xpi: |
|
228 |
for file_ref in self.files_by_path.values(): |
|
229 |
if file_ref.include_in_zipfile: |
|
230 |
xpi.writestr(zippath(file_ref.path), file_ref.contents) |
|
231 |
|
|
232 |
self.source_zip_contents = fb.get_bytes() |
|
233 |
|
|
234 |
return sha256(self.source_zip_contents).digest().hex() |
|
235 |
|
|
236 |
def _process_item(self, item_def: dict): |
|
237 |
""" |
|
238 |
Process 'item_def' as definition of a resource/mapping and store in |
|
239 |
memory its processed form and files used by it. |
|
240 |
|
|
241 |
Return a minimal item reference suitable for using in source |
|
242 |
description. |
|
243 |
""" |
|
244 |
copy_props = ['type', 'identifier', 'long_name', 'uuid', 'description'] |
|
245 |
if 'comment' in item_def: |
|
246 |
copy_props.append('comment') |
|
247 |
|
|
248 |
if item_def['type'] == 'resource': |
|
249 |
item_list = self.resource_list |
|
250 |
|
|
251 |
copy_props.append('revision') |
|
252 |
|
|
253 |
script_file_refs = [self._process_file(f['file']) |
|
254 |
for f in item_def.get('scripts', [])] |
|
255 |
|
|
256 |
new_item_obj = { |
|
257 |
'dependencies': item_def.get('dependencies', []), |
|
258 |
'scripts': script_file_refs |
|
259 |
} |
|
260 |
else: |
|
261 |
item_list = self.mapping_list |
|
262 |
|
|
263 |
payloads = {} |
|
264 |
for pat, res_ref in item_def.get('payloads', {}).items(): |
|
265 |
payloads[pat] = {'identifier': res_ref['identifier']} |
|
266 |
|
|
267 |
new_item_obj = { |
|
268 |
'payloads': payloads |
|
269 |
} |
|
270 |
|
|
271 |
new_item_obj.update([(p, item_def[p]) for p in copy_props]) |
|
272 |
|
|
273 |
new_item_obj['version'] = util.normalize_version(item_def['version']) |
|
274 |
new_item_obj['api_schema_version'] = [1, 0, 1] |
|
275 |
new_item_obj['source_copyright'] = self.copyright_file_refs |
|
276 |
new_item_obj['source_name'] = self.source_name |
|
277 |
|
|
278 |
item_list.append(new_item_obj) |
|
279 |
|
|
280 |
return dict([(prop, new_item_obj[prop]) |
|
281 |
for prop in ('type', 'identifier', 'version')]) |
|
282 |
|
|
283 |
def _process_index_json(self, index_obj: dict): |
|
284 |
""" |
|
285 |
Process 'index_obj' as contents of source package's index.json and store |
|
286 |
in memory this source package's zipfile as well as package's individual |
|
287 |
files and computed definitions of the source package and items defined |
|
288 |
in it. |
|
289 |
""" |
|
290 |
jsonschema.validate(index_obj, index_json_schema) |
|
291 |
|
|
292 |
self.source_name = index_obj['source_name'] |
|
293 |
|
|
294 |
generate_spdx = index_obj.get('reuse_generate_spdx_report', False) |
|
295 |
if generate_spdx: |
|
296 |
contents = generate_spdx_report(self.srcdir) |
|
297 |
spdx_path = (self.srcdir / 'report.spdx').resolve() |
|
298 |
spdx_ref = FileRef(spdx_path, contents) |
|
299 |
|
|
300 |
spdx_ref.include_in_zipfile = False |
|
301 |
self.files_by_path[spdx_path] = spdx_ref |
|
302 |
|
|
303 |
self.copyright_file_refs = \ |
|
304 |
[self._process_file(f['file']) for f in index_obj['copyright']] |
|
305 |
|
|
306 |
if generate_spdx and not spdx_ref.include_in_distribution: |
|
307 |
raise FileReferenceError("Told to generate 'report.spdx' but 'report.spdx' is not listed among copyright files. Refusing to proceed.") |
|
308 |
|
|
309 |
item_refs = [self._process_item(d) for d in index_obj['definitions']] |
|
310 |
|
|
311 |
for file_ref in index_obj.get('additional_files', []): |
|
312 |
self._process_file(file_ref['file'], include_in_distribution=False) |
|
313 |
|
|
314 |
root_dir_path = Path(self.source_name) |
|
315 |
|
|
316 |
source_archives_obj = { |
|
317 |
'zip' : { |
|
318 |
'sha256': self._prepare_source_package_zip(root_dir_path) |
|
319 |
} |
|
320 |
} |
|
321 |
|
|
322 |
self.source_description = { |
|
323 |
'api_schema_version': [1, 0, 1], |
|
324 |
'source_name': self.source_name, |
|
325 |
'source_copyright': self.copyright_file_refs, |
|
326 |
'upstream_url': index_obj['upstream_url'], |
|
327 |
'definitions': item_refs, |
|
328 |
'source_archives': source_archives_obj |
|
329 |
} |
|
330 |
|
|
331 |
if 'comment' in index_obj: |
|
332 |
self.source_description['comment'] = index_obj['comment'] |
|
333 |
|
|
334 |
def write_source_package_zip(self, dstpath: Path): |
|
335 |
""" |
|
336 |
Create a .zip archive containing files needed to build this source |
|
337 |
package and write it at 'dstpath'. |
|
338 |
""" |
|
339 |
with open(dstpath, 'wb') as output: |
|
340 |
output.write(self.source_zip_contents) |
|
341 |
|
|
342 |
def write_package_files(self, dstpath: Path): |
|
343 |
"""Write package files under 'dstpath' for distribution.""" |
|
344 |
file_dir_path = (dstpath / 'file').resolve() |
|
345 |
file_dir_path.mkdir(parents=True, exist_ok=True) |
|
346 |
|
|
347 |
for file_ref in self.files_by_path.values(): |
|
348 |
if file_ref.include_in_distribution: |
|
349 |
file_name = f'sha256-{file_ref.contents_hash}' |
|
350 |
with open(file_dir_path / file_name, 'wb') as output: |
|
351 |
output.write(file_ref.contents) |
|
352 |
|
|
353 |
source_dir_path = (dstpath / 'source').resolve() |
|
354 |
source_dir_path.mkdir(parents=True, exist_ok=True) |
|
355 |
source_name = self.source_description["source_name"] |
|
356 |
|
|
357 |
with open(source_dir_path / f'{source_name}.json', 'wt') as output: |
|
358 |
json.dump(self.source_description, output) |
|
359 |
|
|
360 |
with open(source_dir_path / f'{source_name}.zip', 'wb') as output: |
|
361 |
output.write(self.source_zip_contents) |
|
362 |
|
|
363 |
for item_type, item_list in [ |
|
364 |
('resource', self.resource_list), |
|
365 |
('mapping', self.mapping_list) |
|
366 |
]: |
|
367 |
item_type_dir_path = (dstpath / item_type).resolve() |
|
368 |
|
|
369 |
for item_def in item_list: |
|
370 |
item_dir_path = item_type_dir_path / item_def['identifier'] |
|
371 |
item_dir_path.mkdir(parents=True, exist_ok=True) |
|
372 |
|
|
373 |
version = '.'.join([str(n) for n in item_def['version']]) |
|
374 |
with open(item_dir_path / version, 'wt') as output: |
|
375 |
json.dump(item_def, output) |
src/hydrilla/builder/schemas | ||
---|---|---|
1 |
Subproject commit ca1de2ed4a69a71f2f75552ade693d04ea1baa85 |
src/hydrilla/util/__init__.py | ||
---|---|---|
1 |
# SPDX-License-Identifier: AGPL-3.0-or-later |
|
2 |
|
|
3 |
# Building Hydrilla packages. |
|
4 |
# |
|
5 |
# This file is part of Hydrilla |
|
6 |
# |
|
7 |
# Copyright (C) 2021, 2022 Wojtek Kosior |
|
8 |
# |
|
9 |
# This program is free software: you can redistribute it and/or modify |
|
10 |
# it under the terms of the GNU Affero General Public License as |
|
11 |
# published by the Free Software Foundation, either version 3 of the |
|
12 |
# License, or (at your option) any later version. |
|
13 |
# |
|
14 |
# This program is distributed in the hope that it will be useful, |
|
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
# GNU Affero General Public License for more details. |
|
18 |
# |
|
19 |
# You should have received a copy of the GNU Affero General Public License |
|
20 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
21 |
# |
|
22 |
# |
|
23 |
# I, Wojtek Kosior, thereby promise not to sue for violation of this |
|
24 |
# file's license. Although I request that you do not make use this code |
|
25 |
# in a proprietary program, I am not going to enforce this in court. |
|
26 |
|
|
27 |
import re as _re |
|
28 |
import json as _json |
|
29 |
|
|
30 |
from typing import Optional as _Optional |
|
31 |
|
|
32 |
_strip_comment_re = _re.compile(r''' |
|
33 |
^ # match from the beginning of each line |
|
34 |
( # catch the part before '//' comment |
|
35 |
(?: # this group matches either a string or a single out-of-string character |
|
36 |
[^"/] | |
|
37 |
" |
|
38 |
(?: # this group matches any in-a-string character |
|
39 |
[^"\\] | # match any normal character |
|
40 |
\\[^u] | # match any escaped character like '\f' or '\n' |
|
41 |
\\u[a-fA-F0-9]{4} # match an escape |
|
42 |
)* |
|
43 |
" |
|
44 |
)* |
|
45 |
) |
|
46 |
# expect either end-of-line or a comment: |
|
47 |
# * unterminated strings will cause matching to fail |
|
48 |
# * bad comment (with '/' instead of '//') will be indicated by second group |
|
49 |
# having length 1 instead of 2 or 0 |
|
50 |
(//?|$) |
|
51 |
''', _re.VERBOSE) |
|
52 |
|
|
53 |
def strip_json_comments(text: str) -> str: |
|
54 |
""" |
|
55 |
Accept JSON text with optional C++-style ('//') comments and return the text |
|
56 |
with comments removed. Consecutive slashes inside strings are handled |
|
57 |
properly. A spurious single slash ('/') shall generate an error. Errors in |
|
58 |
JSON itself shall be ignored. |
|
59 |
""" |
|
60 |
processed = 0 |
|
61 |
stripped_text = [] |
|
62 |
for line in text.split('\n'): |
|
63 |
match = _strip_comment_re.match(line) |
|
64 |
|
|
65 |
if match is None: # unterminated string |
|
66 |
# ignore this error, let json module report it |
|
67 |
stripped = line |
|
68 |
elif len(match[2]) == 1: |
|
69 |
raise _json.JSONDecodeError('bad comment', text, |
|
70 |
processed + len(match[1])) |
|
71 |
else: |
|
72 |
stripped = match[1] |
|
73 |
|
|
74 |
stripped_text.append(stripped) |
|
75 |
processed += len(line) + 1 |
|
76 |
|
|
77 |
return '\n'.join(stripped_text) |
|
78 |
|
|
79 |
def normalize_version(ver: list[int]) -> list[int]: |
|
80 |
"""Strip right-most zeroes from 'ver'. The original list is not modified.""" |
|
81 |
new_len = 0 |
|
82 |
for i, num in enumerate(ver): |
|
83 |
if num != 0: |
|
84 |
new_len = i + 1 |
|
85 |
|
|
86 |
return ver[:new_len] |
|
87 |
|
|
88 |
def parse_version(ver_str: str) -> list[int]: |
|
89 |
""" |
|
90 |
Convert 'ver_str' into an array representation, e.g. for ver_str="4.6.13.0" |
|
91 |
return [4, 6, 13, 0]. |
|
92 |
""" |
|
93 |
return [int(num) for num in ver_str.split('.')] |
|
94 |
|
|
95 |
def version_string(ver: list[int], rev: _Optional[int]=None) -> str: |
|
96 |
""" |
|
97 |
Produce version's string representation (optionally with revision), like: |
|
98 |
1.2.3-5 |
|
99 |
No version normalization is performed. |
|
100 |
""" |
|
101 |
return '.'.join([str(n) for n in ver]) + ('' if rev is None else f'-{rev}') |
src/hydrilla_builder/__init__.py | ||
---|---|---|
1 |
# SPDX-License-Identifier: CC0-1.0 |
|
2 |
|
|
3 |
# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org> |
|
4 |
# |
|
5 |
# Available under the terms of Creative Commons Zero v1.0 Universal. |
src/hydrilla_builder/__main__.py | ||
---|---|---|
1 |
# SPDX-License-Identifier: AGPL-3.0-or-later |
|
2 |
|
|
3 |
# Command line interface of Hydrilla package builder. |
|
4 |
# |
|
5 |
# This file is part of Hydrilla |
|
6 |
# |
|
7 |
# Copyright (C) 2022 Wojtek Kosior |
|
8 |
# |
|
9 |
# This program is free software: you can redistribute it and/or modify |
|
10 |
# it under the terms of the GNU Affero General Public License as |
|
11 |
# published by the Free Software Foundation, either version 3 of the |
|
12 |
# License, or (at your option) any later version. |
|
13 |
# |
|
14 |
# This program is distributed in the hope that it will be useful, |
|
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
# GNU Affero General Public License for more details. |
|
18 |
# |
|
19 |
# You should have received a copy of the GNU Affero General Public License |
|
20 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
21 |
# |
|
22 |
# |
|
23 |
# I, Wojtek Kosior, thereby promise not to sue for violation of this |
|
24 |
# file's license. Although I request that you do not make use this code |
|
25 |
# in a proprietary program, I am not going to enforce this in court. |
|
26 |
|
|
27 |
from pathlib import Path |
|
28 |
|
|
29 |
import click |
|
30 |
|
|
31 |
from .build import Build |
|
32 |
|
|
33 |
def validate_dir_path(ctx, param, value): |
|
34 |
path = Path(value) |
|
35 |
if path.is_dir(): |
|
36 |
return path.resolve() |
|
37 |
|
|
38 |
raise click.BadParameter(f'{param.human_readable_name} must be a directory path') |
|
39 |
|
|
40 |
def validate_path(ctx, param, value): |
|
41 |
return Path(value) |
|
42 |
|
|
43 |
@click.command() |
|
44 |
@click.option('-s', '--srcdir', default='.', type=click.Path(), |
|
45 |
callback=validate_dir_path, |
|
46 |
help='Source directory to build from.') |
|
47 |
@click.option('-i', '--index-json', default='index.json', type=click.Path(), |
|
48 |
callback=validate_path, |
|
49 |
help='Path to file to be processed instead of index.json (if not absolute, resolved relative to srcdir).') |
|
50 |
@click.option('-d', '--dstdir', type=click.Path(), required=True, |
|
51 |
callback=validate_dir_path, |
|
52 |
help='Destination directory to write built package files to.') |
|
53 |
def preform_build(srcdir, index_json, dstdir): |
|
54 |
""" |
|
55 |
Build Hydrilla package from scrdir and write the resulting files under |
|
56 |
dstdir. |
|
57 |
""" |
|
58 |
build = Build(srcdir, index_json) |
|
59 |
build.write_package_files(dstdir) |
|
60 |
|
|
61 |
preform_build() |
src/hydrilla_builder/build.py | ||
---|---|---|
1 |
# SPDX-License-Identifier: AGPL-3.0-or-later |
|
2 |
|
|
3 |
# Building Hydrilla packages. |
|
4 |
# |
|
5 |
# This file is part of Hydrilla |
|
6 |
# |
|
7 |
# Copyright (C) 2021,2022 Wojtek Kosior |
|
8 |
# |
|
9 |
# This program is free software: you can redistribute it and/or modify |
|
10 |
# it under the terms of the GNU Affero General Public License as |
|
11 |
# published by the Free Software Foundation, either version 3 of the |
|
12 |
# License, or (at your option) any later version. |
|
13 |
# |
|
14 |
# This program is distributed in the hope that it will be useful, |
|
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
# GNU Affero General Public License for more details. |
|
18 |
# |
|
19 |
# You should have received a copy of the GNU Affero General Public License |
|
20 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
21 |
# |
|
22 |
# |
|
23 |
# I, Wojtek Kosior, thereby promise not to sue for violation of this |
|
24 |
# file's license. Although I request that you do not make use this code |
|
25 |
# in a proprietary program, I am not going to enforce this in court. |
|
26 |
|
|
27 |
|
|
28 |
import json |
|
29 |
import re |
|
30 |
import zipfile |
|
31 |
from pathlib import Path |
|
32 |
from hashlib import sha256 |
|
33 |
from sys import stderr |
|
34 |
|
|
35 |
import jsonschema |
|
36 |
|
|
37 |
here = Path(__file__).resolve().parent |
|
38 |
with open(here / 'schemas' / 'package_source-1.schema.json') as schema_file: |
|
39 |
index_json_schema = json.load(schema_file) |
|
40 |
|
|
41 |
class FileReferenceError(Exception): |
|
42 |
""" |
|
43 |
Exception used to report various problems concerning files referenced from |
|
44 |
source package's index.json. |
|
45 |
""" |
|
46 |
|
|
47 |
class ReuseError(Exception): |
|
48 |
""" |
|
49 |
Exception used to report various problems when calling the REUSE tool. |
|
50 |
""" |
|
51 |
|
|
52 |
strip_comment_re = re.compile(r''' |
|
53 |
^ # match from the beginning of each line |
|
54 |
( # catch the part before '//' comment |
|
55 |
(?: # this group matches either a string or a single out-of-string character |
|
56 |
[^"/] | |
|
57 |
" |
|
58 |
(?: # this group matches any in-a-string character |
|
59 |
[^"\\] | # match any normal character |
|
60 |
\\[^u] | # match any escaped character like '\f' or '\n' |
|
61 |
\\u[a-fA-F0-9]{4} # match an escape |
|
62 |
)* |
|
63 |
" |
|
64 |
)* |
|
65 |
) |
|
66 |
# expect either end-of-line or a comment: |
|
67 |
# * unterminated strings will cause matching to fail |
|
68 |
# * bad comment (with '/' instead of '//') will be indicated by second group |
|
69 |
# having length 1 instead of 2 or 0 |
|
70 |
(//?|$) |
|
71 |
''', re.VERBOSE) |
|
72 |
|
|
73 |
def strip_json_comments(text): |
|
74 |
""" |
|
75 |
Accept JSON text with optional C++-style ('//') comments and return the text |
|
76 |
with comments removed. Consecutive slashes inside strings are handled |
|
77 |
properly. A spurious single slash ('/') shall generate an error. Errors in |
|
78 |
JSON itself shall be ignored. |
|
79 |
""" |
|
80 |
processed = 0 |
|
81 |
stripped_text = [] |
|
82 |
for line in text.split('\n'): |
|
83 |
match = strip_comment_re.match(line) |
|
84 |
|
|
85 |
if match is None: # unterminated string |
|
86 |
# ignore this error, let json module report it |
|
87 |
stripped = line |
|
88 |
elif len(match[2]) == 1: |
|
89 |
raise json.JSONDecodeError('bad comment', text, |
|
90 |
processed + len(match[1])) |
|
91 |
else: |
|
92 |
stripped = match[1] |
|
93 |
|
|
94 |
stripped_text.append(stripped) |
|
95 |
processed += len(line) + 1 |
|
96 |
|
|
97 |
return '\n'.join(stripped_text) |
|
98 |
|
|
99 |
def normalize_version(ver): |
|
100 |
''' |
|
101 |
'ver' is an array of integers. Strip right-most zeroes from ver. |
|
102 |
|
|
103 |
Returns a *new* array. Doesn't modify its argument. |
|
104 |
''' |
|
105 |
new_len = 0 |
|
106 |
for i, num in enumerate(ver): |
|
107 |
if num != 0: |
|
108 |
new_len = i + 1 |
|
109 |
|
|
110 |
return ver[:new_len] |
|
111 |
|
|
112 |
class FileBuffer: |
|
113 |
""" |
|
114 |
Implement a file-like object that buffers data written to it. |
|
115 |
""" |
|
116 |
def __init__(self): |
|
117 |
""" |
|
118 |
Initialize FileBuffer. |
|
119 |
""" |
|
120 |
self.chunks = [] |
|
121 |
|
|
122 |
def write(self, b): |
|
123 |
""" |
|
124 |
Buffer 'b', return number of bytes buffered. |
|
125 |
|
|
126 |
'b' is expected to be an instance of 'bytes' or 'str', in which case it |
|
127 |
gets encoded as UTF-8. |
|
128 |
""" |
|
129 |
if type(b) is str: |
|
130 |
b = b.encode() |
|
131 |
self.chunks.append(b) |
|
132 |
return len(b) |
|
133 |
|
|
134 |
def flush(self): |
|
135 |
""" |
|
136 |
A no-op mock of file-like object's flush() method. |
|
137 |
""" |
|
138 |
pass |
|
139 |
|
|
140 |
def get_bytes(self): |
|
141 |
""" |
|
142 |
Return all data written so far concatenated into a single 'bytes' |
|
143 |
object. |
|
144 |
""" |
|
145 |
return b''.join(self.chunks) |
|
146 |
|
|
147 |
def generate_spdx_report(root): |
|
148 |
""" |
|
149 |
Use REUSE tool to generate an SPDX report for sources under 'root' and |
|
150 |
return the report's contents as 'bytes'. |
|
151 |
|
|
152 |
'root' shall be an instance of pathlib.Path. |
|
153 |
|
|
154 |
In case the directory tree under 'root' does not constitute a |
|
155 |
REUSE-compliant package, linting report is printed to standard output and |
|
156 |
an exception is raised. |
|
157 |
|
|
158 |
In case the reuse package is not installed, an exception is also raised. |
|
159 |
""" |
|
160 |
try: |
|
161 |
from reuse._main import main as reuse_main |
|
162 |
except ModuleNotFoundError: |
|
163 |
ReuseError("Could not import 'reuse'. Is the tool installed and visible to this Python instance?") |
|
164 |
|
|
165 |
mocked_output = FileBuffer() |
|
166 |
if reuse_main(args=['--root', str(root), 'lint'], out=mocked_output) != 0: |
|
167 |
stderr.write(mocked_output.get_bytes().decode()) |
|
168 |
raise ReuseError('Attempt to generate an SPDX report for a REUSE-incompliant package.') |
|
169 |
|
|
170 |
mocked_output = FileBuffer() |
|
171 |
if reuse_main(args=['--root', str(root), 'spdx'], out=mocked_output) != 0: |
|
172 |
stderr.write(mocked_output.get_bytes().decode()) |
|
173 |
raise ReuseError("Couldn't generate an SPDX report for package.") |
|
174 |
|
|
175 |
return mocked_output.get_bytes() |
|
176 |
|
|
177 |
class FileRef: |
|
178 |
"""Represent reference to a file in the package.""" |
|
179 |
def __init__(self, path: Path, contents: bytes): |
|
180 |
"""Initialize FileRef.""" |
|
181 |
self.include_in_distribution = False |
|
182 |
self.include_in_zipfile = True |
|
183 |
self.path = path |
|
184 |
self.contents = contents |
|
185 |
|
|
186 |
self.contents_hash = sha256(contents).digest().hex() |
|
187 |
|
|
188 |
def make_ref_dict(self, filename: str): |
|
189 |
""" |
|
190 |
Represent the file reference through a dict that can be included in JSON |
|
191 |
defintions. |
|
192 |
""" |
|
193 |
return { |
|
194 |
'file': filename, |
|
195 |
'sha256': self.contents_hash |
|
196 |
} |
|
197 |
|
|
198 |
class Build: |
|
199 |
""" |
|
200 |
Build a Hydrilla package. |
|
201 |
""" |
|
202 |
def __init__(self, srcdir, index_json_path): |
|
203 |
""" |
|
204 |
Initialize a build. All files to be included in a distribution package |
|
205 |
are loaded into memory, all data gets validated and all necessary |
|
206 |
computations (e.g. preparing of hashes) are performed. |
|
207 |
|
|
208 |
'srcdir' and 'index_json' are expected to be pathlib.Path objects. |
|
209 |
""" |
|
210 |
self.srcdir = srcdir.resolve() |
|
211 |
self.index_json_path = index_json_path |
|
212 |
self.files_by_path = {} |
|
213 |
self.resource_list = [] |
|
214 |
self.mapping_list = [] |
|
215 |
|
|
216 |
if not index_json_path.is_absolute(): |
|
217 |
self.index_json_path = (self.srcdir / self.index_json_path) |
|
218 |
|
|
219 |
self.index_json_path = self.index_json_path.resolve() |
|
220 |
|
|
221 |
with open(self.index_json_path, 'rt') as index_file: |
|
222 |
index_json_text = index_file.read() |
|
223 |
|
|
224 |
index_obj = json.loads(strip_json_comments(index_json_text)) |
|
225 |
|
|
226 |
self.files_by_path[self.srcdir / 'index.json'] = \ |
|
227 |
FileRef(self.srcdir / 'index.json', index_json_text.encode()) |
|
228 |
|
|
229 |
self._process_index_json(index_obj) |
|
230 |
|
|
231 |
def _process_file(self, filename: str, include_in_distribution: bool=True): |
|
232 |
""" |
|
233 |
Resolve 'filename' relative to srcdir, load it to memory (if not loaded |
|
234 |
before), compute its hash and store its information in |
|
235 |
'self.files_by_path'. |
|
236 |
|
|
237 |
'filename' shall represent a relative path using '/' as a separator. |
|
238 |
|
|
239 |
if 'include_in_distribution' is True it shall cause the file to not only |
|
240 |
be included in the source package's zipfile, but also written as one of |
|
241 |
built package's files. |
|
242 |
|
|
243 |
Return file's reference object that can be included in JSON defintions |
|
244 |
of various kinds. |
|
245 |
""" |
|
246 |
path = self.srcdir |
|
247 |
for segment in filename.split('/'): |
|
248 |
path /= segment |
|
249 |
|
|
250 |
path = path.resolve() |
|
251 |
if not path.is_relative_to(self.srcdir): |
|
252 |
raise FileReferenceError(f"Attempt to load '{filename}' which lies outside package source directory.") |
|
253 |
|
|
254 |
if str(path.relative_to(self.srcdir)) == 'index.json': |
|
255 |
raise FileReferenceError("Attempt to load 'index.json' which is a reserved filename.") |
|
256 |
|
|
257 |
file_ref = self.files_by_path.get(path) |
|
258 |
if file_ref is None: |
|
259 |
with open(path, 'rb') as file_handle: |
|
260 |
contents = file_handle.read() |
|
261 |
|
|
262 |
file_ref = FileRef(path, contents) |
|
263 |
self.files_by_path[path] = file_ref |
|
264 |
|
|
265 |
if include_in_distribution: |
|
266 |
file_ref.include_in_distribution = True |
|
267 |
|
|
268 |
return file_ref.make_ref_dict(filename) |
|
269 |
|
|
270 |
def _prepare_source_package_zip(self, root_dir_name: str): |
|
271 |
""" |
|
272 |
Create and store in memory a .zip archive containing files needed to |
|
273 |
build this source package. |
|
274 |
|
|
275 |
'root_dir_name' shall not contain any slashes ('/'). |
|
276 |
|
|
277 |
Return zipfile's sha256 sum's hexstring. |
|
278 |
""" |
|
279 |
fb = FileBuffer() |
|
280 |
root_dir_path = Path(root_dir_name) |
|
281 |
|
|
282 |
def zippath(file_path): |
|
283 |
file_path = root_dir_path / file_path.relative_to(self.srcdir) |
|
284 |
return file_path.as_posix() |
|
285 |
|
|
286 |
with zipfile.ZipFile(fb, 'w') as xpi: |
|
287 |
for file_ref in self.files_by_path.values(): |
|
288 |
if file_ref.include_in_zipfile: |
|
289 |
xpi.writestr(zippath(file_ref.path), file_ref.contents) |
|
290 |
|
|
291 |
self.source_zip_contents = fb.get_bytes() |
|
292 |
|
|
293 |
return sha256(self.source_zip_contents).digest().hex() |
|
294 |
|
|
295 |
def _process_item(self, item_def: dict): |
|
296 |
""" |
|
297 |
Process 'item_def' as definition of a resource/mapping and store in |
|
298 |
memory its processed form and files used by it. |
|
299 |
|
|
300 |
Return a minimal item reference suitable for using in source |
|
301 |
description. |
|
302 |
""" |
|
303 |
copy_props = ['type', 'identifier', 'long_name', 'uuid', 'description'] |
|
304 |
if 'comment' in item_def: |
|
305 |
copy_props.append('comment') |
|
306 |
|
|
307 |
if item_def['type'] == 'resource': |
|
308 |
item_list = self.resource_list |
|
309 |
|
|
310 |
copy_props.append('revision') |
|
311 |
|
|
312 |
script_file_refs = [self._process_file(f['file']) |
|
313 |
for f in item_def.get('scripts', [])] |
|
314 |
|
|
315 |
new_item_obj = { |
|
316 |
'dependencies': item_def.get('dependencies', []), |
|
317 |
'scripts': script_file_refs |
|
318 |
} |
|
319 |
else: |
|
320 |
item_list = self.mapping_list |
|
321 |
|
|
322 |
payloads = {} |
|
323 |
for pat, res_ref in item_def.get('payloads', {}).items(): |
|
324 |
payloads[pat] = {'identifier': res_ref['identifier']} |
|
325 |
|
|
326 |
new_item_obj = { |
|
327 |
'payloads': payloads |
|
328 |
} |
|
329 |
|
|
330 |
new_item_obj.update([(p, item_def[p]) for p in copy_props]) |
|
331 |
|
|
332 |
new_item_obj['version'] = normalize_version(item_def['version']) |
|
333 |
new_item_obj['api_schema_version'] = [1, 0, 1] |
|
334 |
new_item_obj['source_copyright'] = self.copyright_file_refs |
|
335 |
new_item_obj['source_name'] = self.source_name |
|
336 |
|
|
337 |
item_list.append(new_item_obj) |
|
338 |
|
|
339 |
return dict([(prop, new_item_obj[prop]) |
|
340 |
for prop in ('type', 'identifier', 'version')]) |
|
341 |
|
|
342 |
def _process_index_json(self, index_obj: dict): |
|
343 |
""" |
|
344 |
Process 'index_obj' as contents of source package's index.json and store |
|
345 |
in memory this source package's zipfile as well as package's individual |
|
346 |
files and computed definitions of the source package and items defined |
|
347 |
in it. |
|
348 |
""" |
|
349 |
jsonschema.validate(index_obj, index_json_schema) |
|
350 |
|
|
351 |
self.source_name = index_obj['source_name'] |
|
352 |
|
|
353 |
generate_spdx = index_obj.get('reuse_generate_spdx_report', False) |
|
354 |
if generate_spdx: |
|
355 |
contents = generate_spdx_report(self.srcdir) |
|
356 |
spdx_path = (self.srcdir / 'report.spdx').resolve() |
|
357 |
spdx_ref = FileRef(spdx_path, contents) |
|
358 |
|
|
359 |
spdx_ref.include_in_zipfile = False |
|
360 |
self.files_by_path[spdx_path] = spdx_ref |
|
361 |
|
|
362 |
self.copyright_file_refs = \ |
|
363 |
[self._process_file(f['file']) for f in index_obj['copyright']] |
|
364 |
|
|
365 |
if generate_spdx and not spdx_ref.include_in_distribution: |
|
366 |
raise FileReferenceError("Told to generate 'report.spdx' but 'report.spdx' is not listed among copyright files. Refusing to proceed.") |
|
367 |
|
|
368 |
item_refs = [self._process_item(d) for d in index_obj['definitions']] |
|
369 |
|
|
370 |
for file_ref in index_obj.get('additional_files', []): |
|
371 |
self._process_file(file_ref['file'], include_in_distribution=False) |
|
372 |
|
|
373 |
root_dir_path = Path(self.source_name) |
|
374 |
|
|
375 |
source_archives_obj = { |
|
376 |
'zip' : { |
|
377 |
'sha256': self._prepare_source_package_zip(root_dir_path) |
|
378 |
} |
|
379 |
} |
|
380 |
|
|
381 |
self.source_description = { |
|
382 |
'api_schema_version': [1, 0, 1], |
|
383 |
'source_name': self.source_name, |
|
384 |
'source_copyright': self.copyright_file_refs, |
|
385 |
'upstream_url': index_obj['upstream_url'], |
|
386 |
'definitions': item_refs, |
|
387 |
'source_archives': source_archives_obj |
|
388 |
} |
|
389 |
|
|
390 |
if 'comment' in index_obj: |
|
391 |
self.source_description['comment'] = index_obj['comment'] |
|
392 |
|
|
393 |
def write_source_package_zip(self, dstpath: Path): |
|
394 |
""" |
|
395 |
Create a .zip archive containing files needed to build this source |
|
396 |
package and write it at 'dstpath'. |
|
397 |
""" |
|
398 |
with open(dstpath, 'wb') as output: |
|
399 |
output.write(self.source_zip_contents) |
|
400 |
|
|
401 |
def write_package_files(self, dstpath: Path): |
|
402 |
"""Write package files under 'dstpath' for distribution.""" |
|
403 |
file_dir_path = (dstpath / 'file').resolve() |
|
404 |
file_dir_path.mkdir(parents=True, exist_ok=True) |
|
405 |
|
|
406 |
for file_ref in self.files_by_path.values(): |
|
407 |
if file_ref.include_in_distribution: |
|
408 |
file_name = f'sha256-{file_ref.contents_hash}' |
|
409 |
with open(file_dir_path / file_name, 'wb') as output: |
|
410 |
output.write(file_ref.contents) |
|
411 |
|
|
412 |
source_dir_path = (dstpath / 'source').resolve() |
|
413 |
source_dir_path.mkdir(parents=True, exist_ok=True) |
|
414 |
source_name = self.source_description["source_name"] |
|
415 |
|
|
416 |
with open(source_dir_path / f'{source_name}.json', 'wt') as output: |
|
417 |
json.dump(self.source_description, output) |
|
418 |
|
|
419 |
with open(source_dir_path / f'{source_name}.zip', 'wb') as output: |
|
420 |
output.write(self.source_zip_contents) |
|
421 |
|
|
422 |
for item_type, item_list in [ |
|
423 |
('resource', self.resource_list), |
|
424 |
('mapping', self.mapping_list) |
|
425 |
]: |
|
426 |
item_type_dir_path = (dstpath / item_type).resolve() |
|
427 |
|
|
428 |
for item_def in item_list: |
|
429 |
item_dir_path = item_type_dir_path / item_def['identifier'] |
|
430 |
item_dir_path.mkdir(parents=True, exist_ok=True) |
|
431 |
|
|
432 |
version = '.'.join([str(n) for n in item_def['version']]) |
|
433 |
with open(item_dir_path / version, 'wt') as output: |
|
434 |
json.dump(item_def, output) |
src/hydrilla_builder/schemas | ||
---|---|---|
1 |
Subproject commit ca1de2ed4a69a71f2f75552ade693d04ea1baa85 |
src/test/test_hydrilla_builder.py | ||
---|---|---|
16 | 16 |
|
17 | 17 |
from jsonschema import ValidationError |
18 | 18 |
|
19 |
from hydrilla_builder import build |
|
19 |
from hydrilla import util as hydrilla_util |
|
20 |
from hydrilla.builder import build |
|
20 | 21 |
|
21 | 22 |
here = Path(__file__).resolve().parent |
22 | 23 |
|
... | ... | |
187 | 188 |
settings.srcdir = tmpdir / 'srcdir_copy' |
188 | 189 |
|
189 | 190 |
with open(settings.srcdir / 'index.json', 'rt') as file_handle: |
190 |
obj = json.loads(build.strip_json_comments(file_handle.read()))
|
|
191 |
obj = json.loads(hydrilla_util.strip_json_comments(file_handle.read()))
|
|
191 | 192 |
|
192 | 193 |
contents = modify_cb(settings, obj) |
193 | 194 |
|
... | ... | |
438 | 439 |
|
439 | 440 |
with pytest.raises(error_type): |
440 | 441 |
build.Build(settings.srcdir, settings.index_json_path)\ |
441 |
.write_package_files(dstdir) |
|
442 |
.write_package_files(dstdir) |
Also available in: Unified diff
move to a namespace package under 'hydrilla'