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

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