Blame SOURCES/check-pyc-timestamps.py

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