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 |
Guido van Rossum | 19b23e5 | 1998-10-07 19:45:33 +0000 | [diff] [blame] | 8 | import imp |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 9 | |
| 10 | def main(): |
| 11 | silent = 0 |
| 12 | verbose = 0 |
| 13 | if sys.argv[1:]: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 14 | if sys.argv[1] == '-v': |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 15 | verbose = 1 |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 16 | elif sys.argv[1] == '-s': |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 17 | silent = 1 |
Guido van Rossum | 19b23e5 | 1998-10-07 19:45:33 +0000 | [diff] [blame] | 18 | MAGIC = imp.get_magic() |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 19 | if not silent: |
| 20 | print 'Using MAGIC word', `MAGIC` |
| 21 | for dirname in sys.path: |
| 22 | try: |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 23 | names = os.listdir(dirname) |
| 24 | except os.error: |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 25 | print 'Cannot list directory', `dirname` |
| 26 | continue |
| 27 | if not silent: |
| 28 | print 'Checking', `dirname`, '...' |
| 29 | names.sort() |
| 30 | for name in names: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 31 | if name[-3:] == '.py': |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 32 | name = os.path.join(dirname, name) |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 33 | try: |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 34 | st = os.stat(name) |
| 35 | except os.error: |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 36 | print 'Cannot stat', `name` |
| 37 | continue |
| 38 | if verbose: |
| 39 | print 'Check', `name`, '...' |
| 40 | name_c = name + 'c' |
| 41 | try: |
| 42 | f = open(name_c, 'r') |
| 43 | except IOError: |
| 44 | print 'Cannot open', `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 `name_c` |
| 52 | continue |
| 53 | mtime = get_long(mtime_str) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 54 | if mtime == 0 or mtime == -1: |
Guido van Rossum | 5378d5c | 1991-12-18 13:38:27 +0000 | [diff] [blame] | 55 | print 'Bad ".pyc" file', `name_c` |
| 56 | elif mtime <> st[ST_MTIME]: |
| 57 | print 'Out-of-date ".pyc" file', |
| 58 | print `name_c` |
| 59 | |
| 60 | def 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 | |
| 65 | main() |