cpython/Tools/scripts/checkpyc.py

71 lines
1.5 KiB
Python
Raw Normal View History

1992-05-19 10:50:10 -03:00
#! /ufs/guido/bin/sgi/python
1993-04-01 16:50:35 -04:00
#! /usr/local/bin/python
1991-12-18 09:38:27 -04:00
# Check that all ".pyc" files exist and are up-to-date
1992-03-30 07:15:26 -04:00
# Uses module 'os'
1991-12-18 09:38:27 -04:00
import sys
1992-03-30 07:15:26 -04:00
import os
1991-12-18 09:38:27 -04:00
from stat import ST_MTIME
def main():
silent = 0
verbose = 0
if sys.argv[1:]:
1992-01-01 15:35:13 -04:00
if sys.argv[1] == '-v':
1991-12-18 09:38:27 -04:00
verbose = 1
1992-01-01 15:35:13 -04:00
elif sys.argv[1] == '-s':
1991-12-18 09:38:27 -04:00
silent = 1
MAGIC = '\0\0\0\0'
try:
if sys.version[:5] >= '0.9.4':
MAGIC = '\224\224\224\0'
except:
pass
if not silent:
print 'Using MAGIC word', `MAGIC`
for dirname in sys.path:
try:
1992-03-30 07:15:26 -04:00
names = os.listdir(dirname)
except os.error:
1991-12-18 09:38:27 -04:00
print 'Cannot list directory', `dirname`
continue
if not silent:
print 'Checking', `dirname`, '...'
names.sort()
for name in names:
1992-01-01 15:35:13 -04:00
if name[-3:] == '.py':
1992-03-30 07:15:26 -04:00
name = os.path.join(dirname, name)
1991-12-18 09:38:27 -04:00
try:
1992-03-30 07:15:26 -04:00
st = os.stat(name)
except os.error:
1991-12-18 09:38:27 -04:00
print 'Cannot stat', `name`
continue
if verbose:
print 'Check', `name`, '...'
name_c = name + 'c'
try:
f = open(name_c, 'r')
except IOError:
print 'Cannot open', `name_c`
continue
magic_str = f.read(4)
mtime_str = f.read(4)
f.close()
if magic_str <> MAGIC:
print 'Bad MAGIC word in ".pyc" file',
print `name_c`
continue
mtime = get_long(mtime_str)
1992-01-01 15:35:13 -04:00
if mtime == 0 or mtime == -1:
1991-12-18 09:38:27 -04:00
print 'Bad ".pyc" file', `name_c`
elif mtime <> st[ST_MTIME]:
print 'Out-of-date ".pyc" file',
print `name_c`
def get_long(s):
if len(s) <> 4:
return -1
return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
main()