home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2565 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  8.4 KB  |  310 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import wincerapi
  5. import win32api
  6. import win32file
  7. import getopt
  8. import sys
  9. import os
  10. import string
  11. import win32con
  12. import fnmatch
  13.  
  14. class InvalidUsage(Exception):
  15.     pass
  16.  
  17.  
  18. def print_error(api_exc, msg):
  19.     (hr, fn, errmsg) = api_exc
  20.     print '%s - %s(%d)' % (msg, errmsg, hr)
  21.  
  22.  
  23. def GetFileAttributes(file, local = 1):
  24.     if local:
  25.         return win32api.GetFileAttributes(file)
  26.     return wincerapi.CeGetFileAttributes(file)
  27.  
  28.  
  29. def FindFiles(spec, local = 1):
  30.     if local:
  31.         return win32api.FindFiles(spec)
  32.     return wincerapi.CeFindFiles(spec)
  33.  
  34.  
  35. def isdir(name, local = 1):
  36.     
  37.     try:
  38.         attr = GetFileAttributes(name, local)
  39.         return attr & win32con.FILE_ATTRIBUTE_DIRECTORY
  40.     except win32api.error:
  41.         return 0
  42.  
  43.  
  44.  
  45. def CopyFileToCe(src_name, dest_name, progress = None):
  46.     sh = win32file.CreateFile(src_name, win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None)
  47.     bytes = 0
  48.     
  49.     try:
  50.         dh = wincerapi.CeCreateFile(dest_name, win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, 0, None)
  51.         
  52.         try:
  53.             while None:
  54.                 (hr, data) = win32file.ReadFile(sh, 2048)
  55.                 if not data:
  56.                     break
  57.                 
  58.                 bytes = bytes + len(data)
  59.                 if progress is not None:
  60.                     progress(bytes)
  61.                     continue
  62.             dh.Close()
  63.         finally:
  64.             sh.Close()
  65.  
  66.         return bytes
  67.  
  68.  
  69.  
  70. def BuildFileList(spec, local, recurse, filter, filter_args, recursed_path = ''):
  71.     files = []
  72.     if isdir(spec, local):
  73.         path = spec
  74.         raw_spec = '*'
  75.     else:
  76.         (path, raw_spec) = os.path.split(spec)
  77.     if recurse:
  78.         infos = FindFiles(os.path.join(path, '*'), local)
  79.     else:
  80.         infos = FindFiles(os.path.join(path, raw_spec), local)
  81.     for info in infos:
  82.         src_name = str(info[8])
  83.         full_src_name = os.path.join(path, src_name)
  84.         if local:
  85.             full_src_name = win32api.GetFullPathName(full_src_name)
  86.         
  87.         if isdir(full_src_name, local):
  88.             if recurse and src_name not in ('.', '..'):
  89.                 new_spec = os.path.join(full_src_name, raw_spec)
  90.                 files = files + BuildFileList(new_spec, local, 1, filter, filter_args, os.path.join(recursed_path, src_name))
  91.             
  92.         
  93.         if fnmatch.fnmatch(src_name, raw_spec):
  94.             rel_name = os.path.join(recursed_path, src_name)
  95.             filter_data = filter(full_src_name, rel_name, info, local, filter_args)
  96.             if filter_data is not None:
  97.                 files.append((full_src_name, info, filter_data))
  98.             
  99.         filter_data is not None
  100.     
  101.     return files
  102.  
  103.  
  104. def _copyfilter(full_name, rel_name, info, local, bMaintainDir):
  105.     if isdir(full_name, local):
  106.         return None
  107.     if bMaintainDir:
  108.         return rel_name
  109.     return os.path.split(rel_name)[1]
  110.  
  111. import pywin.dialogs.status as pywin
  112. import win32ui
  113.  
  114. class FileCopyProgressDialog(pywin.dialogs.status.CStatusProgressDialog):
  115.     
  116.     def CopyProgress(self, bytes):
  117.         self.Set(bytes / 1024)
  118.  
  119.  
  120.  
  121. def copy(args):
  122.     bRecurse = bVerbose = 0
  123.     bMaintainDir = 1
  124.     
  125.     try:
  126.         (opts, args) = getopt.getopt(args, 'rv')
  127.     except getopt.error:
  128.         details = None
  129.         raise InvalidUsage(details)
  130.  
  131.     for o, v in opts:
  132.         if o == '-r':
  133.             bRecuse = 1
  134.             continue
  135.         if o == '-v':
  136.             bVerbose = 1
  137.             continue
  138.     
  139.     if len(args) < 2:
  140.         raise InvalidUsage('Must specify a source and destination')
  141.     len(args) < 2
  142.     src = args[:-1]
  143.     dest = args[-1]
  144.     if string.find(src[0], 'WCE:') == 0:
  145.         bToDevice = 0
  146.     elif string.find(dest, 'WCE:') == 0:
  147.         bToDevice = 1
  148.     else:
  149.         bToDevice = 1
  150.     if not isdir(dest, not bToDevice):
  151.         print '%s does not indicate a directory'
  152.     
  153.     files = []
  154.     num_files = 0
  155.     num_bytes = 0
  156.     dialog = FileCopyProgressDialog('Copying files')
  157.     dialog.CreateWindow(win32ui.GetMainFrame())
  158.     if bToDevice:
  159.         for spec in src:
  160.             new = BuildFileList(spec, 1, bRecurse, _copyfilter, bMaintainDir)
  161.             if not new:
  162.                 print "Warning: '%s' did not match any files" % spec
  163.             
  164.             files = files + new
  165.         
  166.         for full_src, src_info, dest_info in files:
  167.             dest_name = os.path.join(dest, dest_info)
  168.             size = src_info[5]
  169.             print 'Size=', size
  170.             if bVerbose:
  171.                 print full_src, '->', dest_name, '- ',
  172.             
  173.             dialog.SetText(dest_name)
  174.             dialog.Set(0, size / 1024)
  175.             bytes = CopyFileToCe(full_src, dest_name, dialog.CopyProgress)
  176.             num_bytes = num_bytes + bytes
  177.             if bVerbose:
  178.                 print bytes, 'bytes'
  179.             
  180.             num_files = num_files + 1
  181.         
  182.     
  183.     dialog.Close()
  184.     print '%d files copied (%d bytes)' % (num_files, num_bytes)
  185.  
  186.  
  187. def _dirfilter(*args):
  188.     return args[1]
  189.  
  190.  
  191. def dir(args):
  192.     bRecurse = 0
  193.     
  194.     try:
  195.         (opts, args) = getopt.getopt(args, 'r')
  196.     except getopt.error:
  197.         details = None
  198.         raise InvalidUsage(details)
  199.  
  200.     for o, v in opts:
  201.         if o == '-r':
  202.             bRecurse = 1
  203.             continue
  204.     
  205.     for arg in args:
  206.         print 'Directory of WCE:%s' % arg
  207.         files = BuildFileList(arg, 0, bRecurse, _dirfilter, None)
  208.         total_size = 0
  209.         for full_name, info, rel_name in files:
  210.             date_str = info[3].Format('%d-%b-%Y %H:%M')
  211.             attr_string = '     '
  212.             if info[0] & win32con.FILE_ATTRIBUTE_DIRECTORY:
  213.                 attr_string = '<DIR>'
  214.             
  215.             print '%s  %s %10d %s' % (date_str, attr_string, info[5], rel_name)
  216.             total_size = total_size + info[5]
  217.         
  218.         print '              ' + '%3d files, %10d bytes' % (len(files), total_size)
  219.     
  220.  
  221.  
  222. def run(args):
  223.     prog_args = []
  224.     for arg in args:
  225.         if ' ' in arg:
  226.             prog_args.append('"' + arg + '"')
  227.             continue
  228.         prog_args.append(arg)
  229.     
  230.     prog_args = string.join(prog_args, ' ')
  231.     wincerapi.CeCreateProcess(prog_args, '', None, None, 0, 0, None, '', None)
  232.  
  233.  
  234. def delete(args):
  235.     for arg in args:
  236.         
  237.         try:
  238.             wincerapi.CeDeleteFile(arg)
  239.             print 'Deleted: %s' % arg
  240.         continue
  241.         except win32api.error:
  242.             details = None
  243.             print_error(details, "Error deleting '%s'" % arg)
  244.             continue
  245.         
  246.  
  247.     
  248.  
  249.  
  250. def DumpCommands():
  251.     print '%-10s - %s' % ('Command', 'Description')
  252.     print '%-10s - %s' % ('-------', '-----------')
  253.     for name, item in globals().items():
  254.         if type(item) == type(DumpCommands):
  255.             doc = getattr(item, '__doc__', '')
  256.             if doc:
  257.                 lines = string.split(doc, '\n')
  258.                 print '%-10s - %s' % (name, lines[0])
  259.                 for line in lines[1:]:
  260.                     if line:
  261.                         print '        ', line
  262.                         continue
  263.                 
  264.             
  265.         doc
  266.     
  267.  
  268.  
  269. def main():
  270.     if len(sys.argv) < 2:
  271.         print 'You must specify a command!'
  272.         DumpCommands()
  273.         return None
  274.     command = sys.argv[1]
  275.     fn = globals().get(command)
  276.     if fn is None:
  277.         print 'Unknown command:', command
  278.         DumpCommands()
  279.         return None
  280.     wincerapi.CeRapiInit()
  281.     
  282.     try:
  283.         verinfo = wincerapi.CeGetVersionEx()
  284.         print 'Connected to device, CE version %d.%d %s' % (verinfo[0], verinfo[1], verinfo[4])
  285.         
  286.         try:
  287.             fn(sys.argv[2:])
  288.         except InvalidUsage:
  289.             len(sys.argv) < 2
  290.             msg = len(sys.argv) < 2
  291.             print 'Invalid syntax -', msg
  292.             print fn.__doc__
  293.         except:
  294.             len(sys.argv) < 2
  295.  
  296.     finally:
  297.         
  298.         try:
  299.             wincerapi.CeRapiUninit()
  300.         except win32api.error:
  301.             details = fn is None
  302.             len(sys.argv) < 2
  303.             print_error(details, 'Error disconnecting')
  304.  
  305.  
  306.  
  307. if __name__ == '__main__':
  308.     main()
  309.  
  310.