<s> import wincerapi <EOL> import win32api <EOL> import win32file <EOL> import getopt <EOL> import sys <EOL> import os <EOL> import string <EOL> import win32con <EOL> import fnmatch <EOL> class InvalidUsage ( Exception ) : pass <EOL> def print_error ( api_exc , msg ) : <EOL> hr , fn , errmsg = api_exc <EOL> print "<STR_LIT>" % ( msg , errmsg , hr ) <EOL> def GetFileAttributes ( file , local = <NUM_LIT:1> ) : <EOL> if local : return win32api . GetFileAttributes ( file ) <EOL> else : return wincerapi . CeGetFileAttributes ( file ) <EOL> def FindFiles ( spec , local = <NUM_LIT:1> ) : <EOL> if local : return win32api . FindFiles ( spec ) <EOL> else : return wincerapi . CeFindFiles ( spec ) <EOL> def isdir ( name , local = <NUM_LIT:1> ) : <EOL> try : <EOL> attr = GetFileAttributes ( name , local ) <EOL> return attr & win32con . FILE_ATTRIBUTE_DIRECTORY <EOL> except win32api . error : <EOL> return <NUM_LIT:0> <EOL> def CopyFileToCe ( src_name , dest_name , progress = None ) : <EOL> sh = win32file . CreateFile ( src_name , win32con . GENERIC_READ , <NUM_LIT:0> , None , win32con . OPEN_EXISTING , <NUM_LIT:0> , None ) <EOL> bytes = <NUM_LIT:0> <EOL> try : <EOL> dh = wincerapi . CeCreateFile ( dest_name , win32con . GENERIC_WRITE , <NUM_LIT:0> , None , win32con . OPEN_ALWAYS , <NUM_LIT:0> , None ) <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> hr , data = win32file . ReadFile ( sh , <NUM_LIT> ) <EOL> if not data : <EOL> break <EOL> wincerapi . CeWriteFile ( dh , data ) <EOL> bytes = bytes + len ( data ) <EOL> if progress is not None : progress ( bytes ) <EOL> finally : <EOL> pass <EOL> dh . Close ( ) <EOL> finally : <EOL> sh . Close ( ) <EOL> return bytes <EOL> def BuildFileList ( spec , local , recurse , filter , filter_args , recursed_path = "<STR_LIT>" ) : <EOL> files = [ ] <EOL> if isdir ( spec , local ) : <EOL> path = spec <EOL> raw_spec = "<STR_LIT:*>" <EOL> else : <EOL> path , raw_spec = os . path . split ( spec ) <EOL> if recurse : <EOL> infos = FindFiles ( os . path . join ( path , "<STR_LIT:*>" ) , local ) <EOL> else : <EOL> infos = FindFiles ( os . path . join ( path , raw_spec ) , local ) <EOL> for info in infos : <EOL> src_name = str ( info [ <NUM_LIT:8> ] ) <EOL> full_src_name = os . path . join ( path , src_name ) <EOL> if local : <EOL> full_src_name = win32api . GetFullPathName ( full_src_name ) <EOL> if isdir ( full_src_name , local ) : <EOL> if recurse and src_name not in [ '<STR_LIT:.>' , '<STR_LIT:..>' ] : <EOL> new_spec = os . path . join ( full_src_name , raw_spec ) <EOL> files = files + BuildFileList ( new_spec , local , <NUM_LIT:1> , filter , filter_args , os . path . join ( recursed_path , src_name ) ) <EOL> if fnmatch . fnmatch ( src_name , raw_spec ) : <EOL> rel_name = os . path . join ( recursed_path , src_name ) <EOL> filter_data = filter ( full_src_name , rel_name , info , local , filter_args ) <EOL> if filter_data is not None : <EOL> files . append ( ( full_src_name , info , filter_data ) ) <EOL> return files <EOL> def _copyfilter ( full_name , rel_name , info , local , bMaintainDir ) : <EOL> if isdir ( full_name , local ) : return <EOL> if bMaintainDir : <EOL> return rel_name <EOL> return os . path . split ( rel_name ) [ <NUM_LIT:1> ] <EOL> import pywin . dialogs . status , win32ui <EOL> class FileCopyProgressDialog ( pywin . dialogs . status . CStatusProgressDialog ) : <EOL> def CopyProgress ( self , bytes ) : <EOL> self . Set ( bytes / <NUM_LIT> ) <EOL> def copy ( args ) : <EOL> """<STR_LIT>""" <EOL> bRecurse = bVerbose = <NUM_LIT:0> <EOL> bMaintainDir = <NUM_LIT:1> <EOL> try : <EOL> opts , args = getopt . getopt ( args , "<STR_LIT>" ) <EOL> except getopt . error , details : <EOL> raise InvalidUsage ( details ) <EOL> for o , v in opts : <EOL> if o == "<STR_LIT>" : <EOL> bRecuse = <NUM_LIT:1> <EOL> elif o == '<STR_LIT>' : <EOL> bVerbose = <NUM_LIT:1> <EOL> if len ( args ) < <NUM_LIT:2> : <EOL> raise InvalidUsage ( "<STR_LIT>" ) <EOL> src = args [ : - <NUM_LIT:1> ] <EOL> dest = args [ - <NUM_LIT:1> ] <EOL> if string . find ( src [ <NUM_LIT:0> ] , "<STR_LIT>" ) == <NUM_LIT:0> : <EOL> bToDevice = <NUM_LIT:0> <EOL> elif string . find ( dest , "<STR_LIT>" ) == <NUM_LIT:0> : <EOL> bToDevice = <NUM_LIT:1> <EOL> else : <EOL> bToDevice = <NUM_LIT:1> <EOL> if not isdir ( dest , not bToDevice ) : <EOL> print "<STR_LIT>" <EOL> files = [ ] <EOL> num_files = <NUM_LIT:0> <EOL> num_bytes = <NUM_LIT:0> <EOL> dialog = FileCopyProgressDialog ( "<STR_LIT>" ) <EOL> dialog . CreateWindow ( win32ui . GetMainFrame ( ) ) <EOL> if bToDevice : <EOL> for spec in src : <EOL> new = BuildFileList ( spec , <NUM_LIT:1> , bRecurse , _copyfilter , bMaintainDir ) <EOL> if not new : <EOL> print "<STR_LIT>" % ( spec ) <EOL> files = files + new <EOL> for full_src , src_info , dest_info in files : <EOL> dest_name = os . path . join ( dest , dest_info ) <EOL> size = src_info [ <NUM_LIT:5> ] <EOL> print "<STR_LIT>" , size <EOL> if bVerbose : <EOL> print full_src , "<STR_LIT>" , dest_name , "<STR_LIT>" , <EOL> dialog . SetText ( dest_name ) <EOL> dialog . Set ( <NUM_LIT:0> , size / <NUM_LIT> ) <EOL> bytes = CopyFileToCe ( full_src , dest_name , dialog . CopyProgress ) <EOL> num_bytes = num_bytes + bytes <EOL> if bVerbose : <EOL> print bytes , "<STR_LIT>" <EOL> num_files = num_files + <NUM_LIT:1> <EOL> dialog . Close ( ) <EOL> print "<STR_LIT>" % ( num_files , num_bytes ) <EOL> def _dirfilter ( * args ) : <EOL> return args [ <NUM_LIT:1> ] <EOL> def dir ( args ) : <EOL> """<STR_LIT>""" <EOL> bRecurse = <NUM_LIT:0> <EOL> try : <EOL> opts , args = getopt . getopt ( args , "<STR_LIT:r>" ) <EOL> except getopt . error , details : <EOL> raise InvalidUsage ( details ) <EOL> for o , v in opts : <EOL> if o == "<STR_LIT>" : <EOL> bRecurse = <NUM_LIT:1> <EOL> for arg in args : <EOL> print "<STR_LIT>" % arg <EOL> files = BuildFileList ( arg , <NUM_LIT:0> , bRecurse , _dirfilter , None ) <EOL> total_size = <NUM_LIT:0> <EOL> for full_name , info , rel_name in files : <EOL> date_str = info [ <NUM_LIT:3> ] . Format ( "<STR_LIT>" ) <EOL> attr_string = "<STR_LIT:U+0020>" <EOL> if info [ <NUM_LIT:0> ] & win32con . FILE_ATTRIBUTE_DIRECTORY : attr_string = "<STR_LIT>" <EOL> print "<STR_LIT>" % ( date_str , attr_string , info [ <NUM_LIT:5> ] , rel_name ) <EOL> total_size = total_size + info [ <NUM_LIT:5> ] <EOL> print "<STR_LIT:U+0020>" * <NUM_LIT> + "<STR_LIT>" % ( len ( files ) , total_size ) <EOL> def run ( args ) : <EOL> """<STR_LIT>""" <EOL> prog_args = [ ] <EOL> for arg in args : <EOL> if "<STR_LIT:U+0020>" in arg : <EOL> prog_args . append ( '<STR_LIT:">' + arg + '<STR_LIT:">' ) <EOL> else : <EOL> prog_args . append ( arg ) <EOL> prog_args = string . join ( prog_args , "<STR_LIT:U+0020>" ) <EOL> wincerapi . CeCreateProcess ( prog_args , "<STR_LIT>" , None , None , <NUM_LIT:0> , <NUM_LIT:0> , None , "<STR_LIT>" , None ) <EOL> def delete ( args ) : <EOL> """<STR_LIT>""" <EOL> for arg in args : <EOL> try : <EOL> wincerapi . CeDeleteFile ( arg ) <EOL> print "<STR_LIT>" % arg <EOL> except win32api . error , details : <EOL> print_error ( details , "<STR_LIT>" % arg ) <EOL> def DumpCommands ( ) : <EOL> print "<STR_LIT>" % ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> print "<STR_LIT>" % ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> for name , item in globals ( ) . items ( ) : <EOL> if type ( item ) == type ( DumpCommands ) : <EOL> doc = getattr ( item , "<STR_LIT>" , "<STR_LIT>" ) <EOL> if doc : <EOL> lines = string . split ( doc , "<STR_LIT:\n>" ) <EOL> print "<STR_LIT>" % ( name , lines [ <NUM_LIT:0> ] ) <EOL> for line in lines [ <NUM_LIT:1> : ] : <EOL> if line : <EOL> print "<STR_LIT:U+0020>" * <NUM_LIT:8> , line <EOL> def main ( ) : <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> print "<STR_LIT>" <EOL> DumpCommands ( ) <EOL> return <EOL> command = sys . argv [ <NUM_LIT:1> ] <EOL> fn = globals ( ) . get ( command ) <EOL> if fn is None : <EOL> print "<STR_LIT>" , command <EOL> DumpCommands ( ) <EOL> return <EOL> wincerapi . CeRapiInit ( ) <EOL> try : <EOL> verinfo = wincerapi . CeGetVersionEx ( ) <EOL> print "<STR_LIT>" % ( verinfo [ <NUM_LIT:0> ] , verinfo [ <NUM_LIT:1> ] , verinfo [ <NUM_LIT:4> ] ) <EOL> try : <EOL> fn ( sys . argv [ <NUM_LIT:2> : ] ) <EOL> except InvalidUsage , msg : <EOL> print "<STR_LIT>" , msg <EOL> print fn . __doc__ <EOL> finally : <EOL> try : <EOL> wincerapi . CeRapiUninit ( ) <EOL> except win32api . error , details : <EOL> print_error ( details , "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
