Blame SOURCES/check-pyc-timestamps.py

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