2024-07-14 15:55:23 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from setuptools import Command, setup
|
2024-08-26 15:52:32 +02:00
|
|
|
from setuptools.command.build import build as OriginalBuildCommand
|
2024-07-14 15:55:23 +02:00
|
|
|
|
2024-08-26 15:52:32 +02:00
|
|
|
class BuildCommand(OriginalBuildCommand):
|
|
|
|
sub_commands = [('build_mo', None), *OriginalBuildCommand.sub_commands]
|
2024-07-14 15:55:23 +02:00
|
|
|
|
|
|
|
|
2024-08-26 15:52:32 +02:00
|
|
|
class BuildMoSubCommand(Command):
|
2024-07-14 15:55:23 +02:00
|
|
|
description = 'Compile .po files into .mo files'
|
|
|
|
|
|
|
|
files = None
|
|
|
|
|
|
|
|
def initialize_options(self):
|
|
|
|
self.files = None
|
|
|
|
self.editable_mode = False
|
|
|
|
self.build_lib = None
|
|
|
|
|
|
|
|
def finalize_options(self):
|
|
|
|
self.set_undefined_options("build_py", ("build_lib", "build_lib"))
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
files = self._get_files()
|
|
|
|
|
|
|
|
for build_file, source_file in files.items():
|
|
|
|
if not self.editable_mode:
|
|
|
|
# Parent directory required for msgfmt to work correctly
|
|
|
|
Path(build_file).parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
self.spawn(['msgfmt', source_file, '-o', build_file])
|
|
|
|
|
|
|
|
def _get_files(self):
|
|
|
|
if self.files is not None:
|
|
|
|
return self.files
|
|
|
|
|
|
|
|
files = {}
|
|
|
|
|
2024-08-26 16:25:32 +02:00
|
|
|
localedir = Path('safeeyes/config/locale')
|
|
|
|
po_dirs = [l.joinpath('LC_MESSAGES') for l in localedir.iterdir() if l.is_dir()]
|
2024-07-14 15:55:23 +02:00
|
|
|
for po_dir in po_dirs:
|
|
|
|
po_files = [f
|
2024-08-26 16:25:32 +02:00
|
|
|
for f in po_dir.iterdir()
|
|
|
|
if f.is_file() and f.suffix == '.po']
|
2024-07-14 15:55:23 +02:00
|
|
|
for po_file in po_files:
|
2024-08-26 16:25:32 +02:00
|
|
|
mo_file = po_file.with_suffix(".mo")
|
2024-07-14 15:55:23 +02:00
|
|
|
|
2024-08-26 16:25:32 +02:00
|
|
|
source_file = po_file
|
|
|
|
build_file = mo_file
|
2024-07-14 15:55:23 +02:00
|
|
|
|
|
|
|
if not self.editable_mode:
|
2024-08-26 16:25:32 +02:00
|
|
|
build_file = Path(self.build_lib).joinpath(build_file)
|
2024-07-14 15:55:23 +02:00
|
|
|
|
2024-08-26 16:25:32 +02:00
|
|
|
files[str(build_file)] = str(source_file)
|
2024-07-14 15:55:23 +02:00
|
|
|
|
|
|
|
self.files = files
|
|
|
|
return files
|
|
|
|
|
|
|
|
def get_output_mapping(self):
|
|
|
|
return self._get_files()
|
|
|
|
|
|
|
|
def get_outputs(self):
|
|
|
|
return self._get_files().keys()
|
|
|
|
|
|
|
|
def get_source_files(self):
|
|
|
|
return self._get_files().values()
|
|
|
|
|
|
|
|
|
|
|
|
setup(
|
2024-08-26 15:52:32 +02:00
|
|
|
cmdclass={'build': BuildCommand, 'build_mo': BuildMoSubCommand}
|
2017-03-10 23:35:06 +01:00
|
|
|
)
|