Blame SOURCES/check-pyc-timestamps.py

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