churchyard / rpms / python3

Forked from rpms/python3 3 years ago
Clone

Blame check-pyc-timestamps.py

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