Example #1
0
def _run(exiter):
  # Place the registration of the unhandled exception hook as early as possible in the code.
  sys.excepthook = exiter.unhandled_exception_hook

  # We want to present warnings to the user, set this up early to ensure all warnings are seen.
  # The "default" action displays a warning for a particular file and line number exactly once.
  # See https://docs.python.org/2/library/warnings.html#the-warnings-filter for the complete action
  # list.
  warnings.simplefilter("default")

  # The GoalRunner will setup final logging below in `.setup()`, but span the gap until then.
  logging.basicConfig()
  # This routes the warnings we enabled above through our loggers instead of straight to stderr raw.
  logging.captureWarnings(True)

  version = pants_version()
  if len(sys.argv) == 2 and sys.argv[1] == _VERSION_OPTION:
    exiter.do_exit(msg=version, out=sys.stdout)

  root_dir = get_buildroot()
  if not os.path.exists(root_dir):
    exiter.exit_and_fail('PANTS_BUILD_ROOT does not point to a valid path: %s' % root_dir)

  goal_runner = GoalRunner(root_dir)
  goal_runner.setup()
  result = goal_runner.run()
  exiter.do_exit(result)
Example #2
0
def test_version_request(version_flag):
  class ExitException(Exception):
    def __init__(self, exit_code):
      self.exit_code = exit_code

  with temporary_dir() as build_root:
    def exiter(exit_code):
      raise ExitException(exit_code)

    goal_runner = GoalRunner(build_root, exiter=exiter)
    options_bootstrapper = OptionsBootstrapper(args=[version_flag])

    with pytest.raises(ExitException) as excinfo:
      goal_runner.setup(options_bootstrapper=options_bootstrapper, working_set=WorkingSet())
    assert 0 == excinfo.value.exit_code
Example #3
0
  def _install_options(self, options_bootstrapper, build_configuration):
    """Parse and register options.

    :returns: An Options object representing the full set of runtime options.
    """
    # TODO: This inline import is currently necessary to resolve a ~legitimate cycle between
    # `GoalRunner`->`EngineInitializer`->`OptionsInitializer`->`GoalRunner`.
    from pants.bin.goal_runner import GoalRunner

    # Now that plugins and backends are loaded, we can gather the known scopes.
    known_scope_infos = [GlobalOptionsRegistrar.get_scope_info()]

    # Add scopes for all needed subsystems via a union of all known subsystem sets.
    subsystems = Subsystem.closure(
      GoalRunner.subsystems() | Goal.subsystems() | build_configuration.subsystems()
    )
    for subsystem in subsystems:
      known_scope_infos.append(subsystem.get_scope_info())

    # Add scopes for all tasks in all goals.
    for goal in Goal.all():
      known_scope_infos.extend(filter(None, goal.known_scope_infos()))

    # Now that we have the known scopes we can get the full options.
    options = options_bootstrapper.get_full_options(known_scope_infos)
    self._register_options(subsystems, options)

    # Make the options values available to all subsystems.
    Subsystem.set_options(options)

    return options
  def _install_options(self, options_bootstrapper, build_configuration):
    """Parse and register options.

    :returns: An Options object representing the full set of runtime options.
    """
    # TODO: This inline import is currently necessary to resolve a ~legitimate cycle between
    # `GoalRunner`->`EngineInitializer`->`OptionsInitializer`->`GoalRunner`.
    from pants.bin.goal_runner import GoalRunner

    # Now that plugins and backends are loaded, we can gather the known scopes.

    # Gather the optionables that are not scoped to any other.  All known scopes are reachable
    # via these optionables' known_scope_infos() methods.
    top_level_optionables = ({GlobalOptionsRegistrar} |
                             GoalRunner.subsystems() |
                             build_configuration.subsystems() |
                             set(Goal.get_optionables()))

    known_scope_infos = sorted({
      si for optionable in top_level_optionables for si in optionable.known_scope_infos()
    })

    # Now that we have the known scopes we can get the full options.
    options = options_bootstrapper.get_full_options(known_scope_infos)

    distinct_optionable_classes = sorted({si.optionable_cls for si in known_scope_infos},
                                         key=lambda o: o.options_scope)
    for optionable_cls in distinct_optionable_classes:
      optionable_cls.register_options_on_scope(options)

    # Make the options values available to all subsystems.
    Subsystem.set_options(options)

    return options
Example #5
0
def _run(exiter):
  # Place the registration of the unhandled exception hook as early as possible in the code.
  sys.excepthook = exiter.unhandled_exception_hook

  logging.basicConfig()
  version = pants_version()
  if len(sys.argv) == 2 and sys.argv[1] == _VERSION_OPTION:
    exiter.do_exit(msg=version, out=sys.stdout)

  root_dir = get_buildroot()
  if not os.path.exists(root_dir):
    exiter.exit_and_fail('PANTS_BUILD_ROOT does not point to a valid path: %s' % root_dir)

  goal_runner = GoalRunner(root_dir)
  goal_runner.setup()
  result = goal_runner.run()
  exiter.do_exit(result)
Example #6
0
  def _setup_options(self, options_bootstrapper, working_set):
    # TODO: This inline import is currently necessary to resolve a ~legitimate cycle between
    # `GoalRunner`->`EngineInitializer`->`OptionsInitializer`->`GoalRunner`.
    from pants.bin.goal_runner import GoalRunner

    bootstrap_options = options_bootstrapper.get_bootstrap_options()
    global_bootstrap_options = bootstrap_options.for_global_scope()

    if global_bootstrap_options.pants_version != pants_version():
      raise BuildConfigurationError(
        'Version mismatch: Requested version was {}, our version is {}.'.format(
          global_bootstrap_options.pants_version, pants_version()
        )
      )

    # Get logging setup prior to loading backends so that they can log as needed.
    if self._init_logging:
      self._setup_logging(global_bootstrap_options)

    # Add any extra paths to python path (e.g., for loading extra source backends).
    for path in global_bootstrap_options.pythonpath:
      sys.path.append(path)
      pkg_resources.fixup_namespace_packages(path)

    # Load plugins and backends.
    plugins = global_bootstrap_options.plugins
    backend_packages = global_bootstrap_options.backend_packages
    build_configuration = load_plugins_and_backends(plugins, working_set, backend_packages)

    # Now that plugins and backends are loaded, we can gather the known scopes.
    known_scope_infos = [GlobalOptionsRegistrar.get_scope_info()]

    # Add scopes for all needed subsystems via a union of all known subsystem sets.
    subsystems = Subsystem.closure(
      GoalRunner.subsystems() | Goal.subsystems() | build_configuration.subsystems()
    )
    for subsystem in subsystems:
      known_scope_infos.append(subsystem.get_scope_info())

    # Add scopes for all tasks in all goals.
    for goal in Goal.all():
      known_scope_infos.extend(filter(None, goal.known_scope_infos()))

    # Now that we have the known scopes we can get the full options.
    options = options_bootstrapper.get_full_options(known_scope_infos)
    self._register_options(subsystems, options)

    # Make the options values available to all subsystems.
    Subsystem.set_options(options)

    return options, build_configuration
Example #7
0
    def _run(self):
        # Bootstrap options and logging.
        options_bootstrapper = self._options_bootstrapper or OptionsBootstrapper(
            env=self._env, args=self._args)
        options, build_config = OptionsInitializer(
            options_bootstrapper, exiter=self._exiter).setup()
        global_options = options.for_global_scope()

        # Apply exiter options.
        self._exiter.apply_options(options)

        # Option values are usually computed lazily on demand,
        # but command line options are eagerly computed for validation.
        for scope in options.scope_to_flags.keys():
            options.for_scope(scope)

        # Verify the configs here.
        if global_options.verify_config:
            options_bootstrapper.verify_configs_against_options(options)

        # Launch RunTracker as early as possible (just after Subsystem options are initialized).
        run_tracker, reporting = ReportingInitializer().setup()

        try:
            # Determine the build root dir.
            root_dir = get_buildroot()

            # Capture a repro of the 'before' state for this build, if needed.
            repro = Reproducer.global_instance().create_repro()
            if repro:
                repro.capture(run_tracker.run_info.get_as_dict())

            # Setup and run GoalRunner.
            goal_runner = GoalRunner.Factory(root_dir, options, build_config,
                                             run_tracker, reporting,
                                             self._daemon_build_graph,
                                             self._exiter).setup()

            goal_runner_result = goal_runner.run()

            if repro:
                # TODO: Have Repro capture the 'after' state (as a diff) as well?
                repro.log_location_of_repro_file()
        finally:
            run_tracker_result = run_tracker.end()

        # Take the exit code with higher abs value in case of negative values.
        final_exit_code = goal_runner_result if abs(goal_runner_result) > abs(
            run_tracker_result) else run_tracker_result
        self._exiter.exit(final_exit_code)
Example #8
0
    def _maybe_run_v1(self):
        v1_goals, ambiguous_goals, _ = self._options.goals_by_version
        if not v1_goals and (not ambiguous_goals
                             or not self._global_options.v1):
            return PANTS_SUCCEEDED_EXIT_CODE

        # Setup and run GoalRunner.
        goal_runner_factory = GoalRunner.Factory(
            self._build_root, self._options, self._build_config,
            self._run_tracker, self._reporting, self._graph_session,
            self._target_roots, self._exiter)
        if self._options.help_request:
            return goal_runner_factory.handle_help()

        return goal_runner_factory.create().run()
Example #9
0
  def _maybe_run_v1(self, run_tracker, reporting):
    if not self._global_options.v1:
      return 0

    # Setup and run GoalRunner.
    goal_runner_factory = GoalRunner.Factory(
      self._build_root,
      self._options,
      self._build_config,
      run_tracker,
      reporting,
      self._graph_session,
      self._target_roots,
      self._exiter
    )
    return goal_runner_factory.create().run()
Example #10
0
    def _maybe_run_v1(self):
        v1_goals, ambiguous_goals, _ = self._options.goals_by_version
        if not self._global_options.v1:
            if v1_goals:
                HelpPrinter(
                    self._options,
                    help_request=UnknownGoalHelp(v1_goals)).print_help()
                return PANTS_FAILED_EXIT_CODE
            return PANTS_SUCCEEDED_EXIT_CODE

        if not v1_goals and not ambiguous_goals:
            return PANTS_SUCCEEDED_EXIT_CODE

        # Setup and run GoalRunner.
        return GoalRunner.Factory(self._build_root, self._options,
                                  self._build_config, self._run_tracker,
                                  self._reporting, self._graph_session,
                                  self._target_roots,
                                  self._exiter).create().run()
Example #11
0
    def _run(self):
        # Bootstrap options and logging.
        options_bootstrapper = self._options_bootstrapper or OptionsBootstrapper(
            env=self._env, args=self._args)
        options, build_config = OptionsInitializer(
            options_bootstrapper, exiter=self._exiter).setup()

        # Apply exiter options.
        self._exiter.apply_options(options)

        # Verify the configs here.
        if options.for_global_scope().verify_config:
            options_bootstrapper.verify_configs_against_options(options)

        # Launch RunTracker as early as possible (just after Subsystem options are initialized).
        run_tracker, reporting = ReportingInitializer().setup()

        try:
            # Determine the build root dir.
            root_dir = get_buildroot()

            # Capture a repro of the 'before' state for this build, if needed.
            repro = Reproducer.global_instance().create_repro()
            if repro:
                repro.capture(run_tracker.run_info.get_as_dict())

            # Setup and run GoalRunner.
            goal_runner = GoalRunner.Factory(root_dir,
                                             options,
                                             build_config,
                                             run_tracker,
                                             reporting,
                                             exiter=self._exiter).setup()

            result = goal_runner.run()

            if repro:
                # TODO: Have Repro capture the 'after' state (as a diff) as well?
                repro.log_location_of_repro_file()
        finally:
            run_tracker.end()

        self._exiter.exit(result)
Example #12
0
    def _install_options(self, options_bootstrapper, build_configuration):
        """Parse and register options.

    :returns: An Options object representing the full set of runtime options.
    """
        # TODO: This inline import is currently necessary to resolve a ~legitimate cycle between
        # `GoalRunner`->`EngineInitializer`->`OptionsInitializer`->`GoalRunner`.
        from pants.bin.goal_runner import GoalRunner

        # Now that plugins and backends are loaded, we can gather the known scopes.

        # Gather the optionables that are not scoped to any other.  All known scopes are reachable
        # via these optionables' known_scope_infos() methods.
        top_level_optionables = ({GlobalOptionsRegistrar}
                                 | GoalRunner.subsystems()
                                 | build_configuration.subsystems()
                                 | set(Goal.get_optionables()))

        known_scope_infos = sorted({
            si
            for optionable in top_level_optionables
            for si in optionable.known_scope_infos()
        })

        # Now that we have the known scopes we can get the full options.
        options = options_bootstrapper.get_full_options(known_scope_infos)

        distinct_optionable_classes = sorted(
            {si.optionable_cls
             for si in known_scope_infos},
            key=lambda o: o.options_scope)
        for optionable_cls in distinct_optionable_classes:
            optionable_cls.register_options_on_scope(options)

        # Make the options values available to all subsystems.
        Subsystem.set_options(options)

        return options
Example #13
0
def _run(exiter):
  # We want to present warnings to the user, set this up early to ensure all warnings are seen.
  # The "default" action displays a warning for a particular file and line number exactly once.
  # See https://docs.python.org/2/library/warnings.html#the-warnings-filter for the complete action
  # list.
  warnings.simplefilter('default')

  # Bootstrap options and logging.
  options, build_config = OptionsInitializer().setup()

  # Apply exiter options.
  exiter.apply_options(options)

  # Launch RunTracker as early as possible (just after Subsystem options are initialized).
  run_tracker, reporting = ReportingInitializer().setup()

  # Determine the build root dir.
  root_dir = get_buildroot()

  # Setup and run GoalRunner.
  goal_runner = GoalRunner.Factory(root_dir, options, build_config, run_tracker, reporting).setup()
  result = goal_runner.run()
  exiter.do_exit(result)
Example #14
0
 def run():
     goal_runner = GoalRunner.Factory(root_dir, options, build_config,
                                      run_tracker, reporting).setup()
     return goal_runner.run()