Beispiel #1
0
def configure(logger_name=None, log_level=None):
    if not log_level:
        log_level = logging.INFO

    stdout_handler = logging.StreamHandler(stream=sys.stdout)
    stdout_handler.addFilter(_StdoutFilter())
    stderr_handler = logging.StreamHandler(stream=sys.stderr)
    stderr_handler.addFilter(_StderrFilter())
    handlers = [stdout_handler, stderr_handler]

    if is_dumb_terminal():
        formatter = logging.Formatter(style="{")
    else:
        formatter = _ColoredFormatter(style="{")
    logger = logging.getLogger(logger_name)
    for handler in handlers:
        handler.setFormatter(formatter)
        logger.addHandler(handler)

    logger.setLevel(log_level)

    # INFO by default for the requests lib as it is too noisy
    if log_level == logging.DEBUG:
        logging.getLogger("requests").setLevel(log_level)
    else:
        logging.getLogger("requests").setLevel(logging.WARNING)
        logging.getLogger("gnupg").setLevel(logging.WARNING)
Beispiel #2
0
def _run_pack(snap_command: List[Union[str, pathlib.Path]]) -> str:
    ret = None
    stdout = ""
    stderr = ""
    with subprocess.Popen(
        snap_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE
    ) as proc:
        if indicators.is_dumb_terminal():
            echo.info("Snapping...")
            ret = proc.wait()
        else:
            message = "\033[0;32mSnapping \033[0m"
            progress_indicator = progressbar.ProgressBar(
                widgets=[message, progressbar.AnimatedMarker()],
                # From progressbar.ProgressBar.update(...).
                maxval=progressbar.UnknownLength,
            )
            progress_indicator.start()
            for counter in itertools.count():
                progress_indicator.update(counter)
                time.sleep(0.2)
                ret = proc.poll()
                if ret is not None:
                    break
            progress_indicator.finish()

        if proc.stdout is not None:
            stdout = proc.stdout.read().decode()
        if proc.stderr is not None:
            stderr = proc.stderr.read().decode()
        logger.debug(f"stdout: {stdout} | stderr: {stderr}")

    if ret != 0:
        raise RuntimeError(
            f"Failed to create snap, snap command failed:\nstdout:\n{stdout}\nstderr:\n{stderr}"
        )

    try:
        snap_filename = stdout.split(":")[1].strip()
    except IndexError:
        logger.debug("Failed to parse snap pack outpout: {stdout}")
        snap_filename = stdout

    return snap_filename
Beispiel #3
0
    def _configure_apt(self):
        # Do not install recommends.
        apt.apt_pkg.config.set("Apt::Install-Recommends", "False")

        # Ensure repos are provided by trusted third-parties.
        apt.apt_pkg.config.set("Acquire::AllowInsecureRepositories", "False")

        # Methods and solvers dir for when in the SNAP.
        snap_dir = os.getenv("SNAP")
        if common.is_snap() and snap_dir and os.path.exists(snap_dir):
            apt_dir = os.path.join(snap_dir, "usr", "lib", "apt")
            apt.apt_pkg.config.set("Dir", apt_dir)
            # yes apt is broken like that we need to append os.path.sep
            methods_dir = os.path.join(apt_dir, "methods")
            apt.apt_pkg.config.set("Dir::Bin::methods",
                                   methods_dir + os.path.sep)
            solvers_dir = os.path.join(apt_dir, "solvers")
            apt.apt_pkg.config.set("Dir::Bin::solvers::",
                                   solvers_dir + os.path.sep)
            apt_key_path = os.path.join(snap_dir, "usr", "bin", "apt-key")
            apt.apt_pkg.config.set("Dir::Bin::apt-key", apt_key_path)
            gpgv_path = os.path.join(snap_dir, "usr", "bin", "gpgv")
            apt.apt_pkg.config.set("Apt::Key::gpgvcommand", gpgv_path)

        apt.apt_pkg.config.set("Dir::Etc::Trusted", "/etc/apt/trusted.gpg")
        apt.apt_pkg.config.set("Dir::Etc::TrustedParts",
                               "/etc/apt/trusted.gpg.d/")
        apt.apt_pkg.config.set("Dir::State", "/var/lib/apt")

        # Clear up apt's Post-Invoke-Success as we are not running
        # on the system.
        apt.apt_pkg.config.clear("APT::Update::Post-Invoke-Success")

        self.progress = apt.progress.text.AcquireProgress()
        if is_dumb_terminal():
            # Make output more suitable for logging.
            self.progress.pulse = lambda owner: True
            self.progress._width = 0
Beispiel #4
0
    def _install_packages(cls, package_names: List[str]) -> None:
        logger.info("Installing build dependencies: %s", " ".join(package_names))
        env = os.environ.copy()
        env.update(
            {
                "DEBIAN_FRONTEND": "noninteractive",
                "DEBCONF_NONINTERACTIVE_SEEN": "true",
                "DEBIAN_PRIORITY": "critical",
            }
        )

        apt_command = [
            "sudo",
            "--preserve-env",
            "apt-get",
            "--no-install-recommends",
            "-y",
            "--allow-downgrades",
        ]
        if not is_dumb_terminal():
            apt_command.extend(["-o", "Dpkg::Progress-Fancy=1"])
        apt_command.append("install")

        try:
            subprocess.check_call(apt_command + package_names, env=env)
        except subprocess.CalledProcessError:
            raise errors.BuildPackagesNotInstalledError(packages=package_names)

        versionless_names = [get_pkg_name_parts(p)[0] for p in package_names]
        try:
            subprocess.check_call(
                ["sudo", "apt-mark", "auto"] + versionless_names, env=env
            )
        except subprocess.CalledProcessError as e:
            logger.warning(
                "Impossible to mark packages as auto-installed: {}".format(e)
            )
Beispiel #5
0
 def test_vt100_terminal_environmment(self):
     self.useFixture(fixtures.EnvironmentVariable("TERM", "vt100"))
     self.assertFalse(indicators.is_dumb_terminal())
Beispiel #6
0
 def test_dumb_terminal_environment(self):
     self.useFixture(fixtures.EnvironmentVariable("TERM", "dumb"))
     self.assertTrue(indicators.is_dumb_terminal())
Beispiel #7
0
 def test_not_a_tty_terminal(self):
     self.mock_os_isatty.return_value = False
     self.assertFalse(indicators.is_dumb_terminal())
Beispiel #8
0
 def test_tty_terminal(self):
     self.assertTrue(indicators.is_dumb_terminal())