blob: 6df0472b28358c1c24d5927e36d718c76782fe77 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum5378d5c1991-12-18 13:38:27 +00002# Check that all ".pyc" files exist and are up-to-date
Guido van Rossum9a6757d1992-03-30 11:15:26 +00003# Uses module 'os'
Guido van Rossum5378d5c1991-12-18 13:38:27 +00004
5import sys
Guido van Rossum9a6757d1992-03-30 11:15:26 +00006import os
Guido van Rossum5378d5c1991-12-18 13:38:27 +00007from stat import ST_MTIME
8
9def main():
10 silent = 0
11 verbose = 0
12 if sys.argv[1:]:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000013 if sys.argv[1] == '-v':
Guido van Rossum5378d5c1991-12-18 13:38:27 +000014 verbose = 1
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000015 elif sys.argv[1] == '-s':
Guido van Rossum5378d5c1991-12-18 13:38:27 +000016 silent = 1
17 MAGIC = '\0\0\0\0'
18 try:
19 if sys.version[:5] >= '0.9.4':
20 MAGIC = '\224\224\224\0'
21 except:
22 pass
23 if not silent:
24 print 'Using MAGIC word', `MAGIC`
25 for dirname in sys.path:
26 try:
Guido van Rossum9a6757d1992-03-30 11:15:26 +000027 names = os.listdir(dirname)
28 except os.error:
Guido van Rossum5378d5c1991-12-18 13:38:27 +000029 print 'Cannot list directory', `dirname`
30 continue
31 if not silent:
32 print 'Checking', `dirname`, '...'
33 names.sort()
34 for name in names:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000035 if name[-3:] == '.py':
Guido van Rossum9a6757d1992-03-30 11:15:26 +000036 name = os.path.join(dirname, name)
Guido van Rossum5378d5c1991-12-18 13:38:27 +000037 try:
Guido van Rossum9a6757d1992-03-30 11:15:26 +000038 st = os.stat(name)
39 except os.error:
Guido van Rossum5378d5c1991-12-18 13:38:27 +000040 print 'Cannot stat', `name`
41 continue
42 if verbose:
43 print 'Check', `name`, '...'
44 name_c = name + 'c'
45 try:
46 f = open(name_c, 'r')
47 except IOError:
48 print 'Cannot open', `name_c`
49 continue
50 magic_str = f.read(4)
51 mtime_str = f.read(4)
52 f.close()
53 if magic_str <> MAGIC:
54 print 'Bad MAGIC word in ".pyc" file',
55 print `name_c`
56 continue
57 mtime = get_long(mtime_str)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000058 if mtime == 0 or mtime == -1:
Guido van Rossum5378d5c1991-12-18 13:38:27 +000059 print 'Bad ".pyc" file', `name_c`
60 elif mtime <> st[ST_MTIME]:
61 print 'Out-of-date ".pyc" file',
62 print `name_c`
63
64def get_long(s):
65 if len(s) <> 4:
66 return -1
67 return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
68
69main()