Blame SOURCES/check-pyc-and-pyo-timestamps.py

dd3773
"""Checks if all *.pyc and *.pyo files have later mtime than their *.py files."""
dd3773
dd3773
import importlib.util
dd3773
import os
dd3773
import sys
dd3773
dd3773
# list of test and other files that we expect not to have bytecode
dd3773
not_compiled = [
dd3773
    '/usr/bin/pathfix.py',
dd3773
    'test/bad_coding.py',
dd3773
    'test/bad_coding2.py',
dd3773
    'test/badsyntax_3131.py',
dd3773
    'test/badsyntax_future3.py',
dd3773
    'test/badsyntax_future4.py',
dd3773
    'test/badsyntax_future5.py',
dd3773
    'test/badsyntax_future6.py',
dd3773
    'test/badsyntax_future7.py',
dd3773
    'test/badsyntax_future8.py',
dd3773
    'test/badsyntax_future9.py',
dd3773
    'test/badsyntax_future10.py',
dd3773
    'test/badsyntax_async1.py',
dd3773
    'test/badsyntax_async2.py',
dd3773
    'test/badsyntax_async3.py',
dd3773
    'test/badsyntax_async4.py',
dd3773
    'test/badsyntax_async5.py',
dd3773
    'test/badsyntax_async6.py',
dd3773
    'test/badsyntax_async7.py',
dd3773
    'test/badsyntax_async8.py',
dd3773
    'test/badsyntax_async9.py',
dd3773
    'test/badsyntax_pep3120.py',
dd3773
    'lib2to3/tests/data/bom.py',
dd3773
    'lib2to3/tests/data/crlf.py',
dd3773
    'lib2to3/tests/data/different_encoding.py',
dd3773
    'lib2to3/tests/data/false_encoding.py',
dd3773
    'lib2to3/tests/data/py2_test_grammar.py',
dd3773
    '.debug-gdb.py',
dd3773
]
dd3773
failed = 0
dd3773
dd3773
dd3773
def bytecode_expected(source):
dd3773
    for f in not_compiled:
dd3773
        if source.endswith(f):
dd3773
            return False
dd3773
    return True
dd3773
dd3773
dd3773
compiled = filter(lambda f: bytecode_expected(f), sys.argv[1:])
dd3773
for f in compiled:
dd3773
    # check both pyo and pyc
dd3773
    to_check = map(lambda b: importlib.util.cache_from_source(f, b), (True, False))
dd3773
    f_mtime = os.path.getmtime(f)
dd3773
    for c in to_check:
dd3773
        c_mtime = os.path.getmtime(c)
dd3773
        if c_mtime < f_mtime:
dd3773
            sys.stderr.write('Failed bytecompilation timestamps check: ')
dd3773
            sys.stderr.write('Bytecode file {} is older than source file {}.\n'.format(c, f))
dd3773
            failed += 1
dd3773
dd3773
if failed:
dd3773
    sys.stderr.write('\n{} files failed bytecompilation timestamps check.\n'.format(failed))
dd3773
    sys.exit(1)