mwchase

LC:Nobilis Update 3 - backend

May 14th, 2017
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.55 KB | None | 0 0
  1. import sys
  2.  
  3. from coverage import cmdline
  4.  
  5.  
  6. class CustomScript(cmdline.CoverageScript):
  7.  
  8.     def __init__(self, *args, **kwargs):
  9.         super().__init__(*args, **kwargs)
  10.         self.module_coverage = {}
  11.         self.coverage_args = {}
  12.         self.current_coverage = None
  13.         self.import_coverage = None
  14.         self.options = None
  15.         self.code_ran = None
  16.         self.ran = False
  17.  
  18.     def make_import_coverage(self, roots):
  19.         kwargs = self.coverage_args.copy()
  20.         kwargs['source'] = sorted(set(kwargs['source']).union(roots))
  21.         self.import_coverage = self.covpkg.Coverage(**kwargs)
  22.  
  23.     def new_coverage(self, test_path, module_path):
  24.         # This should only be called during "run"
  25.         kwargs = self.coverage_args.copy()
  26.         kwargs['source'] = kwargs['source'].copy() + [test_path, module_path]
  27.         self.module_coverage[test_path] = self.covpkg.Coverage(**kwargs)
  28.  
  29.     def _end_coverage(self):
  30.             self.current_coverage.stop()
  31.             if self.code_ran:
  32.                 data_file = self.current_coverage.get_option("run:data_file")
  33.                 if self.path_exists(data_file):
  34.                     self.current_coverage.combine(data_paths=[data_file])
  35.             self.current_coverage.save()
  36.  
  37.     def switch_coverage(self, target_coverage):
  38.         if self.current_coverage is not target_coverage:
  39.             self._end_coverage()
  40.             self.current_coverage = target_coverage
  41.             self.current_coverage.start()
  42.  
  43.     def activate_coverage(self, test_path):
  44.         target_coverage = self.module_coverage.get(test_path)
  45.         if target_coverage:
  46.             self.switch_coverage(target_coverage)
  47.         else:
  48.             print('Unknown test path: {}'.format(test_path))
  49.  
  50.     def deactivate_coverage(self):
  51.         self.switch_coverage(self.coverage)
  52.  
  53.     def complete(self):
  54.         if self.ran:
  55.             self._end_coverage()
  56.  
  57.     def command_line(self, argv):
  58.         """The bulk of the command line interface to coverage.py.
  59.  
  60.        `argv` is the argument list to process.
  61.  
  62.        Returns 0 if all is well, 1 if something went wrong.
  63.  
  64.        """
  65.         # Collect the command-line options.
  66.         if not argv:
  67.             self.help_fn(topic='minimum_help')
  68.             return cmdline.OK
  69.  
  70.         # The command syntax we parse depends on the first argument.  Global
  71.         # switch syntax always starts with an option.
  72.         self.global_option = argv[0].startswith('-')
  73.         if self.global_option:
  74.             parser = cmdline.GlobalOptionParser()
  75.         else:
  76.             parser = cmdline.CMDS.get(argv[0])
  77.             if not parser:
  78.                 self.help_fn("Unknown command: '%s'" % argv[0])
  79.                 return cmdline.ERR
  80.             argv = argv[1:]
  81.  
  82.         parser.help_fn = self.help_fn
  83.         ok, options, args = parser.parse_args_ok(argv)
  84.         if not ok:
  85.             return cmdline.ERR
  86.  
  87.         # Handle help and version.
  88.         if self.do_help(options, args, parser):
  89.             return cmdline.OK
  90.  
  91.         # We need to be able to import from the current directory, because
  92.         # plugins may try to, for example, to read Django settings.
  93.         sys.path[0] = ''
  94.  
  95.         # Listify the list options.
  96.         source = (cmdline.unshell_list(options.source) or []) + ['tests']
  97.         omit = cmdline.unshell_list(options.omit)
  98.         include = cmdline.unshell_list(options.include)
  99.         debug = cmdline.unshell_list(options.debug)
  100.  
  101.         self.options = options
  102.  
  103.         self.coverage_args = dict(
  104.             data_suffix=options.parallel_mode,
  105.             cover_pylib=options.pylib,
  106.             timid=options.timid,
  107.             branch=options.branch,
  108.             config_file=options.rcfile,
  109.             source=source,
  110.             omit=omit,
  111.             include=include,
  112.             debug=debug,
  113.             concurrency=options.concurrency,
  114.         )
  115.  
  116.         # Do something.
  117.         self.current_coverage = self.coverage = self.covpkg.Coverage(
  118.             **self.coverage_args)
  119.  
  120.         if options.action == "debug":
  121.             return self.do_debug(args)
  122.  
  123.         elif options.action == "erase":
  124.             self.coverage.erase()
  125.             return cmdline.OK
  126.  
  127.         elif options.action == "run":
  128.             return self.do_run(options, args)
  129.  
  130.         elif options.action == "combine":
  131.             if options.append:
  132.                 self.coverage.load()
  133.             data_dirs = args or None
  134.             self.coverage.combine(data_dirs, strict=True)
  135.             self.coverage.save()
  136.             return cmdline.OK
  137.  
  138.         # Remaining actions are reporting, with some common options.
  139.         report_args = dict(
  140.             morfs=cmdline.unglob_args(args),
  141.             ignore_errors=options.ignore_errors,
  142.             omit=omit,
  143.             include=include,
  144.         )
  145.  
  146.         self.coverage.load()
  147.  
  148.         total = None
  149.         if options.action == "report":
  150.             total = self.coverage.report(
  151.                 show_missing=options.show_missing,
  152.                 skip_covered=options.skip_covered, **report_args)
  153.         elif options.action == "annotate":
  154.             self.coverage.annotate(
  155.                 directory=options.directory, **report_args)
  156.         elif options.action == "html":
  157.             total = self.coverage.html_report(
  158.                 directory=options.directory, title=options.title,
  159.                 skip_covered=options.skip_covered, **report_args)
  160.         elif options.action == "xml":
  161.             outfile = options.outfile
  162.             total = self.coverage.xml_report(outfile=outfile, **report_args)
  163.  
  164.         if total is not None:
  165.             # Apply the command line fail-under options, and then use the
  166.             # config value, so we can get fail_under from the config file.
  167.             if options.fail_under is not None:
  168.                 self.coverage.set_option("report:fail_under",
  169.                                          options.fail_under)
  170.  
  171.             fail_under = self.coverage.get_option("report:fail_under")
  172.             if cmdline.should_fail_under(total, fail_under):
  173.                 return cmdline.FAIL_UNDER
  174.  
  175.         return cmdline.OK
  176.  
  177.     def do_run(self, options, args):
  178.         """Implementation of 'coverage run'."""
  179.  
  180.         self.ran = True
  181.  
  182.         if not args:
  183.             self.help_fn("Nothing to do.")
  184.             return cmdline.ERR
  185.  
  186.         if options.append and self.coverage.get_option("run:parallel"):
  187.             self.help_fn("Can't append to data files in parallel mode.")
  188.             return cmdline.ERR
  189.  
  190.         if options.concurrency == "multiprocessing":
  191.             # Can't set other run-affecting command line options with
  192.             # multiprocessing.
  193.             for opt_name in ['branch', 'include', 'omit', 'pylib', 'source',
  194.                              'timid']:
  195.                 # As it happens, all of these options have no default, meaning
  196.                 # they will be None if they have not been specified.
  197.                 if getattr(options, opt_name) is not None:
  198.                     self.help_fn(
  199.                         "Options affecting multiprocessing must be specified "
  200.                         "in a configuration file."
  201.                     )
  202.                     return cmdline.ERR
  203.  
  204.         if not self.coverage.get_option("run:parallel"):
  205.             if not options.append:
  206.                 self.coverage.erase()
  207.  
  208.         self.coverage.start()
  209.         self.code_ran = True
  210.         try:
  211.             if options.module:
  212.                 self.run_python_module(args[0], args)
  213.             else:
  214.                 filename = args[0]
  215.                 self.run_python_file(filename, args)
  216.         except cmdline.NoSource:
  217.             self.code_ran = False
  218.             raise
  219.  
  220.         return cmdline.OK
  221.  
  222.  
  223. coverage_script = CustomScript()
Advertisement
Add Comment
Please, Sign In to add comment