示例#1
0
def main():
    parser = parse_args.make_parser()
    args = parser.parse_args(sys.argv[1:])

    if args.version:
        print(io.get_pytype_version())
        sys.exit(0)

    tool_utils.setup_logging_or_die(args.verbosity)

    if args.generate_config:
        config.generate_sample_config_or_die(args.generate_config)
        sys.exit(0)

    args.filenames = file_utils.expand_source_files(args.filenames)
    conf = parser.config_from_defaults()
    # File options overwrite defaults.
    file_config = config.read_config_file_or_die(args.config)
    parser.postprocess(file_config, from_strings=True)
    conf.populate_from(file_config)
    # Command line arguments overwrite file options.
    conf.populate_from(args)
    if not conf.pythonpath:
        conf.pythonpath = environment.compute_pythonpath(args.filenames)
    logging.info('\n  '.join(['Configuration:'] + str(conf).split('\n')))

    if not args.filenames:
        parser.parser.print_usage()
        sys.exit(0)

    # Importlab needs the python exe, so we check it as early as possible.
    environment.check_python_exe_or_die(conf.python_version)

    typeshed = environment.initialize_typeshed_or_die()
    env = create_importlab_environment(conf, typeshed)
    try:
        import_graph = importlab.graph.ImportGraph.create(env, args.filenames)
    except Exception as e:  # pylint: disable=broad-except
        logging.critical('Cannot parse input files:\n%s', str(e))
        sys.exit(1)

    if args.tree:
        print('Source tree:')
        importlab.output.print_tree(import_graph)
        sys.exit(0)

    if args.unresolved:
        print('Unresolved dependencies:')
        for imp in sorted(import_graph.get_all_unresolved()):
            print(' ', imp.name)
        sys.exit(0)

    logging.info('Source tree:\n%s',
                 importlab.output.formatted_deps_list(import_graph))
    tool_utils.makedirs_or_die(conf.output,
                               'Could not create output directory')
    deps = pytype_runner.deps_from_import_graph(import_graph)
    runner = pytype_runner.PytypeRunner(args.filenames, deps, conf)
    runner.run()
示例#2
0
def main():
  parser = parse_args.make_parser()
  args = parser.parse_args(sys.argv[1:])

  if args.version:
    print(io.get_pytype_version())
    sys.exit(0)

  tool_utils.setup_logging_or_die(args.verbosity)

  if args.generate_config:
    config.generate_sample_config_or_die(args.generate_config,
                                         parser.pytype_single_args)
    sys.exit(0)

  conf = parser.config_from_defaults()
  # File options overwrite defaults.
  file_config = config.read_config_file_or_die(args.config)
  parser.postprocess(file_config, from_strings=True)
  conf.populate_from(file_config)
  # Command line arguments overwrite file options.
  conf.populate_from(args)
  conf.inputs -= conf.exclude
  if args.no_cache:
    conf.output = tempfile.mkdtemp()
  if not conf.pythonpath:
    conf.pythonpath = environment.compute_pythonpath(conf.inputs)
  logging.info('\n  '.join(['Configuration:'] + str(conf).split('\n')))

  if not conf.inputs:
    parser.parser.error('Need an input.')

  # Importlab needs the python exe, so we check it as early as possible.
  environment.check_python_exe_or_die(conf.python_version)

  typeshed = environment.initialize_typeshed_or_die()
  env = analyze_project_env.create_importlab_environment(conf, typeshed)
  print('Computing dependencies')
  import_graph = importlab.graph.ImportGraph.create(env, conf.inputs, trim=True)

  if args.tree:
    print('Source tree:')
    importlab.output.print_tree(import_graph)
    sys.exit(0)

  if args.unresolved:
    print('Unresolved dependencies:')
    for imp in sorted(import_graph.get_all_unresolved()):
      print(' ', imp.name)
    sys.exit(0)

  # Main usage mode: analyze the project file by file in dependency order.

  logging.info('Source tree:\n%s',
               importlab.output.formatted_deps_list(import_graph))
  tool_utils.makedirs_or_die(conf.output, 'Could not create output directory')
  deps = pytype_runner.deps_from_import_graph(import_graph)
  runner = pytype_runner.PytypeRunner(conf, deps)
  return runner.run()
示例#3
0
 def test_setup_cfg_from_subdir(self):
     with file_utils.Tempdir() as d:
         d.create_file('setup.cfg', SETUP_CFG)
         sub = d.create_directory('x/y/z')
         conf = config.Config()
         with file_utils.cd(sub):
             conf = config.read_config_file_or_die(None)
             self._validate_file_contents(conf, d.path)
示例#4
0
 def test_read(self):
   with file_utils.Tempdir() as d:
     f = os.path.join(d.path, 'test.cfg')
     config.generate_sample_config_or_die(f, self.parser.pytype_single_args)
     conf = config.read_config_file_or_die(f)
   # Smoke test for postprocessing and spot-check of a result.
   self.parser.postprocess(conf, from_strings=True)
   self.assertIsInstance(conf.report_errors, bool)
示例#5
0
 def test_missing_setup_cfg_section(self):
     with file_utils.Tempdir() as d:
         d.create_file('setup.cfg', RANDOM_CFG)
         with file_utils.cd(d.path):
             conf = config.read_config_file_or_die(None)
             self._validate_empty_contents(conf)
示例#6
0
 def test_setup_cfg(self):
     with file_utils.Tempdir() as d:
         d.create_file('setup.cfg', SETUP_CFG)
         with file_utils.cd(d.path):
             conf = config.read_config_file_or_die(None)
             self._validate_file_contents(conf, d.path)
示例#7
0
 def test_missing_config_file_section(self):
     with file_utils.Tempdir() as d:
         f = d.create_file('test.cfg', RANDOM_CFG)
         with self.assertRaises(SystemExit):
             config.read_config_file_or_die(f)
示例#8
0
 def test_config_file(self):
     with file_utils.Tempdir() as d:
         f = d.create_file('test.cfg', PYTYPE_CFG)
         conf = config.read_config_file_or_die(f)
         self._validate_file_contents(conf, d.path)