diff options
author | Eli Schwartz <eschwartz@archlinux.org> | 2021-10-31 20:23:23 -0400 |
---|---|---|
committer | Eli Schwartz <eschwartz@archlinux.org> | 2022-01-10 18:36:57 -0500 |
commit | 140097faf0eddcc7819a3353eb7c21b82a7df1e0 (patch) | |
tree | b70acd7f70f5431a06db73f3db23f86ff896c3b3 /tools/gen_data.py | |
parent | be6e09bfdb5c911eecbc976ed71d427f052a1b81 (diff) | |
download | meson-140097faf0eddcc7819a3353eb7c21b82a7df1e0.zip meson-140097faf0eddcc7819a3353eb7c21b82a7df1e0.tar.gz meson-140097faf0eddcc7819a3353eb7c21b82a7df1e0.tar.bz2 |
port from embedded data to importlib.resources
Diffstat (limited to 'tools/gen_data.py')
-rwxr-xr-x | tools/gen_data.py | 140 |
1 files changed, 0 insertions, 140 deletions
diff --git a/tools/gen_data.py b/tools/gen_data.py deleted file mode 100755 index b1c62e0..0000000 --- a/tools/gen_data.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python3 - -# Copyright 2020 Daniel Mensinger - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import sys -import hashlib -import textwrap -import re -from pathlib import Path -from datetime import datetime -import typing as T - -class DataFile: - file_counter = 0 - - def __init__(self, path: Path, root: Path): - self.path = path - self.id = self.path.relative_to(root) - self.data_str = f'file_{DataFile.file_counter}_data_' + re.sub('[^a-zA-Z0-9]', '_', self.path.name) - DataFile.file_counter += 1 - - b = self.path.read_bytes() - self.data = b.decode() - self.sha256sum = hashlib.sha256(b).hexdigest() - - def __repr__(self) -> str: - return f'<{type(self).__name__}: [{self.sha256sum}] {self.id}>' - -def main() -> int: - root_dir = Path(__file__).resolve().parents[1] - mesonbuild_dir = root_dir / 'mesonbuild' - out_file = mesonbuild_dir / 'mesondata.py' - - data_dirs = sorted(mesonbuild_dir.glob('**/data')) - - data_files: T.List[DataFile] = [] - - for d in data_dirs: - for p in sorted(d.iterdir()): - data_files += [DataFile(p, mesonbuild_dir)] - - print(f'Found {len(data_files)} data files') - - # Generate the data script - data = '' - - data += textwrap.dedent(f'''\ - # Copyright {datetime.today().year} The Meson development team - - # Licensed under the Apache License, Version 2.0 (the "License"); - # you may not use this file except in compliance with the License. - # You may obtain a copy of the License at - - # http://www.apache.org/licenses/LICENSE-2.0 - - # Unless required by applicable law or agreed to in writing, software - # distributed under the License is distributed on an "AS IS" BASIS, - # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - # See the License for the specific language governing permissions and - # limitations under the License. - - - #### - #### WARNING: This is an automatically generated file! Do not edit! - #### Generated by {Path(__file__).resolve().relative_to(root_dir)} - #### - - - # TODO: Remember to remove this also from tools/gen_data.py - from pathlib import Path - import typing as T - - if T.TYPE_CHECKING: - from .environment import Environment - - ###################### - # BEGIN Data section # - ###################### - - ''') - - for i in data_files: - data += f"{i.data_str} = '''\\\n{i.data}'''\n\n" - - data += textwrap.dedent(f''' - #################### - # END Data section # - #################### - - class DataFile: - def __init__(self, path: Path, sha256sum: str, data: str) -> None: - self.path = path - self.sha256sum = sha256sum - self.data = data - - def write_once(self, path: Path) -> None: - if not path.exists(): - path.write_text(self.data, encoding='utf-8') - - def write_to_private(self, env: 'Environment') -> Path: - out_file = Path(env.scratch_dir) / 'data' / self.path.name - out_file.parent.mkdir(exist_ok=True) - self.write_once(out_file) - return out_file - - - mesondata = {{ - ''') - - for i in data_files: - data += textwrap.indent(textwrap.dedent(f"""\ - '{i.id}': DataFile( - Path('{i.id}'), - '{i.sha256sum}', - {i.data_str}, - ), - """), ' ') - - data += textwrap.dedent('''\ - } - ''') - - print(f'Updating {out_file}') - out_file.write_text(data, encoding='utf-8') - return 0 - -if __name__ == '__main__': - sys.exit(main()) |