Blame check-pyc-timestamps.py

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