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 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_isort(setup_request: SetupRequest, isort: Isort) -> Setup: isort_pex_get = Get( VenvPex, PexRequest( output_filename="isort.pex", internal_only=True, requirements=isort.pex_requirements(), interpreter_constraints=isort.interpreter_constraints, main=isort.main, ), ) source_files_get = Get( SourceFiles, SourceFilesRequest(field_set.source for field_set in setup_request.request.field_sets), ) source_files, isort_pex = await MultiGet(source_files_get, isort_pex_get) source_files_snapshot = ( source_files.snapshot if setup_request.request.prior_formatter_result is None else setup_request.request.prior_formatter_result) config_files = await Get(ConfigFiles, ConfigFilesRequest, isort.config_request(source_files_snapshot.dirs)) # Isort 5+ changes how config files are handled. Determine which semantics we should use. is_isort5 = False if isort.config: isort_info = await Get(PexResolveInfo, VenvPex, isort_pex) is_isort5 = any( dist_info.project_name == "isort" and dist_info.version.major >= 5 for dist_info in isort_info) input_digest = await Get( Digest, MergeDigests( (source_files_snapshot.digest, config_files.snapshot.digest))) process = await Get( Process, VenvPexProcess( isort_pex, argv=generate_argv(source_files, isort, is_isort5=is_isort5, 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), 'file')}.", level=LogLevel.DEBUG, ), ) return Setup(process, original_digest=source_files_snapshot.digest)
def execute(self): targets = self.get_targets(self.is_non_synthetic_python_target) with self.invalidated(targets=targets) as invalidation_check: if not invalidation_check.invalid_vts: logging.debug(self.NOOP_MSG_HAS_TARGET_BUT_NO_SOURCE) return invalid_tgts = [vt.target for vt in invalidation_check.invalid_vts] sources = self._calculate_isortable_python_sources(invalid_tgts) if not sources: logging.debug(self.NOOP_MSG_HAS_TARGET_BUT_NO_SOURCE) return isort = self.context.products.get_data(IsortPrep.tool_instance_cls) isort_subsystem = Isort.global_instance() args = [*isort_subsystem.options.args, "--filter-files", *sources] # NB: We execute isort out of process to avoid unwanted side-effects from importing it: # https://github.com/timothycrosley/isort/issues/456 with pushd(get_buildroot()): workunit_factory = functools.partial( self.context.new_workunit, name="run-isort", labels=[WorkUnitLabel.TOOL, WorkUnitLabel.LINT], ) cmdline, exit_code = isort.run(workunit_factory, args) if exit_code != 0: raise TaskError( f"Exited with return code {exit_code} while running `{cmdline}`.", exit_code=exit_code, )
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(), )
async def isort_fmt(request: IsortRequest, isort: Isort) -> FmtResult: if isort.skip: return FmtResult.skip(formatter_name=request.name) isort_pex_get = Get(VenvPex, PexRequest, isort.to_pex_request()) config_files_get = Get(ConfigFiles, ConfigFilesRequest, isort.config_request(request.snapshot.dirs)) isort_pex, config_files = await MultiGet(isort_pex_get, config_files_get) # Isort 5+ changes how config files are handled. Determine which semantics we should use. is_isort5 = False if isort.config: isort_info = await Get(PexResolveInfo, VenvPex, isort_pex) is_isort5 = any( dist_info.project_name == "isort" and dist_info.version.major >= 5 for dist_info in isort_info) input_digest = await Get( Digest, MergeDigests((request.snapshot.digest, config_files.snapshot.digest))) result = await Get( ProcessResult, VenvPexProcess( isort_pex, argv=generate_argv(request.snapshot.files, isort, is_isort5=is_isort5), input_digest=input_digest, output_files=request.snapshot.files, description= f"Run isort on {pluralize(len(request.field_sets), 'file')}.", level=LogLevel.DEBUG, ), ) output_snapshot = await Get(Snapshot, Digest, result.output_digest) return FmtResult.create(request, result, output_snapshot, strip_chroot_path=True)
async def setup_isort(isort: Isort) -> IsortSetup: 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`", )) 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=tuple(isort.options.args), skip=isort.options.skip, )
def execute(self): targets = self.get_targets(self.is_non_synthetic_python_target) with self.invalidated(targets=targets) as invalidation_check: if not invalidation_check.invalid_vts: logging.debug(self.NOOP_MSG_HAS_TARGET_BUT_NO_SOURCE) return invalid_tgts = [vt.target for vt in invalidation_check.invalid_vts] sources = self._calculate_isortable_python_sources(invalid_tgts) if not sources: logging.debug(self.NOOP_MSG_HAS_TARGET_BUT_NO_SOURCE) return isort = self.context.products.get_data(IsortPrep.tool_instance_cls) isort_subsystem = Isort.global_instance() deprecated_conditional( lambda: self.get_passthru_args(), removal_version='1.26.0.dev3', entity_description= 'Using the old style of passthrough args for isort', hint_message="You passed arguments to isort through either the " "`--fmt-isort-passthrough-args` option or the style " "`./pants fmt.isort -- --case-sensitive --trailing-comma`. Instead, pass any " "arguments to isort like this: `./pants fmt :: " "--isort-args='--case-sensitive --trailing-comma'`.\n\n" "This change is meant to reduce confusion in how option scopes work with " "passthrough args and to prepare for isort eventually exclusively using the " "V2 implementation, which only supports `--isort-args`.", ) args = [ *self.get_passthru_args(), *isort_subsystem.get_args(), '--filter-files', *sources ] # NB: We execute isort out of process to avoid unwanted side-effects from importing it: # https://github.com/timothycrosley/isort/issues/456 with pushd(get_buildroot()): workunit_factory = functools.partial( self.context.new_workunit, name='run-isort', labels=[WorkUnitLabel.TOOL, WorkUnitLabel.LINT]) cmdline, exit_code = isort.run(workunit_factory, args) if exit_code != 0: raise TaskError( f"Exited with return code {exit_code} while running `{cmdline}`.", exit_code=exit_code)
def skip_execution(self): return self.get_options().skip or Isort.global_instance().options.skip
def skip_execution(self): return self.determine_if_skipped( formatter_subsystem=Isort.global_instance())