Blame SOURCES/check-pyc-timestamps.py

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