4 -#!/usr/bin/env python3
6 -# SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. <https://fsfe.org>
7 -# SPDX-FileCopyrightText: 2022 Carmen Bianca Bakker <carmenbianca@fsfe.org>
9 -# SPDX-License-Identifier: GPL-3.0-or-later
11 -"""Script called by poetry. The API used by poetry is unstable, but let's hope
12 -this stays functional.
19 -from pathlib import Path
20 -from warnings import warn
22 -from setuptools import Distribution
23 -from setuptools.command.build_py import build_py
25 -# pylint: disable=attribute-defined-outside-init
28 -class Build(build_py):
29 - """Redefined build."""
31 - def initialize_options(self):
32 - super().initialize_options()
33 - self.po_files = None
35 - self.mo_outputs = []
37 - def finalize_options(self):
38 - super().finalize_options()
39 - self.po_files = glob.glob("po/*.po")
40 - for msgfmt in ["msgfmt", "msgfmt.py", "msgfmt3.py"]:
41 - self.msgfmt = shutil.which(msgfmt)
48 - for po_file in self.po_files:
49 - self.announce(f"compiling {po_file}")
51 - Path(self.build_lib)
53 - / Path(po_file).stem
56 - destination = str(Path(lang_dir) / "reuse.mo")
57 - compile_func = lambda msgfmt, in_file, out: subprocess.run(
58 - [msgfmt, in_file, "-o", out],
62 - self.mkpath(lang_dir)
67 - (self.msgfmt, po_file, destination),
69 - self.mo_outputs.append(destination)
72 - warn("msgfmt is not installed. Translations will not be included.")
74 - def get_outputs(self, include_bytecode=1):
76 - super().get_outputs(include_bytecode=include_bytecode)
82 - """Main function that runs the compilation."""
83 - distribution = Distribution(
85 - "package_dir": {"": "src"},
88 - cmd = Build(distribution)
90 - cmd.ensure_finalized()
93 - # Copy into src/. This appears to be the thing that actually does all the
94 - # heavy lifting. I'm not sure why I'm bothering with all the
95 - # setuptools-specific logic above.
97 - # In summary: Get .mo files from build directory and put them into
98 - # src/reuse/locale/{lang}/LC_MESSAGES/reuse.mo.
99 - for output in cmd.get_outputs():
100 - relative = Path("src") / os.path.relpath(output, cmd.build_lib)
101 - relative.parent.mkdir(parents=True, exist_ok=True)
102 - shutil.copyfile(output, relative)
105 -if __name__ == "__main__":
109 @@ -68,7 +68,6 @@ reuse = 'reuse._main:main'
112 generate-setup-file = false
116 requires = ["poetry-core>=1.1.0", "setuptools"]