• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env python
2# Check that all ".pyc" files exist and are up-to-date
3# Uses module 'os'
4
5import sys
6import os
7from stat import ST_MTIME
8import imp
9
10def main():
11    silent = 0
12    verbose = 0
13    if sys.argv[1:]:
14        if sys.argv[1] == '-v':
15            verbose = 1
16        elif sys.argv[1] == '-s':
17            silent = 1
18    MAGIC = imp.get_magic()
19    if not silent:
20        print 'Using MAGIC word', repr(MAGIC)
21    for dirname in sys.path:
22        try:
23            names = os.listdir(dirname)
24        except os.error:
25            print 'Cannot list directory', repr(dirname)
26            continue
27        if not silent:
28            print 'Checking ', repr(dirname), '...'
29        names.sort()
30        for name in names:
31            if name[-3:] == '.py':
32                name = os.path.join(dirname, name)
33                try:
34                    st = os.stat(name)
35                except os.error:
36                    print 'Cannot stat', repr(name)
37                    continue
38                if verbose:
39                    print 'Check', repr(name), '...'
40                name_c = name + 'c'
41                try:
42                    f = open(name_c, 'r')
43                except IOError:
44                    print 'Cannot open', repr(name_c)
45                    continue
46                magic_str = f.read(4)
47                mtime_str = f.read(4)
48                f.close()
49                if magic_str <> MAGIC:
50                    print 'Bad MAGIC word in ".pyc" file',
51                    print repr(name_c)
52                    continue
53                mtime = get_long(mtime_str)
54                if mtime == 0 or mtime == -1:
55                    print 'Bad ".pyc" file', repr(name_c)
56                elif mtime <> st[ST_MTIME]:
57                    print 'Out-of-date ".pyc" file',
58                    print repr(name_c)
59
60def get_long(s):
61    if len(s) <> 4:
62        return -1
63    return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
64
65if __name__ == '__main__':
66    main()
67