summaryrefslogtreecommitdiffstats
path: root/check-pyc-and-pyo-timestamps.py
blob: 5fd11cc9a6509e77825e57fbf8b66e2d1c1d6a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Checks if all *.pyc and *.pyo files have later mtime than their *.py files."""

import imp
import os
import sys

# list of test and other files that we expect not to have bytecode
not_compiled = [
    'test/bad_coding.py',
    'test/bad_coding2.py',
    'test/badsyntax_3131.py',
    'test/badsyntax_future3.py',
    'test/badsyntax_future4.py',
    'test/badsyntax_future5.py',
    'test/badsyntax_future6.py',
    'test/badsyntax_future7.py',
    'test/badsyntax_future8.py',
    'test/badsyntax_future9.py',
    'test/badsyntax_future10.py',
    'test/badsyntax_pep3120.py',
    'lib2to3/tests/data/bom.py',
    'lib2to3/tests/data/crlf.py',
    'lib2to3/tests/data/different_encoding.py',
    'lib2to3/tests/data/false_encoding.py',
    'lib2to3/tests/data/py2_test_grammar.py',
    '.debug-gdb.py',
]
failed = 0

def bytecode_expected(source):
    for f in not_compiled:
        if source.endswith(f):
            return False
    return True

compiled = filter(lambda f: bytecode_expected(f), sys.argv[1:])
for f in compiled:
    # check both pyo and pyc
    to_check = map(lambda b: imp.cache_from_source(f, b), (True, False))
    f_mtime = os.path.getmtime(f)
    for c in to_check:
        c_mtime = os.path.getmtime(c)
        if c_mtime < f_mtime:
            sys.stderr.write('Failed bytecompilation timestamps check: ')
            sys.stderr.write('Bytecode file {} is older than source file {}.\n'.format(c, f))
            failed += 1

if failed:
    sys.stderr.write('\n{} files failed bytecompilation timestamps check.\n'.format(failed))
    sys.exit(1)