def parse_bootstrap_options(
        env: Mapping[str, str], args: Sequence[str], config: Config
    ) -> Options:
        bootstrap_options = Options.create(
            env=env, config=config, known_scope_infos=[GlobalOptions.get_scope_info()], args=args,
        )

        def register_global(*args, **kwargs):
            # Only use of Options.register?
            bootstrap_options.register(GLOBAL_SCOPE, *args, **kwargs)

        GlobalOptions.register_bootstrap_options(register_global)
        return bootstrap_options
    def do_test(kwargs, expected_basic=False, expected_advanced=False):
        def exp_to_len(exp):
            return int(exp)  # True -> 1, False -> 0.

        parser = Parser(
            env={},
            config=Config.load([]),
            scope_info=GlobalOptions.get_scope_info(),
        )
        parser.register("--foo", **kwargs)
        oshi = HelpInfoExtracter("").get_option_scope_help_info(
            "", parser, False)
        assert exp_to_len(expected_basic) == len(oshi.basic)
        assert exp_to_len(expected_advanced) == len(oshi.advanced)
Beispiel #3
0
 def _format_for_global_scope(show_advanced, show_deprecated, args, kwargs):
     parser = Parser(
         env={},
         config=Config.load([]),
         scope_info=GlobalOptions.get_scope_info(),
         parent_parser=None,
     )
     parser.register(*args, **kwargs)
     # Force a parse to generate the derivation history.
     parser.parse_args(Parser.ParseArgsRequest((), OptionValueContainerBuilder(), [], False))
     oshi = HelpInfoExtracter("").get_option_scope_help_info("", parser, False)
     return HelpFormatter(
         show_advanced=show_advanced, show_deprecated=show_deprecated, color=False
     ).format_options(oshi)
 def do_test(args, kwargs, expected_default_str):
     # Defaults are computed in the parser and added into the kwargs, so we
     # must jump through this hoop in this test.
     parser = Parser(
         env={},
         config=Config.load([]),
         scope_info=GlobalOptions.get_scope_info(),
     )
     parser.register(*args, **kwargs)
     oshi = HelpInfoExtracter(parser.scope).get_option_scope_help_info(
         "description", parser, False)
     assert oshi.description == "description"
     assert len(oshi.basic) == 1
     ohi = oshi.basic[0]
     assert to_help_str(ohi.default) == expected_default_str
Beispiel #5
0
    def parse_bootstrap_options(env: Mapping[str, str], args: Sequence[str],
                                config: Config) -> Options:
        bootstrap_options = Options.create(
            env=env,
            config=config,
            known_scope_infos=[GlobalOptions.get_scope_info()],
            args=args,
        )

        for options_info in collect_options_info(BootstrapOptions):
            # Only use of Options.register?
            bootstrap_options.register(GLOBAL_SCOPE, *options_info.flag_names,
                                       **options_info.flag_options)

        return bootstrap_options
Beispiel #6
0
 def do_test(args, kwargs, expected_default):
     # Defaults are computed in the parser and added into the kwargs, so we
     # must jump through this hoop in this test.
     parser = Parser(
         env={},
         config=Config.load([]),
         scope_info=GlobalOptions.get_scope_info(),
         parent_parser=None,
         option_tracker=OptionTracker(),
     )
     parser.register(*args, **kwargs)
     oshi = HelpInfoExtracter.get_option_scope_help_info_from_parser(
         parser).basic
     assert len(oshi) == 1
     ohi = oshi[0]
     assert ohi.default == expected_default
Beispiel #7
0
    def parse_bootstrap_options(env: Mapping[str, str], args: Sequence[str],
                                config: Config) -> Options:
        bootstrap_options = Options.create(
            env=env,
            config=config,
            known_scope_infos=[GlobalOptions.get_scope_info()],
            args=args,
        )

        def register_global(*args, **kwargs):
            ## Only use of Options.register?
            bootstrap_options.register(GLOBAL_SCOPE, *args, **kwargs)

        GlobalOptions.register_bootstrap_options(register_global)
        opts = bootstrap_options.for_global_scope()
        if opts.v2 and not opts.v1 and opts.backend_packages == []:
            is_v2_exclusive.set()
        return bootstrap_options
Beispiel #8
0
    def run_goal_rule(
        self,
        goal: type[Goal],
        *,
        global_args: Iterable[str] | None = None,
        args: Iterable[str] | None = None,
        env: Mapping[str, str] | None = None,
        env_inherit: set[str] | None = None,
    ) -> GoalRuleResult:
        merged_args = (*(global_args or []), goal.name, *(args or []))
        self.set_options(merged_args, env=env, env_inherit=env_inherit)

        raw_specs = self.options_bootstrapper.full_options_for_scopes([
            GlobalOptions.get_scope_info(),
            goal.subsystem_cls.get_scope_info()
        ]).specs
        specs = SpecsParser(self.build_root).parse_specs(
            raw_specs,
            convert_dir_literal_to_address_literal=True,
            description_of_origin="RuleRunner.run_goal_rule()",
        )

        stdout, stderr = StringIO(), StringIO()
        console = Console(stdout=stdout,
                          stderr=stderr,
                          use_colors=False,
                          session=self.scheduler)

        exit_code = self.scheduler.run_goal_rule(
            goal,
            Params(
                specs,
                console,
                Workspace(self.scheduler),
            ),
        )

        console.flush()
        return GoalRuleResult(exit_code, stdout.getvalue(), stderr.getvalue())
Beispiel #9
0
def mock_console(
    options_bootstrapper: OptionsBootstrapper,
    *,
    stdin_content: bytes | str | None = None,
) -> Iterator[tuple[Console, StdioReader]]:
    global_bootstrap_options = options_bootstrapper.bootstrap_options.for_global_scope(
    )
    colors = (options_bootstrapper.full_options_for_scopes(
        [GlobalOptions.get_scope_info()],
        allow_unknown_options=True).for_global_scope().colors)

    with initialize_stdio(global_bootstrap_options), stdin_context(
            stdin_content) as stdin, temporary_file(
                binary_mode=False) as stdout, temporary_file(
                    binary_mode=False) as stderr, stdio_destination(
                        stdin_fileno=stdin.fileno(),
                        stdout_fileno=stdout.fileno(),
                        stderr_fileno=stderr.fileno(),
                    ):
        # NB: We yield a Console without overriding the destination argument, because we have
        # already done a sys.std* level replacement. The replacement is necessary in order for
        # InteractiveProcess to have native file handles to interact with.
        yield Console(use_colors=colors), StdioReader(
            _stdout=Path(stdout.name), _stderr=Path(stderr.name))
Beispiel #10
0
def parser() -> Parser:
    return Parser(
        env={},
        config=Config.load([]),
        scope_info=GlobalOptions.get_scope_info(),
    )