Пример #1
0
def run_black(
    rule_runner: RuleRunner,
    targets: list[Target],
    *,
    extra_args: list[str] | None = None
) -> tuple[tuple[LintResult, ...], FmtResult]:
    rule_runner.set_options(
        [
            "--backend-packages=pants.backend.python.lint.black",
            *(extra_args or ())
        ],
        env_inherit={"PATH", "PYENV_ROOT", "HOME"},
    )
    field_sets = [BlackFieldSet.create(tgt) for tgt in targets]
    lint_results = rule_runner.request(LintResults, [BlackRequest(field_sets)])
    input_sources = rule_runner.request(
        SourceFiles,
        [
            SourceFilesRequest(field_set.sources for field_set in field_sets),
        ],
    )
    fmt_result = rule_runner.request(
        FmtResult,
        [
            BlackRequest(field_sets,
                         prior_formatter_result=input_sources.snapshot),
        ],
    )
    return lint_results.results, fmt_result
Пример #2
0
def run_black(
    rule_runner: RuleRunner, targets: list[Target], *, extra_args: list[str] | None = None
) -> FmtResult:
    rule_runner.set_options(
        ["--backend-packages=pants.backend.python.lint.black", *(extra_args or ())],
        # We propagate LANG and LC_ALL to satisfy click, which black depends upon. Without this we
        # see something like the following in CI:
        #
        # RuntimeError: Click will abort further execution because Python was configured to use
        # ASCII as encoding for the environment. Consult
        # https://click.palletsprojects.com/unicode-support/ for mitigation steps.
        #
        # This system supports the C.UTF-8 locale which is recommended. You might be able to
        # resolve your issue by exporting the following environment variables:
        #
        #     export LC_ALL=C.UTF-8
        #     export LANG=C.UTF-8
        #
        env_inherit={"PATH", "PYENV_ROOT", "HOME", "LANG", "LC_ALL"},
    )
    field_sets = [BlackFieldSet.create(tgt) for tgt in targets]
    input_sources = rule_runner.request(
        SourceFiles,
        [
            SourceFilesRequest(field_set.source for field_set in field_sets),
        ],
    )
    fmt_result = rule_runner.request(
        FmtResult,
        [
            BlackRequest(field_sets, snapshot=input_sources.snapshot),
        ],
    )
    return fmt_result
Пример #3
0
def run_black(
    rule_runner: RuleRunner,
    targets: List[Target],
    *,
    config: Optional[str] = None,
    passthrough_args: Optional[str] = None,
    skip: bool = False,
) -> Tuple[Sequence[LintResult], FmtResult]:
    args = ["--backend-packages=pants.backend.python.lint.black"]
    if config is not None:
        rule_runner.create_file(relpath="pyproject.toml", contents=config)
        args.append("--black-config=pyproject.toml")
    if passthrough_args:
        args.append(f"--black-args='{passthrough_args}'")
    if skip:
        args.append("--black-skip")
    rule_runner.set_options(args)
    field_sets = [BlackFieldSet.create(tgt) for tgt in targets]
    lint_results = rule_runner.request(LintResults, [BlackRequest(field_sets)])
    input_sources = rule_runner.request(
        SourceFiles,
        [
            SourceFilesRequest(field_set.sources for field_set in field_sets),
        ],
    )
    fmt_result = rule_runner.request(
        FmtResult,
        [
            BlackRequest(field_sets,
                         prior_formatter_result=input_sources.snapshot),
        ],
    )
    return lint_results.results, fmt_result
Пример #4
0
 def run_black(
     self,
     targets: List[TargetWithOrigin],
     *,
     config: Optional[str] = None,
     passthrough_args: Optional[str] = None,
     skip: bool = False,
 ) -> Tuple[LintResults, FmtResult]:
     args = ["--backend-packages2=pants.backend.python.lint.black"]
     if config is not None:
         self.create_file(relpath="pyproject.toml", contents=config)
         args.append("--black-config=pyproject.toml")
     if passthrough_args:
         args.append(f"--black-args='{passthrough_args}'")
     if skip:
         args.append("--black-skip")
     options_bootstrapper = create_options_bootstrapper(args=args)
     field_sets = [BlackFieldSet.create(tgt) for tgt in targets]
     lint_results = self.request_single_product(
         LintResults, Params(BlackRequest(field_sets),
                             options_bootstrapper))
     input_sources = self.request_single_product(
         SourceFiles,
         Params(
             AllSourceFilesRequest(field_set.sources
                                   for field_set in field_sets),
             options_bootstrapper,
         ),
     )
     fmt_result = self.request_single_product(
         FmtResult,
         Params(
             BlackRequest(field_sets,
                          prior_formatter_result=input_sources.snapshot),
             options_bootstrapper,
         ),
     )
     return lint_results, fmt_result