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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  7. import os
  8. import ctypes
  9. import sys
  10. import re
  11. from ctypes import Structure as _Structure, c_char_p, c_uint, c_void_p, POINTER, byref, c_wchar_p, c_int, c_char, c_wchar
  12. from tempfile import NamedTemporaryFile
  13. from StringIO import StringIO
  14. from calibre import iswindows, load_library, CurrentDir, prints
  15. from calibre.ptempfile import TemporaryDirectory
  16. _librar_name = 'libunrar'
  17. cdll = ctypes.cdll
  18. if iswindows:
  19.     
  20.     class Structure(_Structure):
  21.         _pack_ = 1
  22.  
  23.     _librar_name = 'unrar'
  24.     cdll = ctypes.windll
  25. else:
  26.     Structure = _Structure
  27. if hasattr(sys, 'frozen') and iswindows:
  28.     lp = os.path.join(os.path.dirname(sys.executable), 'DLLs', 'unrar.dll')
  29.     _libunrar = cdll.LoadLibrary(lp)
  30. elif hasattr(sys, 'frozen_path'):
  31.     lp = os.path.join(sys.frozen_path, 'libunrar.so')
  32.     _libunrar = cdll.LoadLibrary(lp)
  33. else:
  34.     _libunrar = load_library(_librar_name, cdll)
  35. RAR_OM_LIST = 0
  36. RAR_OM_EXTRACT = 1
  37. ERAR_END_ARCHIVE = 10
  38. ERAR_NO_MEMORY = 11
  39. ERAR_BAD_DATA = 12
  40. ERAR_BAD_ARCHIVE = 13
  41. ERAR_UNKNOWN_FORMAT = 14
  42. ERAR_EOPEN = 15
  43. ERAR_ECREATE = 16
  44. ERAR_ECLOSE = 17
  45. ERAR_EREAD = 18
  46. ERAR_EWRITE = 19
  47. ERAR_SMALL_BUF = 20
  48. ERAR_UNKNOWN = 21
  49. ERAR_MISSING_PASSWORD = 22
  50. RAR_VOL_ASK = 0
  51. RAR_VOL_NOTIFY = 1
  52. RAR_SKIP = 0
  53. RAR_TEST = 1
  54. RAR_EXTRACT = 2
  55.  
  56. class UnRARException(Exception):
  57.     pass
  58.  
  59.  
  60. class RAROpenArchiveDataEx(Structure):
  61.     _fields_ = [
  62.         ('ArcName', c_char_p),
  63.         ('ArcNameW', c_wchar_p),
  64.         ('OpenMode', c_uint),
  65.         ('OpenResult', c_uint),
  66.         ('CmtBuf', c_char_p),
  67.         ('CmtBufSize', c_uint),
  68.         ('CmtSize', c_uint),
  69.         ('CmtState', c_uint),
  70.         ('Flags', c_uint),
  71.         ('Reserved', c_uint * 32)]
  72.  
  73.  
  74. class RARHeaderDataEx(Structure):
  75.     _fields_ = [
  76.         ('ArcName', c_char * 1024),
  77.         ('ArcNameW', c_wchar * 1024),
  78.         ('FileName', c_char * 1024),
  79.         ('FileNameW', c_wchar * 1024),
  80.         ('Flags', c_uint),
  81.         ('PackSize', c_uint),
  82.         ('PackSizeHigh', c_uint),
  83.         ('UnpSize', c_uint),
  84.         ('UnpSizeHigh', c_uint),
  85.         ('HostOS', c_uint),
  86.         ('FileCRC', c_uint),
  87.         ('FileTime', c_uint),
  88.         ('UnpVer', c_uint),
  89.         ('Method', c_uint),
  90.         ('FileAttr', c_uint),
  91.         ('CmtBuf', c_char_p),
  92.         ('CmtBufSize', c_uint),
  93.         ('CmtSize', c_uint),
  94.         ('CmtState', c_uint),
  95.         ('Reserved', c_uint * 1024)]
  96.  
  97. _libunrar.RAROpenArchiveEx.argtypes = [
  98.     POINTER(RAROpenArchiveDataEx)]
  99. _libunrar.RAROpenArchiveEx.restype = c_void_p
  100. _libunrar.RARReadHeaderEx.argtypes = [
  101.     c_void_p,
  102.     POINTER(RARHeaderDataEx)]
  103. _libunrar.RARReadHeaderEx.restype = c_int
  104. _libunrar.RARProcessFileW.argtypes = [
  105.     c_void_p,
  106.     c_int,
  107.     c_wchar_p,
  108.     c_wchar_p]
  109. _libunrar.RARProcessFileW.restype = c_int
  110. _libunrar.RARCloseArchive.argtypes = [
  111.     c_void_p]
  112. _libunrar.RARCloseArchive.restype = c_int
  113. _libunrar.RARSetPassword.argtypes = [
  114.     c_void_p,
  115.     c_char_p]
  116.  
  117. def _interpret_open_error(code, path):
  118.     msg = 'Unknown error.'
  119.     if code == ERAR_NO_MEMORY:
  120.         msg = 'Not enough memory to process ' + path
  121.     elif code == ERAR_BAD_DATA:
  122.         msg = 'Archive header broken: ' + path
  123.     elif code == ERAR_BAD_ARCHIVE:
  124.         msg = path + ' is not a RAR archive.'
  125.     elif code == ERAR_EOPEN:
  126.         msg = 'Cannot open ' + path
  127.     
  128.     return msg
  129.  
  130.  
  131. def _interpret_process_file_error(code):
  132.     msg = 'Unknown Error'
  133.     if code == ERAR_UNKNOWN_FORMAT:
  134.         msg = 'Unknown archive format'
  135.     elif code == ERAR_BAD_ARCHIVE:
  136.         msg = 'Bad volume'
  137.     elif code == ERAR_ECREATE:
  138.         msg = 'File create error'
  139.     elif code == ERAR_EOPEN:
  140.         msg = 'Volume open error'
  141.     elif code == ERAR_ECLOSE:
  142.         msg = 'File close error'
  143.     elif code == ERAR_EREAD:
  144.         msg = 'Read error'
  145.     elif code == ERAR_EWRITE:
  146.         msg = 'Write error'
  147.     elif code == ERAR_BAD_DATA:
  148.         msg = 'CRC error'
  149.     elif code == ERAR_MISSING_PASSWORD:
  150.         msg = 'Password is required.'
  151.     
  152.     return msg
  153.  
  154.  
  155. def get_archive_info(flags):
  156.     ios = StringIO()
  157.     print >>ios, 'Volume:\t\t', ios if flags & 1 else 'no'
  158.     print >>ios, 'Comment:\t', ios if flags & 2 else 'no'
  159.     print >>ios, 'Locked:\t\t', ios if flags & 4 else 'no'
  160.     print >>ios, 'Solid:\t\t', ios if flags & 8 else 'no'
  161.     print >>ios, 'New naming:\t', ios if flags & 16 else 'no'
  162.     print >>ios, 'Authenticity:\t', ios if flags & 32 else 'no'
  163.     print >>ios, 'Recovery:\t', ios if flags & 64 else 'no'
  164.     print >>ios, 'Encr.headers:\t', ios if flags & 128 else 'no'
  165.     print >>ios, 'First Volume:\t', ios if flags & 256 else 'no or older than 3.0'
  166.     return ios.getvalue()
  167.  
  168.  
  169. def extract(path, dir):
  170.     open_archive_data = RAROpenArchiveDataEx(ArcName = path, OpenMode = RAR_OM_EXTRACT, CmtBuf = None)
  171.     arc_data = _libunrar.RAROpenArchiveEx(byref(open_archive_data))
  172.     cwd = os.getcwd()
  173.     if not os.path.isdir(dir):
  174.         os.mkdir(dir)
  175.     
  176.     os.chdir(dir)
  177.     
  178.     try:
  179.         if open_archive_data.OpenResult != 0:
  180.             raise UnRARException(_interpret_open_error(open_archive_data.OpenResult, path))
  181.         open_archive_data.OpenResult != 0
  182.         prints('Archive:', path)
  183.         header_data = RARHeaderDataEx(CmtBuf = None)
  184.         while True:
  185.             RHCode = _libunrar.RARReadHeaderEx(arc_data, byref(header_data))
  186.             if RHCode != 0:
  187.                 break
  188.             
  189.             PFCode = _libunrar.RARProcessFileW(arc_data, RAR_EXTRACT, None, None)
  190.             if PFCode != 0:
  191.                 raise UnRARException(_interpret_process_file_error(PFCode))
  192.             PFCode != 0
  193.         if RHCode == ERAR_BAD_DATA:
  194.             raise UnRARException('File header broken')
  195.         RHCode == ERAR_BAD_DATA
  196.     finally:
  197.         os.chdir(cwd)
  198.         _libunrar.RARCloseArchive(arc_data)
  199.  
  200.  
  201.  
  202. def names(path):
  203.     if hasattr(path, 'read'):
  204.         data = path.read()
  205.         f = NamedTemporaryFile(suffix = '.rar')
  206.         f.write(data)
  207.         f.flush()
  208.         path = f.name
  209.     
  210.     open_archive_data = RAROpenArchiveDataEx(ArcName = path, OpenMode = RAR_OM_LIST, CmtBuf = None)
  211.     arc_data = _libunrar.RAROpenArchiveEx(byref(open_archive_data))
  212.     
  213.     try:
  214.         if open_archive_data.OpenResult != 0:
  215.             raise UnRARException(_interpret_open_error(open_archive_data.OpenResult, path))
  216.         open_archive_data.OpenResult != 0
  217.         header_data = RARHeaderDataEx(CmtBuf = None)
  218.         while True:
  219.             if _libunrar.RARReadHeaderEx(arc_data, byref(header_data)) != 0:
  220.                 break
  221.             
  222.             PFCode = _libunrar.RARProcessFileW(arc_data, RAR_SKIP, None, None)
  223.             if PFCode != 0:
  224.                 raise UnRARException(_interpret_process_file_error(PFCode))
  225.             PFCode != 0
  226.             yield header_data.FileNameW
  227.     finally:
  228.         _libunrar.RARCloseArchive(arc_data)
  229.  
  230.  
  231.  
  232. def _extract_member(path, match, name):
  233.     
  234.     def is_match(fname):
  235.         if (name is not None or fname == name) and match is not None:
  236.             pass
  237.         return match.search(fname) is not None
  238.  
  239.     open_archive_data = RAROpenArchiveDataEx(ArcName = path, OpenMode = RAR_OM_EXTRACT, CmtBuf = None)
  240.     arc_data = _libunrar.RAROpenArchiveEx(byref(open_archive_data))
  241.     
  242.     try:
  243.         if open_archive_data.OpenResult != 0:
  244.             raise UnRARException(_interpret_open_error(open_archive_data.OpenResult, path))
  245.         open_archive_data.OpenResult != 0
  246.         header_data = RARHeaderDataEx(CmtBuf = None)
  247.         first = True
  248.         while True:
  249.             if _libunrar.RARReadHeaderEx(arc_data, byref(header_data)) != 0:
  250.                 raise (None, None)(UnRARException if first else 'No match found in %s' % path)
  251.             _libunrar.RARReadHeaderEx(arc_data, byref(header_data)) != 0
  252.             file_name = header_data.FileNameW
  253.             if is_match(file_name):
  254.                 PFCode = _libunrar.RARProcessFileW(arc_data, RAR_EXTRACT, None, None)
  255.                 if PFCode != 0:
  256.                     raise UnRARException(_interpret_process_file_error(PFCode))
  257.                 PFCode != 0
  258.                 abspath = os.path.abspath(*file_name.split('/'))
  259.                 return abspath
  260.             PFCode = _libunrar.RARProcessFileW(arc_data, RAR_SKIP, None, None)
  261.             if PFCode != 0:
  262.                 raise UnRARException(_interpret_process_file_error(PFCode))
  263.             PFCode != 0
  264.             first = False
  265.             continue
  266.             is_match(file_name)
  267.     finally:
  268.         _libunrar.RARCloseArchive(arc_data)
  269.  
  270.  
  271.  
  272. def extract_member(path, match = re.compile('\\.(jpg|jpeg|gif|png)\\s*$', re.I), name = None, as_file = False):
  273.     if hasattr(path, 'read'):
  274.         data = path.read()
  275.         f = NamedTemporaryFile(suffix = '.rar')
  276.         f.write(data)
  277.         f.flush()
  278.         path = f.name
  279.     
  280.     path = os.path.abspath(path)
  281.     if as_file:
  282.         path = _extract_member(path, match, name)
  283.         return (path, open(path, 'rb'))
  284.     
  285.     try:
  286.         tdir = _[1]
  287.         CurrentDir(tdir).__enter__()
  288.         
  289.         try:
  290.             path = _extract_member(path, match, name)
  291.             return (path, open(path, 'rb').read())
  292.         finally:
  293.             pass
  294.  
  295.     finally:
  296.         pass
  297.  
  298.  
  299.