- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathcheckpyc.py
executable file
·66 lines (62 loc) · 1.96 KB
/
checkpyc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/env python
# Check that all ".pyc" files exist and are up-to-date
# Uses module 'os'
importsys
importos
fromstatimportST_MTIME
importimp
defmain():
silent=0
verbose=0
ifsys.argv[1:]:
ifsys.argv[1] =='-v':
verbose=1
elifsys.argv[1] =='-s':
silent=1
MAGIC=imp.get_magic()
ifnotsilent:
print'Using MAGIC word', repr(MAGIC)
fordirnameinsys.path:
try:
names=os.listdir(dirname)
exceptos.error:
print'Cannot list directory', repr(dirname)
continue
ifnotsilent:
print'Checking ', repr(dirname), '...'
names.sort()
fornameinnames:
ifname[-3:] =='.py':
name=os.path.join(dirname, name)
try:
st=os.stat(name)
exceptos.error:
print'Cannot stat', repr(name)
continue
ifverbose:
print'Check', repr(name), '...'
name_c=name+'c'
try:
f=open(name_c, 'r')
exceptIOError:
print'Cannot open', repr(name_c)
continue
magic_str=f.read(4)
mtime_str=f.read(4)
f.close()
ifmagic_str<>MAGIC:
print'Bad MAGIC word in ".pyc" file',
printrepr(name_c)
continue
mtime=get_long(mtime_str)
ifmtime==0ormtime==-1:
print'Bad ".pyc" file', repr(name_c)
elifmtime<>st[ST_MTIME]:
print'Out-of-date ".pyc" file',
printrepr(name_c)
defget_long(s):
iflen(s) <>4:
return-1
returnord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
if__name__=='__main__':
main()