python3.11/check-pyc-timestamps.py
Tomas Orsava b745af58b1 Add the import_all_modules_py3.11.py file for the python3.11-rpm-macros subpackage
Resolves: rhbz#2207631

- The script is copied veratim from the current Fedora version.
- The script is namespaced with the `_py3_11` version, because it's only used for Python 3.11 at this point, and when we're adding the Python 3.12 stack, we don't want to name clash with it. The script might be different at that point, and also we can't depend on Python 3.11 macros from Python 3.12 macros.
- The script is excluded from bytecompilation, because it's run but never imported, and we don't want to pollute the `/usr/lib/rpm/redhat/` directory.
2023-08-11 13:37:36 +02:00

57 lines
1.6 KiB
Python

"""Checks if all *.pyc files have later mtime than their *.py files."""
import os
import sys
from importlib.util import cache_from_source
from pathlib import Path
RPM_BUILD_ROOT = os.environ.get('RPM_BUILD_ROOT', '')
# ...cpython-3X.pyc
# ...cpython-3X.opt-1.pyc
# ...cpython-3X.opt-2.pyc
LEVELS = (None, 1, 2)
# list of globs of test and other files that we expect not to have bytecode
not_compiled = [
'/usr/bin/*',
'/usr/lib/rpm/redhat/*',
'*/test/bad_coding.py',
'*/test/bad_coding2.py',
'*/test/badsyntax_*.py',
'*/lib2to3/tests/data/bom.py',
'*/lib2to3/tests/data/crlf.py',
'*/lib2to3/tests/data/different_encoding.py',
'*/lib2to3/tests/data/false_encoding.py',
'*/lib2to3/tests/data/py2_test_grammar.py',
'*.debug-gdb.py',
]
def bytecode_expected(path):
path = Path(path[len(RPM_BUILD_ROOT):])
for glob in not_compiled:
if path.match(glob):
return False
return True
failed = 0
compiled = (path for path in sys.argv[1:] if bytecode_expected(path))
for path in compiled:
to_check = (cache_from_source(path, optimization=opt) for opt in LEVELS)
f_mtime = os.path.getmtime(path)
for pyc in to_check:
c_mtime = os.path.getmtime(pyc)
if c_mtime < f_mtime:
print('Failed bytecompilation timestamps check: '
f'Bytecode file {pyc} is older than source file {path}',
file=sys.stderr)
failed += 1
if failed:
print(f'\n{failed} files failed bytecompilation timestamps check.',
file=sys.stderr)
sys.exit(1)