示例#1
0
def test_log_filtering_by_rule() -> None:
    with temporary_dir() as tmpdir:
        ob = create_options_bootstrapper([
            f"--pants-workdir={tmpdir}",
            '--log-levels-by-target={"debug_target": "debug"}'
        ])

        # Do not set up a stdio destination, meaning that all messages will go to the log.
        global_bootstrap_options = ob.bootstrap_options.for_global_scope()
        with initialize_stdio(global_bootstrap_options):
            native_engine.write_log(msg="log msg one",
                                    level=LogLevel.INFO.level,
                                    target="some.target")
            native_engine.write_log(msg="log msg two",
                                    level=LogLevel.DEBUG.level,
                                    target="some.other.target")
            native_engine.write_log(msg="log msg three",
                                    level=LogLevel.DEBUG.level,
                                    target="debug_target")

            loglines = (Path(global_bootstrap_options.pants_workdir,
                             "pants.log").read_text().splitlines())

            assert "[INFO] log msg one" in loglines[0]
            assert "[DEBUG] log msg three" in loglines[1]
            assert len(loglines) == 2
示例#2
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(
    )

    @contextmanager
    def stdin_context():
        if stdin_content is None:
            yield open("/dev/null", "r")
        else:
            with temporary_file(binary_mode=isinstance(stdin_content,
                                                       bytes)) as stdin_file:
                stdin_file.write(stdin_content)
                stdin_file.close()
                yield open(stdin_file.name, "r")

    with initialize_stdio(global_bootstrap_options), stdin_context(
    ) 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=global_bootstrap_options.colors), StdioReader(
            _stdout=Path(stdout.name), _stderr=Path(stderr.name))
示例#3
0
    def run_sync(self):
        """Synchronously run pantsd."""
        os.environ.pop("PYTHONPATH")

        global_bootstrap_options = self._bootstrap_options.for_global_scope()
        # Set the process name in ps output to 'pantsd' vs './pants compile src/etc:: -ldebug'.
        set_process_title(f"pantsd [{self._build_root}]")

        # Switch log output to the daemon's log stream, and empty `env` and `argv` to encourage all
        # further usage of those variables to happen via engine APIs and options.
        self._close_stdio()
        with initialize_stdio(global_bootstrap_options), argv_as(
                tuple()), hermetic_environment_as():
            # Install signal and panic handling.
            ExceptionSink.install(
                log_location=init_workdir(global_bootstrap_options),
                pantsd_instance=True)
            native_engine.maybe_set_panic_handler()

            self._initialize_metadata()

            # Check periodically whether the core is valid, and exit if it is not.
            while self._core.is_valid():
                time.sleep(self.JOIN_TIMEOUT_SECONDS)

            # We're exiting: join the server to avoid interrupting ongoing runs.
            self._logger.info(
                "Waiting for ongoing runs to complete before exiting...")
            native_engine.nailgun_server_await_shutdown(self._server)
            self._logger.info("Exiting pantsd")
示例#4
0
    def run(self, start_time: float) -> ExitCode:
        self.scrub_pythonpath()

        options_bootstrapper = OptionsBootstrapper.create(env=self.env,
                                                          args=self.args,
                                                          allow_pantsrc=True)
        with warnings.catch_warnings(record=True):
            bootstrap_options = options_bootstrapper.bootstrap_options
            global_bootstrap_options = bootstrap_options.for_global_scope()

        # We enable logging here, and everything before it will be routed through regular
        # Python logging.
        stdin_fileno = sys.stdin.fileno()
        stdout_fileno = sys.stdout.fileno()
        stderr_fileno = sys.stderr.fileno()
        with initialize_stdio(global_bootstrap_options), stdio_destination(
                stdin_fileno=stdin_fileno,
                stdout_fileno=stdout_fileno,
                stderr_fileno=stderr_fileno,
        ):
            # N.B. We inline imports to speed up the python thin client run, and avoids importing
            # engine types until after the runner has had a chance to set PANTS_BIN_NAME.

            if self._should_run_with_pantsd(global_bootstrap_options):
                from pants.bin.remote_pants_runner import RemotePantsRunner

                try:
                    remote_runner = RemotePantsRunner(self.args, self.env,
                                                      options_bootstrapper)
                    return remote_runner.run(start_time)
                except RemotePantsRunner.Fallback as e:
                    logger.warning(
                        f"Client exception: {e!r}, falling back to non-daemon mode"
                    )

            from pants.bin.local_pants_runner import LocalPantsRunner

            # We only install signal handling via ExceptionSink if the run will execute in this process.
            ExceptionSink.install(
                log_location=init_workdir(global_bootstrap_options),
                pantsd_instance=False)
            runner = LocalPantsRunner.create(
                env=CompleteEnvironment(self.env),
                options_bootstrapper=options_bootstrapper)
            return runner.run(start_time)
示例#5
0
def mock_console(
    options_bootstrapper: OptionsBootstrapper,
) -> Iterator[Tuple[Console, StdioReader]]:
    global_bootstrap_options = options_bootstrapper.bootstrap_options.for_global_scope(
    )
    with initialize_stdio(global_bootstrap_options), open(
            "/dev/null", "r") 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=global_bootstrap_options.colors), StdioReader(
            _stdout=Path(stdout.name), _stderr=Path(stderr.name))
示例#6
0
def test_file_logging() -> None:
    with temporary_dir() as tmpdir:
        ob = create_options_bootstrapper([f"--pants-workdir={tmpdir}"])

        # Do not set up a stdio destination, meaning that all messages will go to the log.
        global_bootstrap_options = ob.bootstrap_options.for_global_scope()
        with initialize_stdio(global_bootstrap_options):
            logger = logging.getLogger(None)

            cat = "🐈"
            logger.warning("this is a warning")
            logger.info("this is some info")
            logger.debug("this is some debug info")
            logger.info(f"unicode: {cat}")

            loglines = (Path(global_bootstrap_options.pants_workdir,
                             "pants.log").read_text().splitlines())
            print(loglines)
            assert len(loglines) == 3
            assert "[WARN] this is a warning" in loglines[0]
            assert "[INFO] this is some info" in loglines[1]
            assert f"[INFO] unicode: {cat}" in loglines[2]
示例#7
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))