Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 1 | # Check that all ".pyc" files exist and are up-to-date |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame^] | 2 | # Uses module 'os' |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 3 | |
| 4 | import sys |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame^] | 5 | import os |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 6 | from stat import ST_MTIME |
| 7 | |
| 8 | def main(): |
| 9 | silent = 0 |
| 10 | verbose = 0 |
| 11 | if sys.argv[1:]: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 12 | if sys.argv[1] == '-v': |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 13 | verbose = 1 |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 14 | elif sys.argv[1] == '-s': |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 15 | silent = 1 |
| 16 | MAGIC = '\0\0\0\0' |
| 17 | try: |
| 18 | if sys.version[:5] >= '0.9.4': |
| 19 | MAGIC = '\224\224\224\0' |
| 20 | except: |
| 21 | pass |
| 22 | if not silent: |
| 23 | print 'Using MAGIC word', `MAGIC` |
| 24 | for dirname in sys.path: |
| 25 | try: |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame^] | 26 | names = os.listdir(dirname) |
| 27 | except os.error: |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 28 | print 'Cannot list directory', `dirname` |
| 29 | continue |
| 30 | if not silent: |
| 31 | print 'Checking', `dirname`, '...' |
| 32 | names.sort() |
| 33 | for name in names: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 34 | if name[-3:] == '.py': |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame^] | 35 | name = os.path.join(dirname, name) |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 36 | try: |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame^] | 37 | st = os.stat(name) |
| 38 | except os.error: |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 39 | print 'Cannot stat', `name` |
| 40 | continue |
| 41 | if verbose: |
| 42 | print 'Check', `name`, '...' |
| 43 | name_c = name + 'c' |
| 44 | try: |
| 45 | f = open(name_c, 'r') |
| 46 | except IOError: |
| 47 | print 'Cannot open', `name_c` |
| 48 | continue |
| 49 | magic_str = f.read(4) |
| 50 | mtime_str = f.read(4) |
| 51 | f.close() |
| 52 | if magic_str <> MAGIC: |
| 53 | print 'Bad MAGIC word in ".pyc" file', |
| 54 | print `name_c` |
| 55 | continue |
| 56 | mtime = get_long(mtime_str) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 57 | if mtime == 0 or mtime == -1: |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 58 | print 'Bad ".pyc" file', `name_c` |
| 59 | elif mtime <> st[ST_MTIME]: |
| 60 | print 'Out-of-date ".pyc" file', |
| 61 | print `name_c` |
| 62 | |
| 63 | def get_long(s): |
| 64 | if len(s) <> 4: |
| 65 | return -1 |
| 66 | return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24) |
| 67 | |
| 68 | main() |