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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import sys
  6. import unittest
  7. import getopt
  8. import time
  9. use_resources = []
  10.  
  11. class ResourceDenied(Exception):
  12.     pass
  13.  
  14.  
  15. def is_resource_enabled(resource):
  16.     if sys._getframe().f_back.f_globals.get('__name__') == '__main__':
  17.         return True
  18.     if not use_resources is not None and resource in use_resources:
  19.         pass
  20.     result = '*' in use_resources
  21.     if not result:
  22.         _unavail[resource] = None
  23.     
  24.     return result
  25.  
  26. _unavail = { }
  27.  
  28. def requires(resource, msg = None):
  29.     if sys._getframe().f_back.f_globals.get('__name__') == '__main__':
  30.         return None
  31.     if not is_resource_enabled(resource):
  32.         if msg is None:
  33.             msg = "Use of the `%s' resource not enabled" % resource
  34.         
  35.         raise ResourceDenied(msg)
  36.     is_resource_enabled(resource)
  37.  
  38.  
  39. def find_package_modules(package, mask):
  40.     import fnmatch
  41.     if hasattr(package, '__loader__') and hasattr(package.__loader__, '_files'):
  42.         path = package.__name__.replace('.', os.path.sep)
  43.         mask = os.path.join(path, mask)
  44.         for fnm in package.__loader__._files.iterkeys():
  45.             if fnmatch.fnmatchcase(fnm, mask):
  46.                 yield os.path.splitext(fnm)[0].replace(os.path.sep, '.')
  47.                 continue
  48.         
  49.     else:
  50.         path = package.__path__[0]
  51.         for fnm in os.listdir(path):
  52.             if fnmatch.fnmatchcase(fnm, mask):
  53.                 yield '%s.%s' % (package.__name__, os.path.splitext(fnm)[0])
  54.                 continue
  55.         
  56.  
  57.  
  58. def get_tests(package, mask, verbosity, exclude = ()):
  59.     tests = []
  60.     skipped = []
  61.     for modname in find_package_modules(package, mask):
  62.         if modname.split('.')[-1] in exclude:
  63.             skipped.append(modname)
  64.             if verbosity > 1:
  65.                 print >>sys.stderr, 'Skipped %s: excluded' % modname
  66.                 continue
  67.             continue
  68.         
  69.         
  70.         try:
  71.             mod = __import__(modname, globals(), locals(), [
  72.                 '*'])
  73.         except ResourceDenied:
  74.             detail = None
  75.             skipped.append(modname)
  76.             if verbosity > 1:
  77.                 print >>sys.stderr, 'Skipped %s: %s' % (modname, detail)
  78.                 continue
  79.             continue
  80.  
  81.         for name in dir(mod):
  82.             if name.startswith('_'):
  83.                 continue
  84.             
  85.             o = getattr(mod, name)
  86.             if type(o) is type(unittest.TestCase) and issubclass(o, unittest.TestCase):
  87.                 tests.append(o)
  88.                 continue
  89.         
  90.     
  91.     return (skipped, tests)
  92.  
  93.  
  94. def usage():
  95.     print __doc__
  96.     return 1
  97.  
  98.  
  99. def test_with_refcounts(runner, verbosity, testcase):
  100.     import gc
  101.     import ctypes
  102.     ptc = ctypes._pointer_type_cache.copy()
  103.     cfc = ctypes._c_functype_cache.copy()
  104.     wfc = ctypes._win_functype_cache.copy()
  105.     
  106.     def cleanup():
  107.         ctypes._pointer_type_cache = ptc.copy()
  108.         ctypes._c_functype_cache = cfc.copy()
  109.         ctypes._win_functype_cache = wfc.copy()
  110.         gc.collect()
  111.  
  112.     test = unittest.makeSuite(testcase)
  113.     for i in range(5):
  114.         rc = sys.gettotalrefcount()
  115.         runner.run(test)
  116.         cleanup()
  117.     
  118.     COUNT = 5
  119.     refcounts = [
  120.         None] * COUNT
  121.     for i in range(COUNT):
  122.         rc = sys.gettotalrefcount()
  123.         runner.run(test)
  124.         cleanup()
  125.         refcounts[i] = sys.gettotalrefcount() - rc
  126.     
  127.     if filter(None, refcounts):
  128.         print '%s leaks:\n\t' % testcase, refcounts
  129.     elif verbosity:
  130.         print '%s: ok.' % testcase
  131.     
  132.  
  133.  
  134. class TestRunner(unittest.TextTestRunner):
  135.     
  136.     def run(self, test, skipped):
  137.         result = self._makeResult()
  138.         startTime = time.time()
  139.         test(result)
  140.         stopTime = time.time()
  141.         timeTaken = stopTime - startTime
  142.         result.printErrors()
  143.         self.stream.writeln(result.separator2)
  144.         run = result.testsRun
  145.         if _unavail:
  146.             requested = _unavail.keys()
  147.             requested.sort()
  148.             if not run != 1 or 's':
  149.                 pass
  150.             if not len(skipped) != 1 or 's':
  151.                 pass
  152.             self.stream.writeln('Ran %d test%s in %.3fs (%s module%s skipped)' % (run, '', timeTaken, len(skipped), ''))
  153.             self.stream.writeln('Unavailable resources: %s' % ', '.join(requested))
  154.         elif not run != 1 or 's':
  155.             pass
  156.         self.stream.writeln('Ran %d test%s in %.3fs' % (run, '', timeTaken))
  157.         self.stream.writeln()
  158.         if not result.wasSuccessful():
  159.             self.stream.write('FAILED (')
  160.             (failed, errored) = map(len, (result.failures, result.errors))
  161.             if failed:
  162.                 self.stream.write('failures=%d' % failed)
  163.             
  164.             if errored:
  165.                 if failed:
  166.                     self.stream.write(', ')
  167.                 
  168.                 self.stream.write('errors=%d' % errored)
  169.             
  170.             self.stream.writeln(')')
  171.         else:
  172.             self.stream.writeln('OK')
  173.         return result
  174.  
  175.  
  176.  
  177. def main(*packages):
  178.     
  179.     try:
  180.         (opts, args) = getopt.getopt(sys.argv[1:], 'rqvu:x:')
  181.     except getopt.error:
  182.         return usage()
  183.  
  184.     verbosity = 1
  185.     search_leaks = False
  186.     exclude = []
  187.     for flag, value in opts:
  188.         if flag == '-q':
  189.             verbosity -= 1
  190.             continue
  191.         if flag == '-v':
  192.             verbosity += 1
  193.             continue
  194.         if flag == '-r':
  195.             
  196.             try:
  197.                 sys.gettotalrefcount
  198.             except AttributeError:
  199.                 print >>sys.stderr, '-r flag requires Python debug build'
  200.                 return -1
  201.  
  202.             search_leaks = True
  203.             continue
  204.         if flag == '-u':
  205.             use_resources.extend(value.split(','))
  206.             continue
  207.         if flag == '-x':
  208.             exclude.extend(value.split(','))
  209.             continue
  210.     
  211.     mask = 'test_*.py'
  212.     if args:
  213.         mask = args[0]
  214.     
  215.     for package in packages:
  216.         run_tests(package, mask, verbosity, search_leaks, exclude)
  217.     
  218.  
  219.  
  220. def run_tests(package, mask, verbosity, search_leaks, exclude):
  221.     (skipped, testcases) = get_tests(package, mask, verbosity, exclude)
  222.     runner = TestRunner(verbosity = verbosity)
  223.     suites = [ unittest.makeSuite(o) for o in testcases ]
  224.     suite = unittest.TestSuite(suites)
  225.     result = runner.run(suite, skipped)
  226.     return bool(result.errors)
  227.  
  228.  
  229. class BasicTestRunner:
  230.     
  231.     def run(self, test):
  232.         result = unittest.TestResult()
  233.         test(result)
  234.         return result
  235.  
  236.  
  237.