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