view Tools/scripts/checkpyc.py @ 58689:514b56efd89a 3.1

Merged revisions 77962 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ................ r77962 | r.david.murray | 2010-02-04 11:41:57 -0500 (Thu, 04 Feb 2010) | 17 lines Merged revisions 77943,77959-77960 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77943 | r.david.murray | 2010-02-03 08:36:23 -0500 (Wed, 03 Feb 2010) | 2 lines Explicitly mention the default value for formatdate's usegmt parameter. ........ r77959 | nick.coghlan | 2010-02-04 07:43:58 -0500 (Thu, 04 Feb 2010) | 1 line Issue 6760: Clarify args handling for subprocess.Popen. Patch by Chris Rebert ........ r77960 | r.david.murray | 2010-02-04 11:33:31 -0500 (Thu, 04 Feb 2010) | 2 lines Add Chris Rebert to ACKS for issue 6760 Popen doc improvements. ........ ................
author R. David Murray <rdmurray@bitdance.com>
date Thu, 04 Feb 2010 16:44:31 +0000
parents 893a4dc17a91
children fcfe324663a6
line wrap: on
line source

#! /usr/bin/env python
# Check that all ".pyc" files exist and are up-to-date
# Uses module 'os'

import sys
import os
from stat import ST_MTIME
import imp

def main():
    silent = 0
    verbose = 0
    if sys.argv[1:]:
        if sys.argv[1] == '-v':
            verbose = 1
        elif sys.argv[1] == '-s':
            silent = 1
    MAGIC = imp.get_magic()
    if not silent:
        print('Using MAGIC word', repr(MAGIC))
    for dirname in sys.path:
        try:
            names = os.listdir(dirname)
        except os.error:
            print('Cannot list directory', repr(dirname))
            continue
        if not silent:
            print('Checking ', repr(dirname), '...')
        names.sort()
        for name in names:
            if name[-3:] == '.py':
                name = os.path.join(dirname, name)
                try:
                    st = os.stat(name)
                except os.error:
                    print('Cannot stat', repr(name))
                    continue
                if verbose:
                    print('Check', repr(name), '...')
                name_c = name + 'c'
                try:
                    f = open(name_c, 'r')
                except IOError:
                    print('Cannot open', repr(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', end=' ')
                    print(repr(name_c))
                    continue
                mtime = get_long(mtime_str)
                if mtime == 0 or mtime == -1:
                    print('Bad ".pyc" file', repr(name_c))
                elif mtime != st[ST_MTIME]:
                    print('Out-of-date ".pyc" file', end=' ')
                    print(repr(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)

if __name__ == '__main__':
    main()