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