Blame SOURCES/check-pyc-timestamps.py

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