2010-03-11 18:53:45 -04:00
|
|
|
#! /usr/bin/env python3
|
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
|
1998-10-07 16:45:33 -03:00
|
|
|
import imp
|
1991-12-18 09:38:27 -04:00
|
|
|
|
2010-08-09 09:26:44 -03:00
|
|
|
# PEP 3147 compatibility (PYC Repository Directories)
|
|
|
|
cache_from_source = (imp.cache_from_source if hasattr(imp, 'get_tag') else
|
|
|
|
lambda path: path + 'c')
|
|
|
|
|
|
|
|
|
1991-12-18 09:38:27 -04:00
|
|
|
def main():
|
2010-08-09 09:26:44 -03:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
verbose = (sys.argv[1] == '-v')
|
|
|
|
silent = (sys.argv[1] == '-s')
|
|
|
|
else:
|
|
|
|
verbose = silent = False
|
2001-01-17 04:48:39 -04:00
|
|
|
MAGIC = imp.get_magic()
|
|
|
|
if not silent:
|
2007-08-03 14:06:41 -03:00
|
|
|
print('Using MAGIC word', repr(MAGIC))
|
2001-01-17 04:48:39 -04:00
|
|
|
for dirname in sys.path:
|
|
|
|
try:
|
|
|
|
names = os.listdir(dirname)
|
2012-12-24 13:58:48 -04:00
|
|
|
except OSError:
|
2007-08-03 14:06:41 -03:00
|
|
|
print('Cannot list directory', repr(dirname))
|
2001-01-17 04:48:39 -04:00
|
|
|
continue
|
|
|
|
if not silent:
|
2007-08-03 14:06:41 -03:00
|
|
|
print('Checking ', repr(dirname), '...')
|
2010-08-09 09:26:44 -03:00
|
|
|
for name in sorted(names):
|
|
|
|
if name.endswith('.py'):
|
2001-01-17 04:48:39 -04:00
|
|
|
name = os.path.join(dirname, name)
|
|
|
|
try:
|
|
|
|
st = os.stat(name)
|
2012-12-24 13:58:48 -04:00
|
|
|
except OSError:
|
2007-08-03 14:06:41 -03:00
|
|
|
print('Cannot stat', repr(name))
|
2001-01-17 04:48:39 -04:00
|
|
|
continue
|
|
|
|
if verbose:
|
2007-08-03 14:06:41 -03:00
|
|
|
print('Check', repr(name), '...')
|
2010-08-09 09:26:44 -03:00
|
|
|
name_c = cache_from_source(name)
|
2001-01-17 04:48:39 -04:00
|
|
|
try:
|
2010-08-09 09:26:44 -03:00
|
|
|
with open(name_c, 'rb') as f:
|
|
|
|
magic_str = f.read(4)
|
|
|
|
mtime_str = f.read(4)
|
2001-01-17 04:48:39 -04:00
|
|
|
except IOError:
|
2007-08-03 14:06:41 -03:00
|
|
|
print('Cannot open', repr(name_c))
|
2001-01-17 04:48:39 -04:00
|
|
|
continue
|
2008-05-16 12:23:30 -03:00
|
|
|
if magic_str != MAGIC:
|
2007-08-03 14:06:41 -03:00
|
|
|
print('Bad MAGIC word in ".pyc" file', end=' ')
|
|
|
|
print(repr(name_c))
|
2001-01-17 04:48:39 -04:00
|
|
|
continue
|
|
|
|
mtime = get_long(mtime_str)
|
2010-08-09 09:26:44 -03:00
|
|
|
if mtime in {0, -1}:
|
2007-08-03 14:06:41 -03:00
|
|
|
print('Bad ".pyc" file', repr(name_c))
|
2008-05-16 12:23:30 -03:00
|
|
|
elif mtime != st[ST_MTIME]:
|
2007-08-03 14:06:41 -03:00
|
|
|
print('Out-of-date ".pyc" file', end=' ')
|
|
|
|
print(repr(name_c))
|
1991-12-18 09:38:27 -04:00
|
|
|
|
2010-08-09 09:26:44 -03:00
|
|
|
|
1991-12-18 09:38:27 -04:00
|
|
|
def get_long(s):
|
2008-05-16 12:23:30 -03:00
|
|
|
if len(s) != 4:
|
2001-01-17 04:48:39 -04:00
|
|
|
return -1
|
2010-08-09 09:26:44 -03:00
|
|
|
return s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24)
|
|
|
|
|
1991-12-18 09:38:27 -04:00
|
|
|
|
2004-08-09 14:27:55 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|