async def flake8_lint_partition( partition: Flake8Partition, flake8: Flake8, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> LintResult: requirements_pex_request = Get[Pex](PexRequest( output_filename="flake8.pex", requirements=PexRequirements(flake8.get_requirement_specs()), interpreter_constraints=(partition.interpreter_constraints or PexInterpreterConstraints( flake8.default_interpreter_constraints)), entry_point=flake8.get_entry_point(), )) config_path: Optional[str] = flake8.options.config config_snapshot_request = Get[Snapshot](PathGlobs( globs=[config_path] if config_path else [], glob_match_error_behavior=GlobMatchErrorBehavior.error, description_of_origin="the option `--flake8-config`", )) all_source_files_request = Get[SourceFiles](AllSourceFilesRequest( field_set.sources for field_set in partition.field_sets)) specified_source_files_request = Get[SourceFiles]( SpecifiedSourceFilesRequest((field_set.sources, field_set.origin) for field_set in partition.field_sets)) requirements_pex, config_snapshot, all_source_files, specified_source_files = cast( Tuple[Pex, Snapshot, SourceFiles, SourceFiles], await MultiGet([ requirements_pex_request, config_snapshot_request, all_source_files_request, specified_source_files_request, ]), ) input_digest = await Get[Digest](MergeDigests( (all_source_files.snapshot.digest, requirements_pex.digest, config_snapshot.digest))) address_references = ", ".join( sorted(field_set.address.reference() for field_set in partition.field_sets)) process = requirements_pex.create_process( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path="./flake8.pex", pex_args=generate_args(specified_source_files=specified_source_files, flake8=flake8), input_digest=input_digest, description= (f"Run Flake8 on {pluralize(len(partition.field_sets), 'target')}: {address_references}." ), ) result = await Get[FallibleProcessResult](Process, process) return LintResult.from_fallible_process_result(result, linter_name="Flake8")
async def setup(setup_request: SetupRequest, isort: Isort) -> Setup: requirements_pex_request = Get( Pex, PexRequest( output_filename="isort.pex", requirements=PexRequirements(isort.all_requirements), interpreter_constraints=PexInterpreterConstraints( isort.interpreter_constraints), entry_point=isort.entry_point, ), ) config_digest_request = Get( Digest, PathGlobs( globs=isort.config, glob_match_error_behavior=GlobMatchErrorBehavior.error, conjunction=GlobExpansionConjunction.all_match, description_of_origin="the option `--isort-config`", ), ) source_files_request = Get( SourceFiles, SourceFilesRequest(field_set.sources for field_set in setup_request.request.field_sets), ) source_files, requirements_pex, config_digest = await MultiGet( source_files_request, requirements_pex_request, config_digest_request) source_files_snapshot = ( source_files.snapshot if setup_request.request.prior_formatter_result is None else setup_request.request.prior_formatter_result) input_digest = await Get( Digest, MergeDigests((source_files_snapshot.digest, requirements_pex.digest, config_digest)), ) address_references = ", ".join( sorted(field_set.address.spec for field_set in setup_request.request.field_sets)) process = await Get( Process, PexProcess( requirements_pex, argv=generate_args(source_files=source_files, isort=isort, check_only=setup_request.check_only), input_digest=input_digest, output_files=source_files_snapshot.files, description= (f"Run isort on {pluralize(len(setup_request.request.field_sets), 'target')}: " f"{address_references}."), ), ) return Setup(process, original_digest=source_files_snapshot.digest)
def create_pex_and_get_all_data( self, *, requirements=PexRequirements(), entry_point=None, interpreter_constraints=PexInterpreterConstraints(), input_files: Optional[Digest] = None, additional_pants_args: Tuple[str, ...] = (), additional_pex_args: Tuple[str, ...] = (), ) -> Dict: request = CreatePex( output_filename="test.pex", requirements=requirements, interpreter_constraints=interpreter_constraints, entry_point=entry_point, input_files_digest=input_files, additional_args=additional_pex_args, ) requirements_pex = self.request_single_product( Pex, Params( request, create_options_bootstrapper( args=["--backend-packages2=pants.backend.python", *additional_pants_args] ), ), ) self.scheduler.materialize_directory( DirectoryToMaterialize(requirements_pex.directory_digest), ) with zipfile.ZipFile(os.path.join(self.build_root, "test.pex"), "r") as pex: with pex.open("PEX-INFO", "r") as pex_info: pex_info_content = pex_info.readline().decode() pex_list = pex.namelist() return {"pex": requirements_pex, "info": json.loads(pex_info_content), "files": pex_list}
def test_group_field_sets_by_constraints() -> None: py2_fs = MockFieldSet.create_for_test("//:py2", ">=2.7,<3") py3_fs = [ MockFieldSet.create_for_test("//:py3", "==3.6.*"), MockFieldSet.create_for_test("//:py3_second", "==3.6.*"), ] no_constraints_fs = MockFieldSet.create_for_test("//:no_constraints", None) assert PexInterpreterConstraints.group_field_sets_by_constraints( [py2_fs, *py3_fs, no_constraints_fs], python_setup=create_subsystem(PythonSetup, interpreter_constraints=[]), ) == FrozenDict({ PexInterpreterConstraints(): (no_constraints_fs, ), PexInterpreterConstraints(["CPython>=2.7,<3"]): (py2_fs, ), PexInterpreterConstraints(["CPython==3.6.*"]): tuple(py3_fs), })
def create_pex_and_get_all_data( self, *, requirements=PexRequirements(), entry_point=None, interpreter_constraints=PexInterpreterConstraints(), input_files: Digest = None) -> (Dict, List[str]): def hashify_optional_collection(iterable): return tuple(sorted(iterable)) if iterable is not None else tuple() request = CreatePex( output_filename="test.pex", requirements=requirements, interpreter_constraints=interpreter_constraints, entry_point=entry_point, input_files_digest=input_files, ) requirements_pex = self.request_single_product( Pex, Params(request, PythonSetup.global_instance(), SubprocessEnvironment.global_instance(), PythonNativeCode.global_instance())) self.scheduler.materialize_directory( DirectoryToMaterialize(requirements_pex.directory_digest), ) with zipfile.ZipFile(os.path.join(self.build_root, "test.pex"), "r") as pex: with pex.open("PEX-INFO", "r") as pex_info: pex_info_content = pex_info.readline().decode() pex_list = pex.namelist() return { 'pex': requirements_pex, 'info': json.loads(pex_info_content), 'files': pex_list }
def test_interpreter_constraints(self) -> None: constraints = PexInterpreterConstraints( constraint_set=("CPython>=2.7,<3", "CPython>=3.6,<4")) pex_info = self.create_pex_and_get_pex_info( interpreter_constraints=constraints) self.assertEqual(set(pex_info["interpreter_constraints"]), set(constraints.constraint_set))
def get_black_input( wrapped_target: FormattablePythonTarget, black: Black, ) -> BlackInput: config_path = black.get_options().config config_snapshot = yield Get(Snapshot, PathGlobs(include=(config_path, ))) resolved_requirements_pex = yield Get( Pex, CreatePex( output_filename="black.pex", requirements=PexRequirements( requirements=tuple(black.get_requirement_specs())), interpreter_constraints=PexInterpreterConstraints( constraint_set=tuple(black.default_interpreter_constraints)), entry_point=black.get_entry_point(), )) target = wrapped_target.target sources_digest = target.sources.snapshot.directory_digest all_input_digests = [ sources_digest, resolved_requirements_pex.directory_digest, config_snapshot.directory_digest, ] merged_input_files = yield Get( Digest, DirectoriesToMerge(directories=tuple(all_input_digests)), ) yield BlackInput(config_path, resolved_requirements_pex, merged_input_files)
async def setup_black(wrapped_target: FormattablePythonTarget, black: Black) -> BlackSetup: config_path: Optional[str] = black.get_options().config config_snapshot = await Get(Snapshot, PathGlobs(include=(config_path,))) resolved_requirements_pex = await Get( Pex, CreatePex( output_filename="black.pex", requirements=PexRequirements(requirements=tuple(black.get_requirement_specs())), interpreter_constraints=PexInterpreterConstraints( constraint_set=tuple(black.default_interpreter_constraints) ), entry_point=black.get_entry_point(), ) ) sources_digest = wrapped_target.target.sources.snapshot.directory_digest merged_input_files = await Get( Digest, DirectoriesToMerge( directories=( sources_digest, resolved_requirements_pex.directory_digest, config_snapshot.directory_digest, ) ), ) return BlackSetup(config_path, resolved_requirements_pex, merged_input_files)
def test_interpreter_constraints(self) -> None: constraints = PexInterpreterConstraints( ["CPython>=2.7,<3", "CPython>=3.6"]) pex_info = self.create_pex_and_get_pex_info( interpreter_constraints=constraints) assert set(pex_info["interpreter_constraints"]) == set( constraints.constraints)
async def setup( request: SetupRequest, isort: Isort, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> Setup: adaptors_with_origins = request.formatter.adaptors_with_origins requirements_pex = await Get[Pex](PexRequest( output_filename="isort.pex", requirements=PexRequirements(isort.get_requirement_specs()), interpreter_constraints=PexInterpreterConstraints( isort.default_interpreter_constraints), entry_point=isort.get_entry_point(), )) config_path: Optional[List[str]] = isort.options.config config_snapshot = await Get[Snapshot](PathGlobs( globs=config_path or (), glob_match_error_behavior=GlobMatchErrorBehavior.error, conjunction=GlobExpansionConjunction.all_match, description_of_origin="the option `--isort-config`", )) if request.formatter.prior_formatter_result is None: all_source_files = await Get[SourceFiles](LegacyAllSourceFilesRequest( adaptor_with_origin.adaptor for adaptor_with_origin in adaptors_with_origins)) all_source_files_snapshot = all_source_files.snapshot else: all_source_files_snapshot = request.formatter.prior_formatter_result specified_source_files = await Get[SourceFiles]( LegacySpecifiedSourceFilesRequest(adaptors_with_origins)) merged_input_files = await Get[Digest](DirectoriesToMerge(directories=( all_source_files_snapshot.directory_digest, requirements_pex.directory_digest, config_snapshot.directory_digest, )), ) address_references = ", ".join( sorted(adaptor_with_origin.adaptor.address.reference() for adaptor_with_origin in adaptors_with_origins)) process_request = requirements_pex.create_execute_request( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path="./isort.pex", pex_args=generate_args( specified_source_files=specified_source_files, isort=isort, check_only=request.check_only, ), input_files=merged_input_files, output_files=all_source_files_snapshot.files, description=f"Run isort for {address_references}", ) return Setup(process_request)
async def pylint_lint(request: PylintRequest, pylint: Pylint, python_setup: PythonSetup) -> LintResults: if pylint.skip: return LintResults() plugin_targets_request = Get( TransitiveTargets, Addresses( Address.parse(plugin_addr) for plugin_addr in pylint.source_plugins), ) linted_targets_request = Get( Targets, Addresses(field_set.address for field_set in request.field_sets)) plugin_targets, linted_targets = await MultiGet(plugin_targets_request, linted_targets_request) plugin_targets_compatibility_fields = tuple( plugin_tgt[PythonInterpreterCompatibility] for plugin_tgt in plugin_targets.closure if plugin_tgt.has_field(PythonInterpreterCompatibility)) # Pylint needs direct dependencies in the chroot to ensure that imports are valid. However, it # doesn't lint those direct dependencies nor does it care about transitive dependencies. per_target_dependencies = await MultiGet( Get(Targets, DependenciesRequest(field_set.dependencies)) for field_set in request.field_sets) # We batch targets by their interpreter constraints to ensure, for example, that all Python 2 # targets run together and all Python 3 targets run together. interpreter_constraints_to_target_setup = defaultdict(set) for field_set, tgt, dependencies in zip(request.field_sets, linted_targets, per_target_dependencies): target_setup = PylintTargetSetup(field_set, Targets([tgt, *dependencies])) interpreter_constraints = PexInterpreterConstraints.create_from_compatibility_fields( ( *(tgt.get(PythonInterpreterCompatibility) for tgt in [tgt, *dependencies]), *plugin_targets_compatibility_fields, ), python_setup, ) or PexInterpreterConstraints(pylint.interpreter_constraints) interpreter_constraints_to_target_setup[interpreter_constraints].add( target_setup) partitions = (PylintPartition( tuple( sorted(target_setups, key=lambda tgt_setup: tgt_setup.field_set.address)), interpreter_constraints, Targets(plugin_targets.closure), ) for interpreter_constraints, target_setups in sorted( interpreter_constraints_to_target_setup.items())) partitioned_results = await MultiGet( Get(LintResult, PylintPartition, partition) for partition in partitions) return LintResults(partitioned_results)
async def setup( setup_request: SetupRequest, docformatter: Docformatter, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> Setup: requirements_pex_request = Get[Pex](PexRequest( output_filename="docformatter.pex", requirements=PexRequirements(docformatter.get_requirement_specs()), interpreter_constraints=PexInterpreterConstraints( docformatter.default_interpreter_constraints), entry_point=docformatter.get_entry_point(), )) all_source_files_request = Get[SourceFiles](AllSourceFilesRequest( field_set.sources for field_set in setup_request.request.field_sets)) specified_source_files_request = Get[SourceFiles]( SpecifiedSourceFilesRequest( (field_set.sources, field_set.origin) for field_set in setup_request.request.field_sets)) requests: List[Get] = [ requirements_pex_request, specified_source_files_request ] if setup_request.request.prior_formatter_result is None: requests.append(all_source_files_request) requirements_pex, specified_source_files, *rest = cast( Union[Tuple[Pex, SourceFiles], Tuple[Pex, SourceFiles, SourceFiles]], await MultiGet(requests), ) all_source_files_snapshot = ( setup_request.request.prior_formatter_result if setup_request.request.prior_formatter_result else rest[0].snapshot) input_digest = await Get[Digest](MergeDigests( (all_source_files_snapshot.digest, requirements_pex.digest))) address_references = ", ".join( sorted(field_set.address.reference() for field_set in setup_request.request.field_sets)) process = requirements_pex.create_process( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path="./docformatter.pex", pex_args=generate_args( specified_source_files=specified_source_files, docformatter=docformatter, check_only=setup_request.check_only, ), input_digest=input_digest, output_files=all_source_files_snapshot.files, description= (f"Run Docformatter on {pluralize(len(setup_request.request.field_sets), 'target')}: " f"{address_references}."), ) return Setup(process, original_digest=all_source_files_snapshot.digest)
async def setup( request: SetupRequest, black: Black, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> Setup: requirements_pex = await Get[Pex](PexRequest( output_filename="black.pex", requirements=PexRequirements(black.get_requirement_specs()), interpreter_constraints=PexInterpreterConstraints( black.default_interpreter_constraints), entry_point=black.get_entry_point(), )) config_path: Optional[str] = black.options.config config_snapshot = await Get[Snapshot](PathGlobs( globs=tuple([config_path] if config_path else []), glob_match_error_behavior=GlobMatchErrorBehavior.error, description_of_origin="the option `--black-config`", )) if request.configs.prior_formatter_result is None: all_source_files = await Get[SourceFiles](AllSourceFilesRequest( config.sources for config in request.configs)) all_source_files_snapshot = all_source_files.snapshot else: all_source_files_snapshot = request.configs.prior_formatter_result specified_source_files = await Get[SourceFiles]( SpecifiedSourceFilesRequest( (config.sources, config.origin) for config in request.configs)) merged_input_files = await Get[Digest](DirectoriesToMerge(directories=( all_source_files_snapshot.directory_digest, requirements_pex.directory_digest, config_snapshot.directory_digest, )), ) address_references = ", ".join( sorted(config.address.reference() for config in request.configs)) process = requirements_pex.create_process( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path="./black.pex", pex_args=generate_args( specified_source_files=specified_source_files, black=black, check_only=request.check_only, ), input_files=merged_input_files, output_files=all_source_files_snapshot.files, description= (f"Run Black on {pluralize(len(request.configs), 'target')}: {address_references}." ), ) return Setup(process)
async def setup_lambdex(lambdex: Lambdex) -> LambdexSetup: requirements_pex = await Get[Pex](PexRequest( output_filename="lambdex.pex", requirements=PexRequirements(lambdex.get_requirement_specs()), interpreter_constraints=PexInterpreterConstraints( lambdex.default_interpreter_constraints), entry_point=lambdex.get_entry_point(), )) return LambdexSetup(requirements_pex=requirements_pex, )
async def setup( request: SetupRequest, isort: Isort, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> Setup: requirements_pex = await Get[Pex](PexRequest( output_filename="isort.pex", requirements=PexRequirements(isort.get_requirement_specs()), interpreter_constraints=PexInterpreterConstraints( isort.default_interpreter_constraints), entry_point=isort.get_entry_point(), )) config_path: Optional[List[str]] = isort.options.config config_snapshot = await Get[Snapshot](PathGlobs( globs=config_path or (), glob_match_error_behavior=GlobMatchErrorBehavior.error, conjunction=GlobExpansionConjunction.all_match, description_of_origin="the option `--isort-config`", )) if request.configs.prior_formatter_result is None: all_source_files = await Get[SourceFiles](AllSourceFilesRequest( config.sources for config in request.configs)) all_source_files_snapshot = all_source_files.snapshot else: all_source_files_snapshot = request.configs.prior_formatter_result specified_source_files = await Get[SourceFiles]( SpecifiedSourceFilesRequest( (config.sources, config.origin) for config in request.configs)) merged_input_files = await Get[Digest](MergeDigests( (all_source_files_snapshot.digest, requirements_pex.digest, config_snapshot.digest)), ) address_references = ", ".join( sorted(config.address.reference() for config in request.configs)) process = requirements_pex.create_process( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path="./isort.pex", pex_args=generate_args( specified_source_files=specified_source_files, isort=isort, check_only=request.check_only, ), input_files=merged_input_files, output_files=all_source_files_snapshot.files, description= (f"Run isort on {pluralize(len(request.configs), 'target')}: {address_references}." ), ) return Setup(process, original_digest=all_source_files_snapshot.digest)
async def setup_black(setup_request: SetupRequest, black: Black) -> Setup: requirements_pex_request = Get( Pex, PexRequest( output_filename="black.pex", internal_only=True, requirements=PexRequirements(black.all_requirements), interpreter_constraints=PexInterpreterConstraints( black.interpreter_constraints), entry_point=black.entry_point, ), ) config_digest_request = Get( Digest, PathGlobs( globs=[black.config] if black.config else [], glob_match_error_behavior=GlobMatchErrorBehavior.error, description_of_origin="the option `--black-config`", ), ) source_files_request = Get( SourceFiles, SourceFilesRequest(field_set.sources for field_set in setup_request.request.field_sets), ) source_files, requirements_pex, config_digest = await MultiGet( source_files_request, requirements_pex_request, config_digest_request) source_files_snapshot = ( source_files.snapshot if setup_request.request.prior_formatter_result is None else setup_request.request.prior_formatter_result) input_digest = await Get( Digest, MergeDigests((source_files_snapshot.digest, requirements_pex.digest, config_digest)), ) process = await Get( Process, PexProcess( requirements_pex, argv=generate_args(source_files=source_files, black=black, check_only=setup_request.check_only), input_digest=input_digest, output_files=source_files_snapshot.files, description= f"Run Black on {pluralize(len(setup_request.request.field_sets), 'file')}.", level=LogLevel.DEBUG, ), ) return Setup(process, original_digest=source_files_snapshot.digest)
async def setup_setuptools(setuptools: Setuptools) -> SetuptoolsSetup: # Note that this pex has no entrypoint. We use it to run our generated setup.py, which # in turn imports from and invokes setuptools. requirements_pex = await Get[Pex](PexRequest( output_filename="setuptools.pex", requirements=PexRequirements(setuptools.get_requirement_specs()), interpreter_constraints=PexInterpreterConstraints( setuptools.default_interpreter_constraints), )) return SetuptoolsSetup(requirements_pex=requirements_pex, )
def create_pex_and_get_pex_info( self, *, requirements=PexRequirements(), entry_point=None, interpreter_constraints=PexInterpreterConstraints(), input_files: Digest = None) -> Dict: return self.create_pex_and_get_all_data( requirements=requirements, entry_point=entry_point, interpreter_constraints=interpreter_constraints, input_files=input_files)['info']
async def setup_coverage(coverage: CoverageSubsystem) -> CoverageSetup: pex = await Get( Pex, PexRequest( output_filename="coverage.pex", internal_only=True, requirements=PexRequirements(coverage.all_requirements), interpreter_constraints=PexInterpreterConstraints(coverage.interpreter_constraints), entry_point=coverage.entry_point, ), ) return CoverageSetup(pex)
async def setup_lambdex(lambdex: Lambdex) -> LambdexSetup: requirements_pex = await Get( Pex, PexRequest( output_filename="lambdex.pex", requirements=PexRequirements(lambdex.all_requirements), interpreter_constraints=PexInterpreterConstraints( lambdex.interpreter_constraints), entry_point=lambdex.entry_point, ), ) return LambdexSetup(requirements_pex=requirements_pex, )
async def setup_coverage(coverage: PytestCoverage) -> CoverageSetup: plugin_file_digest = await Get[Digest](InputFilesContent, COVERAGE_PLUGIN_INPUT) output_pex_filename = "coverage.pex" requirements_pex = await Get[Pex](PexRequest( output_filename=output_pex_filename, requirements=PexRequirements(coverage.get_requirement_specs()), interpreter_constraints=PexInterpreterConstraints( coverage.default_interpreter_constraints), entry_point=coverage.get_entry_point(), sources=plugin_file_digest, )) return CoverageSetup(requirements_pex)
async def setup_coverage(coverage: PytestCoverage) -> CoverageSetup: plugin_file_digest = await Get[Digest](InputFilesContent, get_coverage_plugin_input()) output_pex_filename = "coverage.pex" requirements_pex = await Get[Pex](CreatePex( output_filename=output_pex_filename, requirements=PexRequirements(coverage.get_requirement_specs()), interpreter_constraints=PexInterpreterConstraints( coverage.default_interpreter_constraints), entry_point=coverage.get_entry_point(), input_files_digest=plugin_file_digest, )) return CoverageSetup(requirements_pex)
async def setup_docformatter(setup_request: SetupRequest, docformatter: Docformatter) -> Setup: requirements_pex_request = Get( Pex, PexRequest( output_filename="docformatter.pex", internal_only=True, requirements=PexRequirements(docformatter.all_requirements), interpreter_constraints=PexInterpreterConstraints( docformatter.interpreter_constraints), entry_point=docformatter.entry_point, ), ) source_files_request = Get( SourceFiles, SourceFilesRequest(field_set.sources for field_set in setup_request.request.field_sets), ) source_files, requirements_pex = await MultiGet(source_files_request, requirements_pex_request) source_files_snapshot = ( source_files.snapshot if setup_request.request.prior_formatter_result is None else setup_request.request.prior_formatter_result) input_digest = await Get( Digest, MergeDigests((source_files_snapshot.digest, requirements_pex.digest))) process = await Get( Process, PexProcess( requirements_pex, argv=generate_args( source_files=source_files, docformatter=docformatter, check_only=setup_request.check_only, ), input_digest=input_digest, output_files=source_files_snapshot.files, description= (f"Run Docformatter on {pluralize(len(setup_request.request.field_sets), 'file')}." ), level=LogLevel.DEBUG, ), ) return Setup(process, original_digest=source_files_snapshot.digest)
async def setup( request: SetupRequest, docformatter: Docformatter, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> Setup: adaptors_with_origins = request.formatter.adaptors_with_origins requirements_pex = await Get[Pex](CreatePex( output_filename="docformatter.pex", requirements=PexRequirements(docformatter.get_requirement_specs()), interpreter_constraints=PexInterpreterConstraints( docformatter.default_interpreter_constraints), entry_point=docformatter.get_entry_point(), )) if request.formatter.prior_formatter_result is None: all_source_files = await Get[SourceFiles](AllSourceFilesRequest( adaptor_with_origin.adaptor for adaptor_with_origin in adaptors_with_origins)) all_source_files_snapshot = all_source_files.snapshot else: all_source_files_snapshot = request.formatter.prior_formatter_result specified_source_files = await Get[SourceFiles]( SpecifiedSourceFilesRequest(adaptors_with_origins)) merged_input_files = await Get[Digest](DirectoriesToMerge(directories=( all_source_files_snapshot.directory_digest, requirements_pex.directory_digest, )), ) address_references = ", ".join( sorted(adaptor_with_origin.adaptor.address.reference() for adaptor_with_origin in adaptors_with_origins)) process_request = requirements_pex.create_execute_request( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path="./docformatter.pex", pex_args=generate_args( specified_source_files=specified_source_files, docformatter=docformatter, check_only=request.check_only, ), input_files=merged_input_files, output_files=all_source_files_snapshot.files, description=f"Run docformatter for {address_references}", ) return Setup(process_request)
async def setup_setuptools(setuptools: Setuptools) -> SetuptoolsSetup: # Note that this pex has no entrypoint. We use it to run our generated setup.py, which # in turn imports from and invokes setuptools. requirements_pex = await Get( Pex, PexRequest( output_filename="setuptools.pex", internal_only=True, requirements=PexRequirements(setuptools.all_requirements), interpreter_constraints=PexInterpreterConstraints( setuptools.interpreter_constraints), ), ) return SetuptoolsSetup(requirements_pex=requirements_pex, )
async def setup( request: SetupRequest, docformatter: Docformatter, python_setup: PythonSetup, subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> Setup: requirements_pex = await Get[Pex](PexRequest( output_filename="docformatter.pex", requirements=PexRequirements(docformatter.get_requirement_specs()), interpreter_constraints=PexInterpreterConstraints( docformatter.default_interpreter_constraints), entry_point=docformatter.get_entry_point(), )) if request.configs.prior_formatter_result is None: all_source_files = await Get[SourceFiles](AllSourceFilesRequest( config.sources for config in request.configs)) all_source_files_snapshot = all_source_files.snapshot else: all_source_files_snapshot = request.configs.prior_formatter_result specified_source_files = await Get[SourceFiles]( SpecifiedSourceFilesRequest( (config.sources, config.origin) for config in request.configs)) merged_input_files = await Get[Digest](DirectoriesToMerge(directories=( all_source_files_snapshot.directory_digest, requirements_pex.directory_digest, )), ) address_references = ", ".join( sorted(config.address.reference() for config in request.configs)) process = requirements_pex.create_execute_request( python_setup=python_setup, subprocess_encoding_environment=subprocess_encoding_environment, pex_path="./docformatter.pex", pex_args=generate_args( specified_source_files=specified_source_files, docformatter=docformatter, check_only=request.check_only, ), input_files=merged_input_files, output_files=all_source_files_snapshot.files, description=( f"Run Docformatter on {pluralize(len(request.configs), 'target')}: " f"{address_references}."), ) return Setup(process)
async def setup_black(black: Black) -> BlackSetup: config_path: Optional[str] = black.get_options().config config_snapshot = await Get[Snapshot](PathGlobs(include=(config_path, ))) requirements_pex = await Get[Pex](CreatePex( output_filename="black.pex", requirements=PexRequirements( requirements=tuple(black.get_requirement_specs())), interpreter_constraints=PexInterpreterConstraints( constraint_set=tuple(black.default_interpreter_constraints)), entry_point=black.get_entry_point(), )) return BlackSetup( requirements_pex=requirements_pex, config_snapshot=config_snapshot, passthrough_args=black.get_args(), )
async def setup_isort(isort: Isort) -> IsortSetup: config_path: Optional[List[str]] = isort.get_options().config config_snapshot = await Get[Snapshot](PathGlobs(include=config_path or ())) requirements_pex = await Get[Pex](CreatePex( output_filename="isort.pex", requirements=PexRequirements( requirements=tuple(isort.get_requirement_specs())), interpreter_constraints=PexInterpreterConstraints( constraint_set=tuple(isort.default_interpreter_constraints)), entry_point=isort.get_entry_point(), )) return IsortSetup( requirements_pex=requirements_pex, config_snapshot=config_snapshot, passthrough_args=isort.get_args(), )
def create_pex_and_get_all_data( self, *, requirements=PexRequirements(), entry_point=None, interpreter_constraints=PexInterpreterConstraints(), platforms=PexPlatforms(), sources: Optional[Digest] = None, additional_inputs: Optional[Digest] = None, additional_pants_args: Tuple[str, ...] = (), additional_pex_args: Tuple[str, ...] = (), ) -> Dict: request = PexRequest( output_filename="test.pex", internal_only=True, requirements=requirements, interpreter_constraints=interpreter_constraints, platforms=platforms, entry_point=entry_point, sources=sources, additional_inputs=additional_inputs, additional_args=additional_pex_args, ) pex = self.request_single_product( Pex, Params( request, create_options_bootstrapper(args=[ "--backend-packages=pants.backend.python", *additional_pants_args ]), ), ) self.scheduler.write_digest(pex.digest) pex_path = os.path.join(self.build_root, "test.pex") with zipfile.ZipFile(pex_path, "r") as zipfp: with zipfp.open("PEX-INFO", "r") as pex_info: pex_info_content = pex_info.readline().decode() pex_list = zipfp.namelist() return { "pex": pex, "local_path": pex_path, "info": json.loads(pex_info_content), "files": pex_list, }
def create_pex_and_get_pex_info( self, *, requirements=PexRequirements(), entry_point=None, interpreter_constraints=PexInterpreterConstraints(), input_files: Optional[Digest] = None, additional_args: Tuple[str, ...] = (), ) -> Dict: return cast( Dict, self.create_pex_and_get_all_data( requirements=requirements, entry_point=entry_point, interpreter_constraints=interpreter_constraints, input_files=input_files, additional_args=additional_args, )['info'])