Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame^] | 1 | #! /usr/bin/env python |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 2 | # Check that all ".pyc" files exist and are up-to-date |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 3 | # Uses module 'os' |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 4 | |
| 5 | import sys |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 6 | import os |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 7 | from stat import ST_MTIME |
| 8 | |
| 9 | def main(): |
| 10 | silent = 0 |
| 11 | verbose = 0 |
| 12 | if sys.argv[1:]: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 13 | if sys.argv[1] == '-v': |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 14 | verbose = 1 |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 15 | elif sys.argv[1] == '-s': |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 16 | 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 Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 27 | names = os.listdir(dirname) |
| 28 | except os.error: |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 29 | 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 Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 35 | if name[-3:] == '.py': |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 36 | name = os.path.join(dirname, name) |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 37 | try: |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 38 | st = os.stat(name) |
| 39 | except os.error: |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 40 | 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 Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 58 | if mtime == 0 or mtime == -1: |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 59 | print 'Bad ".pyc" file', `name_c` |
| 60 | elif mtime <> st[ST_MTIME]: |
| 61 | print 'Out-of-date ".pyc" file', |
| 62 | print `name_c` |
| 63 | |
| 64 | def 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 | |
| 69 | main() |