<s> '''<STR_LIT>''' <EOL> import sys <EOL> import re <EOL> import collections <EOL> import CGAT . Experiment as E <EOL> import CGAT . IOTools as IOTools <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>" , <EOL> action = "<STR_LIT>" , <EOL> type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> action = "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . set_defaults ( <EOL> filename_map = None , <EOL> do_sort = True , <EOL> dry_run = False , <EOL> suffixes = [ ] ) <EOL> ( options , args ) = E . Start ( parser , argv = argv ) <EOL> if options . filename_map : <EOL> map_regex2dest = IOTools . readMap ( <EOL> IOTools . openFile ( options . filename_map ) ) <EOL> map_regex2dest = dict ( [ ( re . compile ( x ) , y ) for x , y in <EOL> map_regex2dest . items ( ) ] ) <EOL> map_suffix2dest = { } <EOL> for suffix in options . suffixes : <EOL> src , dest = suffix . split ( "<STR_LIT:=>" ) <EOL> map_suffix2dest [ src . strip ( ) ] = dest . strip ( ) <EOL> filenames = args <EOL> if options . do_sort : <EOL> filenames . sort ( ) <EOL> dest2src = collections . defaultdict ( list ) <EOL> for filename in filenames : <EOL> dests = [ ] <EOL> for regex , dest in map_regex2dest . items ( ) : <EOL> if regex . search ( filename ) : <EOL> dests . append ( dest ) <EOL> if len ( dests ) == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % filename ) <EOL> elif len ( dests ) > <NUM_LIT:1> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % <EOL> ( filename , dests ) ) <EOL> dest = dests [ <NUM_LIT:0> ] <EOL> for suffix , new_suffix in map_suffix2dest . items ( ) : <EOL> if filename . endswith ( suffix ) : <EOL> if suffix in map_suffix2dest : <EOL> dest = dest + map_suffix2dest [ suffix ] <EOL> break <EOL> dest2src [ dest ] . append ( filename ) <EOL> for dest , srcs in sorted ( dest2src . iteritems ( ) ) : <EOL> E . info ( "<STR_LIT>" % ( dest , srcs ) ) <EOL> if options . dry_run : <EOL> continue <EOL> E . run ( '<STR_LIT>' % ( "<STR_LIT:U+0020>" . join ( srcs ) , <EOL> dest ) ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
