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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import stat
  6. from itertools import ifilter, ifilterfalse, imap, izip
  7. __all__ = [
  8.     'cmp',
  9.     'dircmp',
  10.     'cmpfiles']
  11. _cache = { }
  12. BUFSIZE = 8192
  13.  
  14. def cmp(f1, f2, shallow = 1):
  15.     s1 = _sig(os.stat(f1))
  16.     s2 = _sig(os.stat(f2))
  17.     if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
  18.         return False
  19.     if shallow and s1 == s2:
  20.         return True
  21.     if s1[1] != s2[1]:
  22.         return False
  23.     result = _cache.get((f1, f2))
  24.     if result and (s1, s2) == result[:2]:
  25.         return result[2]
  26.     outcome = _do_cmp(f1, f2)
  27.     _cache[(f1, f2)] = (s1, s2, outcome)
  28.     return outcome
  29.  
  30.  
  31. def _sig(st):
  32.     return (stat.S_IFMT(st.st_mode), st.st_size, st.st_mtime)
  33.  
  34.  
  35. def _do_cmp(f1, f2):
  36.     bufsize = BUFSIZE
  37.     fp1 = open(f1, 'rb')
  38.     fp2 = open(f2, 'rb')
  39.     while True:
  40.         b1 = fp1.read(bufsize)
  41.         b2 = fp2.read(bufsize)
  42.         if b1 != b2:
  43.             return False
  44.         if not b1:
  45.             return True
  46.         continue
  47.         b1
  48.  
  49.  
  50. class dircmp:
  51.     
  52.     def __init__(self, a, b, ignore = None, hide = None):
  53.         self.left = a
  54.         self.right = b
  55.         if hide is None:
  56.             self.hide = [
  57.                 os.curdir,
  58.                 os.pardir]
  59.         else:
  60.             self.hide = hide
  61.         if ignore is None:
  62.             self.ignore = [
  63.                 'RCS',
  64.                 'CVS',
  65.                 'tags']
  66.         else:
  67.             self.ignore = ignore
  68.  
  69.     
  70.     def phase0(self):
  71.         self.left_list = _filter(os.listdir(self.left), self.hide + self.ignore)
  72.         self.right_list = _filter(os.listdir(self.right), self.hide + self.ignore)
  73.         self.left_list.sort()
  74.         self.right_list.sort()
  75.  
  76.     
  77.     def phase1(self):
  78.         a = dict(izip(imap(os.path.normcase, self.left_list), self.left_list))
  79.         b = dict(izip(imap(os.path.normcase, self.right_list), self.right_list))
  80.         self.common = map(a.__getitem__, ifilter(b.__contains__, a))
  81.         self.left_only = map(a.__getitem__, ifilterfalse(b.__contains__, a))
  82.         self.right_only = map(b.__getitem__, ifilterfalse(a.__contains__, b))
  83.  
  84.     
  85.     def phase2(self):
  86.         self.common_dirs = []
  87.         self.common_files = []
  88.         self.common_funny = []
  89.         for x in self.common:
  90.             a_path = os.path.join(self.left, x)
  91.             b_path = os.path.join(self.right, x)
  92.             ok = 1
  93.             
  94.             try:
  95.                 a_stat = os.stat(a_path)
  96.             except os.error:
  97.                 why = None
  98.                 ok = 0
  99.  
  100.             
  101.             try:
  102.                 b_stat = os.stat(b_path)
  103.             except os.error:
  104.                 why = None
  105.                 ok = 0
  106.  
  107.             if ok:
  108.                 a_type = stat.S_IFMT(a_stat.st_mode)
  109.                 b_type = stat.S_IFMT(b_stat.st_mode)
  110.                 if a_type != b_type:
  111.                     self.common_funny.append(x)
  112.                 elif stat.S_ISDIR(a_type):
  113.                     self.common_dirs.append(x)
  114.                 elif stat.S_ISREG(a_type):
  115.                     self.common_files.append(x)
  116.                 else:
  117.                     self.common_funny.append(x)
  118.             a_type != b_type
  119.             self.common_funny.append(x)
  120.         
  121.  
  122.     
  123.     def phase3(self):
  124.         xx = cmpfiles(self.left, self.right, self.common_files)
  125.         (self.same_files, self.diff_files, self.funny_files) = xx
  126.  
  127.     
  128.     def phase4(self):
  129.         self.subdirs = { }
  130.         for x in self.common_dirs:
  131.             a_x = os.path.join(self.left, x)
  132.             b_x = os.path.join(self.right, x)
  133.             self.subdirs[x] = dircmp(a_x, b_x, self.ignore, self.hide)
  134.         
  135.  
  136.     
  137.     def phase4_closure(self):
  138.         self.phase4()
  139.         for sd in self.subdirs.itervalues():
  140.             sd.phase4_closure()
  141.         
  142.  
  143.     
  144.     def report(self):
  145.         print 'diff', self.left, self.right
  146.         if self.left_only:
  147.             self.left_only.sort()
  148.             print 'Only in', self.left, ':', self.left_only
  149.         
  150.         if self.right_only:
  151.             self.right_only.sort()
  152.             print 'Only in', self.right, ':', self.right_only
  153.         
  154.         if self.same_files:
  155.             self.same_files.sort()
  156.             print 'Identical files :', self.same_files
  157.         
  158.         if self.diff_files:
  159.             self.diff_files.sort()
  160.             print 'Differing files :', self.diff_files
  161.         
  162.         if self.funny_files:
  163.             self.funny_files.sort()
  164.             print 'Trouble with common files :', self.funny_files
  165.         
  166.         if self.common_dirs:
  167.             self.common_dirs.sort()
  168.             print 'Common subdirectories :', self.common_dirs
  169.         
  170.         if self.common_funny:
  171.             self.common_funny.sort()
  172.             print 'Common funny cases :', self.common_funny
  173.         
  174.  
  175.     
  176.     def report_partial_closure(self):
  177.         self.report()
  178.         for sd in self.subdirs.itervalues():
  179.             print 
  180.             sd.report()
  181.         
  182.  
  183.     
  184.     def report_full_closure(self):
  185.         self.report()
  186.         for sd in self.subdirs.itervalues():
  187.             print 
  188.             sd.report_full_closure()
  189.         
  190.  
  191.     methodmap = dict(subdirs = phase4, same_files = phase3, diff_files = phase3, funny_files = phase3, common_dirs = phase2, common_files = phase2, common_funny = phase2, common = phase1, left_only = phase1, right_only = phase1, left_list = phase0, right_list = phase0)
  192.     
  193.     def __getattr__(self, attr):
  194.         if attr not in self.methodmap:
  195.             raise AttributeError, attr
  196.         attr not in self.methodmap
  197.         self.methodmap[attr](self)
  198.         return getattr(self, attr)
  199.  
  200.  
  201.  
  202. def cmpfiles(a, b, common, shallow = 1):
  203.     res = ([], [], [])
  204.     for x in common:
  205.         ax = os.path.join(a, x)
  206.         bx = os.path.join(b, x)
  207.         res[_cmp(ax, bx, shallow)].append(x)
  208.     
  209.     return res
  210.  
  211.  
  212. def _cmp(a, b, sh, abs = abs, cmp = cmp):
  213.     
  214.     try:
  215.         return not abs(cmp(a, b, sh))
  216.     except os.error:
  217.         return 2
  218.  
  219.  
  220.  
  221. def _filter(flist, skip):
  222.     return list(ifilterfalse(skip.__contains__, flist))
  223.  
  224.  
  225. def demo():
  226.     import sys
  227.     import getopt
  228.     (options, args) = getopt.getopt(sys.argv[1:], 'r')
  229.     if len(args) != 2:
  230.         raise getopt.GetoptError('need exactly two args', None)
  231.     len(args) != 2
  232.     dd = dircmp(args[0], args[1])
  233.     if ('-r', '') in options:
  234.         dd.report_full_closure()
  235.     else:
  236.         dd.report()
  237.  
  238. if __name__ == '__main__':
  239.     demo()
  240.  
  241.