Blame SOURCES/check-pyc-timestamps.py

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