Automatically mark language files with %lang macro

This commit is contained in:
Tomas Hrnciar 2020-08-11 14:12:34 +02:00 committed by Miro Hrončok
parent a613e176e3
commit da3d9bc23d
6 changed files with 6725 additions and 1 deletions

View File

@ -203,6 +203,13 @@ However, in Fedora packages, always list executables explicitly to avoid uninten
%license LICENSE %license LICENSE
%{_bindir}/downloader %{_bindir}/downloader
`%pyproject_save_files` also automatically recognizes language (`*.mo`) files and marks them with `%lang` macro and appropriate language code.
Note that RPM might warn about such files listed twice:
warning: File listed twice: /usr/lib/python3.9/site-packages/django/conf/locale/af/LC_MESSAGES/django.mo
The warning is harmless.
Limitations Limitations
----------- -----------

View File

@ -6,7 +6,7 @@ License: MIT
# Keep the version at zero and increment only release # Keep the version at zero and increment only release
Version: 0 Version: 0
Release: 25%{?dist} Release: 26%{?dist}
# Macro files # Macro files
Source001: macros.pyproject Source001: macros.pyproject
@ -88,6 +88,10 @@ export HOSTNAME="rpmbuild" # to speedup tox in network-less mock, see rhbz#1856
%license LICENSE %license LICENSE
%changelog %changelog
* Mon Aug 24 2020 Tomas Hrnciar <thrnciar@redhat.com> - 0-26
- Implement automatic detection of %%lang files in %%pyproject_save_files
and mark them with %%lang in filelist
* Fri Aug 14 2020 Miro Hrončok <mhroncok@redhat.com> - 0-25 * Fri Aug 14 2020 Miro Hrončok <mhroncok@redhat.com> - 0-25
- Handle Python Extras in %%pyproject_buildrequires on Fedora 33+ - Handle Python Extras in %%pyproject_buildrequires on Fedora 33+

View File

@ -165,6 +165,21 @@ def add_file_to_module(paths, module_name, module_type, *files):
{"type": module_type, "files": list(files)} {"type": module_type, "files": list(files)}
) )
def add_lang_to_module(paths, module_name, path):
"""
Helper procedure, divides lang files by language and adds them to the module_name
"""
for i, parent in enumerate(path.parents):
if i > 0 and parent.name == 'locale':
lang_country_code = path.parents[i-1].name
break
else:
return
# convert potential en_US to plain en
lang_code = lang_country_code.partition('_')[0]
if module_name not in paths["lang"]:
paths["lang"].update({module_name: defaultdict(list)})
paths["lang"][module_name][lang_code].append(path)
def classify_paths( def classify_paths(
record_path, parsed_record_content, sitedirs, python_version record_path, parsed_record_content, sitedirs, python_version
@ -185,6 +200,7 @@ def classify_paths(
"docs": [], # to be used once there is upstream way to recognize READMEs "docs": [], # to be used once there is upstream way to recognize READMEs
"licenses": [], # to be used once there is upstream way to recognize LICENSEs "licenses": [], # to be used once there is upstream way to recognize LICENSEs
}, },
"lang": {}, # %lang entries: [module_name or None][language_code] lists of .mo files
"modules": defaultdict(list), # each importable module (directory, .py, .so) "modules": defaultdict(list), # each importable module (directory, .py, .so)
"other": {"files": []}, # regular %file entries we could not parse :( "other": {"files": []}, # regular %file entries we could not parse :(
} }
@ -221,10 +237,14 @@ def classify_paths(
index = path.parents.index(sitedir) index = path.parents.index(sitedir)
module_dir = path.parents[index - 1] module_dir = path.parents[index - 1]
add_file_to_module(paths, module_dir.name, "package", module_dir) add_file_to_module(paths, module_dir.name, "package", module_dir)
if path.suffix == ".mo":
add_lang_to_module(paths, module_dir.name, path)
break break
else: else:
warnings.warn(f"Unrecognized file: {path}") warnings.warn(f"Unrecognized file: {path}")
paths["other"]["files"].append(path) paths["other"]["files"].append(path)
if path.suffix == ".mo":
add_lang_to_module(paths, None, path)
return paths return paths
@ -244,6 +264,10 @@ def generate_file_list(paths_dict, module_globs, include_others=False):
if include_others: if include_others:
files.update(f"{p}" for p in paths_dict["other"]["files"]) files.update(f"{p}" for p in paths_dict["other"]["files"])
try:
files.update(f"%lang({lang_code}) {path}" for path in paths_dict["lang"][None][lang_code])
except KeyError:
pass
files.update(f"{p}" for p in paths_dict["metadata"]["files"]) files.update(f"{p}" for p in paths_dict["metadata"]["files"])
for macro in "dir", "doc", "license": for macro in "dir", "doc", "license":
@ -257,6 +281,11 @@ def generate_file_list(paths_dict, module_globs, include_others=False):
for name in modules: for name in modules:
if fnmatch.fnmatchcase(name, glob): if fnmatch.fnmatchcase(name, glob):
if name not in done_modules: if name not in done_modules:
try:
for lang_code in paths_dict["lang"][name]:
files.update(f"%lang({lang_code}) {path}" for path in paths_dict["lang"][name][lang_code])
except KeyError:
pass
for module in modules[name]: for module in modules[name]:
if module["type"] == "package": if module["type"] == "package":
files.update(f"{p}/" for p in module["files"]) files.update(f"{p}/" for p in module["files"])

File diff suppressed because it is too large Load Diff

67
tests/python-django.spec Normal file
View File

@ -0,0 +1,67 @@
Name: python-django
Version: 3.0.7
Release: 0%{?dist}
Summary: A high-level Python Web framework
License: BSD
URL: https://www.djangoproject.com/
Source0: %{pypi_source Django}
BuildArch: noarch
BuildRequires: pyproject-rpm-macros
BuildRequires: python3-devel
%description
This package contains lang files.
Building this tests that lang files are marked with %%lang in filelist.
%package -n python3-django
Summary: %{summary}
%description -n python3-django
...
%prep
%autosetup -p1 -n Django-%{version}
%py3_shebang_fix django/conf/project_template/manage.py-tpl django/bin/django-admin.py
%if 0%{?fedora} < 32 && 0%{?rhel} < 9
# Python RPM dependency generator doesn't support ~= yet
# https://bugzilla.redhat.com/show_bug.cgi?id=1758141
sed -i 's/asgiref ~= /asgiref >= /' setup.py
%endif
%generate_buildrequires
%pyproject_buildrequires
%build
%pyproject_wheel
%install
%pyproject_install
%pyproject_save_files django
# remove .po files
find %{buildroot} -name "*.po" | xargs rm -f
%check
# Internal check if generated lang entries are same as
# the ones generated using %%find_lang
%find_lang django
%find_lang djangojs
grep '^%%lang' %{pyproject_files} | sort > tested.lang
sort django.lang djangojs.lang > expected.lang
diff tested.lang expected.lang
%files -n python3-django -f %{pyproject_files}
%doc README.rst
%license LICENSE
%{_bindir}/django-admin
%{_bindir}/django-admin.py

View File

@ -55,6 +55,9 @@
- zope: - zope:
dir: . dir: .
run: ./mocktest.sh python-zope-event run: ./mocktest.sh python-zope-event
- django:
dir: .
run: ./mocktest.sh python-django
required_packages: required_packages:
- mock - mock
- rpmdevtools - rpmdevtools