home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / filecmp.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  6.7 KB  |  228 lines

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