<s> """<STR_LIT>""" <EOL> import sys , os <EOL> from hashlib import md5 <EOL> if sys . platform == "<STR_LIT:win32>" : <EOL> UserFile = file <EOL> else : <EOL> from pycopia . UserFile import UserFile <EOL> from pycopia . aid import Enum , IF <EOL> Binary = Enum ( <NUM_LIT:1> , "<STR_LIT:*>" ) <EOL> Text = Enum ( <NUM_LIT:0> , "<STR_LIT:U+0020>" ) <EOL> class SumFile ( UserFile ) : <EOL> def write_record ( self , digest , binary , fname ) : <EOL> if "<STR_LIT:\\>" in fname or "<STR_LIT:\n>" in fname : <EOL> self . write ( "<STR_LIT:\\>" ) <EOL> self . write ( "<STR_LIT>" % ( digest , IF ( binary , "<STR_LIT:*>" , "<STR_LIT:U+0020>" ) , repr ( fname ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) ) <EOL> def read_record ( self ) : <EOL> escaped = <NUM_LIT:0> <EOL> line = self . readline ( ) <EOL> if not line : <EOL> return "<STR_LIT>" , Binary , "<STR_LIT>" <EOL> line = line . strip ( ) <EOL> if line . startswith ( "<STR_LIT:\\>" ) : <EOL> escaped = <NUM_LIT:1> <EOL> line = line [ <NUM_LIT:1> : ] <EOL> digest = line [ : <NUM_LIT:32> ] <EOL> assert line [ <NUM_LIT:32> ] == "<STR_LIT:U+0020>" <EOL> if line [ <NUM_LIT> ] == "<STR_LIT:*>" : <EOL> mode = Binary <EOL> else : <EOL> mode = Text <EOL> if escaped : <EOL> fname = eval ( '<STR_LIT>' % line [ <NUM_LIT> : ] ) <EOL> else : <EOL> fname = line [ <NUM_LIT> : ] <EOL> return digest , mode , fname <EOL> def read_records ( self ) : <EOL> while <NUM_LIT:1> : <EOL> d , mode , fname = self . read_record ( ) <EOL> if d : <EOL> yield d , mode , fname <EOL> else : <EOL> raise StopIteration <EOL> def md5sum ( filename , mode = Binary ) : <EOL> m = md5 ( ) <EOL> f = open ( filename , IF ( mode , "<STR_LIT:rb>" , "<STR_LIT:r>" ) ) <EOL> b = f . read ( <NUM_LIT> ) <EOL> while b : <EOL> m . update ( b ) <EOL> b = f . read ( <NUM_LIT> ) <EOL> return m . hexdigest ( ) <EOL> def compare_md5 ( filename , chash , mode ) : <EOL> fhash = md5sum ( filename , mode ) <EOL> return fhash == chash <EOL> def _default_failure ( fname ) : <EOL> print >> sys . stdout , "<STR_LIT>" % ( os . getcwd ( ) , fname , ) <EOL> def _verbose_progress ( name , disp ) : <EOL> print name , IF ( disp , "<STR_LIT:OK>" , "<STR_LIT>" ) <EOL> def check_md5sums_all ( root = None , failure_cb = _default_failure , progress_cb = None ) : <EOL> if root is None : <EOL> root = os . getcwd ( ) <EOL> check_md5sums ( root , failure_cb , progress_cb ) <EOL> names = os . listdir ( root ) <EOL> for name in names : <EOL> if os . path . isdir ( name ) : <EOL> if progress_cb : <EOL> progress_cb ( "<STR_LIT>" % ( name , ) , <NUM_LIT:1> ) <EOL> check_md5sums_all ( os . path . join ( root , name ) , failure_cb , progress_cb ) <EOL> def check_md5sums ( root = None , failure_cb = _default_failure , progress_cb = None ) : <EOL> if root is None : <EOL> savedir = root = os . getcwd ( ) <EOL> else : <EOL> savedir = os . getcwd ( ) <EOL> os . chdir ( root ) <EOL> try : <EOL> try : <EOL> sumfile = SumFile ( "<STR_LIT>" ) <EOL> except IOError : <EOL> if progress_cb : <EOL> print >> sys . stderr , "<STR_LIT>" % ( os . getcwd ( ) , ) <EOL> return <EOL> for chash , mode , fname in sumfile . read_records ( ) : <EOL> try : <EOL> disp = compare_md5 ( fname , chash , mode ) <EOL> except IOError , err : <EOL> failure_cb ( "<STR_LIT>" % ( fname , err ) ) <EOL> continue <EOL> if progress_cb : <EOL> progress_cb ( fname , disp ) <EOL> if not disp : <EOL> failure_cb ( fname ) <EOL> finally : <EOL> os . chdir ( savedir ) <EOL> def make_md5sums ( filelist = None , progress_cb = None , mode = Binary ) : <EOL> if filelist is None : <EOL> filelist = filter ( os . path . isfile , os . listdir ( os . getcwd ( ) ) ) <EOL> try : <EOL> filelist . remove ( "<STR_LIT>" ) <EOL> except ValueError : <EOL> pass <EOL> sumfile = SumFile ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> try : <EOL> for fname in filelist : <EOL> try : <EOL> mdhash = md5sum ( fname , mode ) <EOL> except IOError : <EOL> print >> sys . stderr , "<STR_LIT>" % ( fname , ) <EOL> continue <EOL> if progress_cb : <EOL> progress_cb ( fname , <NUM_LIT:1> ) <EOL> sumfile . write_record ( mdhash , mode , fname ) <EOL> finally : <EOL> sumfile . close ( ) <EOL> def make_md5sums_all ( root = None , progress_cb = None , mode = Binary ) : <EOL> if root is None : <EOL> root = os . getcwd ( ) <EOL> names = os . listdir ( root ) <EOL> filelist = filter ( os . path . isfile , names ) <EOL> make_md5sums ( filelist , progress_cb , mode ) <EOL> try : <EOL> for name in names : <EOL> if os . path . isdir ( name ) : <EOL> if progress_cb : <EOL> progress_cb ( "<STR_LIT>" % ( name , ) , <NUM_LIT:1> ) <EOL> new = os . path . join ( root , name ) <EOL> os . chdir ( new ) <EOL> make_md5sums_all ( new , progress_cb , mode ) <EOL> finally : <EOL> os . chdir ( root ) <EOL> def md5sums ( path ) : <EOL> """<STR_LIT>""" <EOL> failures = [ ] <EOL> counter = Counter ( ) <EOL> check_md5sums ( path , failures . append , counter ) <EOL> return counter . good , counter . bad , failures <EOL> class Counter ( object ) : <EOL> def __init__ ( self ) : <EOL> self . good = <NUM_LIT:0> <EOL> self . bad = <NUM_LIT:0> <EOL> def __call__ ( self , name , disp ) : <EOL> if disp : <EOL> self . good += <NUM_LIT:1> <EOL> else : <EOL> self . bad += <NUM_LIT:1> </s>
