<s> '''<STR_LIT>''' <EOL> import os <EOL> import sys <EOL> import re <EOL> import glob <EOL> import os . path <EOL> import CGAT . Experiment as E <EOL> def getLastLine ( filename , read_size = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> f = open ( <EOL> filename , '<STR_LIT>' ) <EOL> offset = read_size <EOL> f . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> file_size = f . tell ( ) <EOL> if file_size == <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> while <NUM_LIT:1> : <EOL> if file_size < offset : <EOL> offset = file_size <EOL> f . seek ( - <NUM_LIT:1> * offset , <NUM_LIT:2> ) <EOL> read_str = f . read ( offset ) <EOL> if read_str [ offset - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> read_str = read_str [ : - <NUM_LIT:1> ] <EOL> lines = read_str . split ( '<STR_LIT:\n>' ) <EOL> if len ( lines ) >= <NUM_LIT:2> : <EOL> return lines [ - <NUM_LIT:1> ] <EOL> if offset == file_size : <EOL> return read_str <EOL> offset += read_size <EOL> f . close ( ) <EOL> def checkPythonRuns ( filename ) : <EOL> """<STR_LIT>""" <EOL> last_line = getLastLine ( filename ) <EOL> return re . match ( "<STR_LIT>" , last_line ) <EOL> def isNewer ( a , b ) : <EOL> """<STR_LIT>""" <EOL> at = os . stat ( a ) [ <NUM_LIT:7> ] <EOL> bt = os . stat ( b ) [ <NUM_LIT:7> ] <EOL> return at > bt <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if argv is None : <EOL> argv = sys . argv <EOL> parser = E . OptionParser ( version = "<STR_LIT>" , <EOL> usage = globals ( ) [ "<STR_LIT>" ] ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . set_defaults ( glob_pattern = "<STR_LIT>" , <EOL> file_pattern = "<STR_LIT>" , <EOL> check_completeness = "<STR_LIT>" , <EOL> skip_dirs = [ ] , <EOL> dry_run = False , <EOL> ) <EOL> ( options , args ) = E . Start ( parser , <EOL> add_pipe_options = True ) <EOL> if args : <EOL> starts = args <EOL> elif options . glob_pattern : <EOL> starts = glob . glob ( options . glob_pattern ) <EOL> else : <EOL> starts = "<STR_LIT:.>" <EOL> ndirs , nfiles , ndeleted = <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> <EOL> if options . check_completeness == "<STR_LIT>" : <EOL> isComplete = checkPythonRuns <EOL> rx = re . compile ( options . file_pattern ) <EOL> for start in starts : <EOL> for root , dirs , files in os . walk ( start ) : <EOL> ndirs += <NUM_LIT:1> <EOL> for dir in options . skip_dirs : <EOL> if dir in dirs : <EOL> dirs . remove ( dir ) <EOL> for filename in files : <EOL> p = os . path . join ( root , filename ) <EOL> if rx . search ( filename ) and not isComplete ( p ) : <EOL> if options . dry_run : <EOL> options . stdlog . write ( "<STR_LIT>" % p ) <EOL> else : <EOL> os . remove ( p ) <EOL> ndeleted += <NUM_LIT:1> <EOL> if options . loglevel >= <NUM_LIT:1> : <EOL> options . stdlog . write ( "<STR_LIT>" % <EOL> ( ndirs , nfiles , ndeleted ) ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
