def test_uninitialized_global() -> None: Subsystem.reset() with pytest.raises( Subsystem.UninitializedSubsystemError, match=r"UninitializedSubsystem.*uninitialized-scope", ): UninitializedSubsystem.global_instance()
def clean_global_runtime_state(reset_subsystem=False): """Resets the global runtime state of a pants runtime for cleaner forking. :param bool reset_subsystem: Whether or not to clean Subsystem global state. """ if reset_subsystem: # Reset subsystem state. Subsystem.reset() # Reset global plugin state. BuildConfigInitializer.reset()
def test_uninitialized_scoped_instance(self) -> None: Subsystem.reset() class UninitializedOptional(Optionable): options_scope = "optional" optional = UninitializedOptional() with self.assertRaisesRegex( Subsystem.UninitializedSubsystemError, r"UninitializedSubsystem.*uninitialized-scope"): UninitializedSubsystem.scoped_instance(optional)
def test_uninitialized_scoped_instance() -> None: Subsystem.reset() class UninitializedOptional(Optionable): options_scope = "optional" optional = UninitializedOptional() with pytest.raises( Subsystem.UninitializedSubsystemError, match=r"UninitializedSubsystem.*uninitialized-scope", ): UninitializedSubsystem.scoped_instance(optional)
def run(self, start_time: float) -> ExitCode: run_tracker = RunTracker.global_instance() self._start_run(run_tracker, start_time) with maybe_profiled(self.profile_path): global_options = self.options.for_global_scope() if self.options.help_request: return self._print_help(self.options.help_request) streaming_handlers = global_options.streaming_workunits_handlers callbacks = Subsystem.get_streaming_workunit_callbacks( streaming_handlers) streaming_reporter = StreamingWorkunitHandler( self.graph_session.scheduler_session, callbacks=callbacks, report_interval_seconds=global_options. streaming_workunits_report_interval, ) goals = tuple(self.options.goals) with streaming_reporter.session(): engine_result = PANTS_FAILED_EXIT_CODE try: engine_result = self._run_v2(goals) except Exception as e: ExceptionSink.log_exception(e) self._finish_run(run_tracker, engine_result) return engine_result
def test_streaming_workunit_callbacks_bad_module(self) -> None: import_str = "nonexistent_module.AClassThatDoesntActuallyExist" with self.captured_logging(level=logging.WARNING) as captured: callables_list = Subsystem.get_streaming_workunit_callbacks( [import_str]) warnings = captured.warnings() assert len(warnings) == 1 assert len(callables_list) == 0 assert "No module named 'nonexistent_module'" in warnings[0]
def test_streaming_workunit_callbacks_bad_module(caplog) -> None: import_str = "nonexistent_module.AClassThatDoesntActuallyExist" callables_list = Subsystem.get_streaming_workunit_callbacks([import_str]) assert len(callables_list) == 0 warnings = [ record for record in caplog.records if record.levelname == "WARNING" ] assert len(warnings) == 1 assert "No module named 'nonexistent_module'" in warnings[0].msg
def test_streaming_workunit_callbacks_with_invalid_subsystem(self) -> None: import_str = "pants.option.subsystem_test.DummySubsystem" with self.captured_logging(level=logging.WARNING) as captured: callables_list = Subsystem.get_streaming_workunit_callbacks( [import_str]) warnings = captured.warnings() assert len(warnings) == 1 assert "does not have a method named `handle_workunits` defined" in warnings[ 0] assert len(callables_list) == 0
def test_streaming_workunit_callbacks_with_invalid_subsystem(caplog) -> None: import_str = "pants.option.subsystem_test.DummySubsystem" callables_list = Subsystem.get_streaming_workunit_callbacks([import_str]) assert len(callables_list) == 0 warnings = [ record for record in caplog.records if record.levelname == "WARNING" ] assert len(warnings) == 1 assert "does not have a method named `handle_workunits` defined" in warnings[ 0].msg
def test_streaming_workunit_callbacks_good_module_bad_class(self) -> None: import_str = "pants.option.subsystem_test.ANonexistentClass" with self.captured_logging(level=logging.WARNING) as captured: callables_list = Subsystem.get_streaming_workunit_callbacks( [import_str]) warnings = captured.warnings() assert len(warnings) == 1 assert len(callables_list) == 0 assert ( "module 'pants.option.subsystem_test' has no attribute 'ANonexistentClass'" in warnings[0])
def test_streaming_workunit_callbacks_good_module_bad_class(caplog) -> None: import_str = "pants.option.subsystem_test.ANonexistentClass" callables_list = Subsystem.get_streaming_workunit_callbacks([import_str]) assert len(callables_list) == 0 warnings = [ record for record in caplog.records if record.levelname == "WARNING" ] assert len(warnings) == 1 assert ( "module 'pants.option.subsystem_test' has no attribute 'ANonexistentClass'" in warnings[0].msg)
def create(cls, options_bootstrapper, build_configuration, init_subsystems=True): global_bootstrap_options = options_bootstrapper.get_bootstrap_options( ).for_global_scope() if global_bootstrap_options.pants_version != pants_version(): raise BuildConfigurationError( f"Version mismatch: Requested version was {global_bootstrap_options.pants_version}, " f"our version is {pants_version()}.") # Parse and register options. options = cls._construct_options(options_bootstrapper, build_configuration) GlobalOptions.validate_instance(options.for_global_scope()) if init_subsystems: Subsystem.set_options(options) return options
def select(argv): # Parse positional arguments to the script. args = _create_bootstrap_binary_arg_parser().parse_args(argv[1:]) # Resolve bootstrap options with a fake empty command line. options_bootstrapper = OptionsBootstrapper.create(env=os.environ, args=[argv[0]], allow_pantsrc=True) subsystems = (GlobalOptions, BinaryUtil.Factory) known_scope_infos = reduce(set.union, (ss.known_scope_infos() for ss in subsystems), set()) options = options_bootstrapper.get_full_options(known_scope_infos) # Initialize Subsystems. Subsystem.set_options(options) # If the filename provided ends in a known archive extension (such as ".tar.gz"), then we get the # appropriate Archiver to pass to BinaryUtil. archiver_for_current_binary = None filename = args.filename or args.util_name try: archiver_for_current_binary = archiver_for_path(filename) # BinaryRequest requires the `name` field to be provided without an extension, as it appends the # archiver's extension if one is provided, so we have to remove it here. filename = filename[:-(len(archiver_for_current_binary.extension) + 1)] except ValueError: pass binary_util = BinaryUtil.Factory.create() binary_request = BinaryRequest( supportdir="bin/{}".format(args.util_name), version=args.version, name=filename, platform_dependent=True, external_url_generator=None, archiver=archiver_for_current_binary, ) return binary_util.select(binary_request)
def run(self, start_time: float) -> ExitCode: self._set_start_time(start_time) with maybe_profiled(self.profile_path): global_options = self.options.for_global_scope() streaming_handlers = global_options.streaming_workunits_handlers report_interval = global_options.streaming_workunits_report_interval callbacks = Subsystem.get_streaming_workunit_callbacks( streaming_handlers) streaming_reporter = StreamingWorkunitHandler( self.graph_session.scheduler_session, callbacks=callbacks, report_interval_seconds=report_interval, ) if self.options.help_request: all_help_info = HelpInfoExtracter.get_all_help_info( self.options, self.union_membership, self.graph_session.goal_consumed_subsystem_scopes, ) help_printer = HelpPrinter( bin_name=global_options.pants_bin_name, help_request=self.options.help_request, all_help_info=all_help_info, use_color=global_options.colors, ) return help_printer.print_help() with streaming_reporter.session(): engine_result = PANTS_FAILED_EXIT_CODE try: engine_result = self._run_v2() except Exception as e: ExceptionSink.log_exception(e) run_tracker_result = self._finish_run(engine_result) return self._merge_exit_codes(engine_result, run_tracker_result)
def check_false(s: str) -> None: assert not Subsystem.is_valid_scope_name(s)
def check_true(s: str) -> None: assert Subsystem.is_valid_scope_name(s)
def tearDown(self): """ :API: public """ super().tearDown() Subsystem.reset()
def setUp(self): super().setUp() # Some integration tests rely on clean subsystem state (e.g., to set up a DistributionLocator). Subsystem.reset()
def test_get_streaming_workunit_callbacks(self) -> None: import_str = "pants.option.subsystem_test.WorkunitSubscriptableSubsystem" callables_list = Subsystem.get_streaming_workunit_callbacks( [import_str]) assert len(callables_list) == 1
def test_uninitialized_global(self) -> None: Subsystem.reset() with self.assertRaisesRegex( Subsystem.UninitializedSubsystemError, r"UninitializedSubsystem.*uninitialized-scope"): UninitializedSubsystem.global_instance()