def _setUp(self): # This type of monkey patching is borrowed from the distro test # suite. os_release = os.path.join(FIXTURE_DIR, self.distro_name, 'etc', 'os-release') mydistro = distro.LinuxDistribution(False, os_release, 'non') self.useFixture(fixtures.MonkeyPatch('distro._distro', mydistro))
def installPythonPackages(packages:typing.List[str]) -> bool: """ Attempts to install the packages listed in ``packages`` and add them to the global scope. :param packages: A list of missing Python dependencies :return: a truthy value indicating success. """ logging.info("Attempting install of %s", ','.join(packages)) # Ensure `pip` is installed try: import pip except ImportError as e: logging.info("`pip` package not installed. Attempting install with `ensurepip` module") logging.debug("%s", e, exc_info=True, stack_info=True) import ensurepip try: ensurepip.bootstrap(altinstall=True) except (EnvironmentError, PermissionError) as err: logging.info("Permission Denied, attempting user-level install") logging.debug("%s", err, exc_info=True, stack_info=True) ensurepip.bootstrap(altinstall=True, user=True) import pip # Get main pip function try: pipmain = pip.main except AttributeError as e: # This happens when using a version of pip >= 10.0 from pip._internal import main as pipmain # Attempt package install ret = pipmain(["install"] + packages) if ret == 1: logging.info("Possible 'Permission Denied' error, attempting user-level install") ret = pipmain(["install", "--user"] + packages) logging.debug("Pip return code was %d", ret) if not ret: import importlib for package in packages: try: globals()[package] = importlib.import_module(package) except ImportError: logging.error("Failed to import %s", package) logging.warning("Install appeared succesful - subsequent run may succeed") logging.debug("%s", e, exc_info=True, stack_info=True) return False urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) globals()["DISTRO"] = distro.LinuxDistribution().id() return not ret
def get_distro_info(root_dir): # We point _UNIXCONFDIR to root_dir old_value = distro._UNIXCONFDIR distro._UNIXCONFDIR = os.path.join(root_dir, 'etc') obj = distro.LinuxDistribution(include_lsb=False, include_uname=False) # NOTE: The parsing of LinuxDistribution distro information is done in a lazy way. # This will force the parsing to happen before we restore the old value of _UNIXCONFDIR. _ = obj.info() distro._UNIXCONFDIR = old_value return obj
import argparse import enum import logging import os import platform import typing import distro import requests #: A format specifier for logging output. Propagates to all imported modules. LOG_FORMAT = "%(levelname)s: %(asctime)s line %(lineno)d in %(module)s.%(funcName)s: %(message)s" #: contains identifying information about the host system's Linux distribution DISTRO = distro.LinuxDistribution().id() class LogLevels(enum.IntEnum): """ Enumerated Log levels """ ALL = logging.NOTSET #: Outputs all logging information TRACE = logging.NOTSET #: Synonym for :attr:`ALL` DEBUG = logging.DEBUG #: Outputs debugging information as well as all higher log levels INFO = logging.INFO #: Outputs informational messages as well as all higher log levels WARN = logging.WARNING #: Outputs warnings, errors and fatal messages ERROR = logging.ERROR #: Errors - but not more verbose warnings - will be output FATAL = logging.CRITICAL #: Outputs only reasons why the script exited prematurely CRITICAL = logging.CRITICAL #: Synonym for :attr:`FATAL` NONE = logging.CRITICAL + 1 #: Silent mode - no output at all
#!/usr/bin/python import distro # Not the full API, but enough to flex the package. print distro.linux_distribution() print distro.linux_distribution(full_distribution_name=False) print distro.name() print distro.name(pretty=True) # Use lsb_release for enhanced information. assert len(distro.LinuxDistribution().lsb_release_info()) > 0, ( "Why isn't lsb_release(1) available?")