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