Blame SOURCES/check-pyc-timestamps.py

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