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