Blame SOURCES/check-pyc-timestamps.py

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