Exemplo n.º 1
0
    def test_darwin_detection(self, _, __, ___, ____):
        """Test OS detection for Darwin"""
        # Detection based on `distro`.
        self.assertEqual(Distributions.get_local(), Distributions.DARWIN)

        # Detection based on `platform`.
        self.assertEqual(Distributions.get_local(), Distributions.DARWIN)
Exemplo n.º 2
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        for packages_tool in PACKAGES_TOOLS:
            if 'only_on' in packages_tool \
                and Distributions.get_local() not in packages_tool['only_on']:
                continue

            try:
                results = check_output(
                    packages_tool['cmd'],
                    stderr=DEVNULL,
                    env={
                        'LANG': 'C',
                        # Alpine Linux: We have to manually propagate `PATH`.
                        #               `apk` wouldn't be found otherwise.
                        'PATH': os.getenv('PATH')
                    },
                    universal_newlines=True)
            except (OSError, CalledProcessError):
                continue

            # Here we *may* use `\n` as `universal_newlines` has been set.
            if self.value:
                self.value += results.count('\n')
            else:
                self.value = results.count('\n')

            # If any, deduct output skew present due to the packages tool itself.
            if 'skew' in packages_tool:
                self.value -= packages_tool['skew']

            # For DPKG only, remove any not purged package.
            if packages_tool['cmd'][0] == 'dpkg':
                self.value -= results.count('deinstall')
Exemplo n.º 3
0
    def json_serialization(self, indent: int = 0) -> str:
        """
        JSON serialization of entries.
        Set `indent` to the number of wanted output indentation tabs (2-space long).
        """
        document = {
            'data': {entry.name: entry.value
                     for entry in self.entries},
            'meta': {
                'version': Utility.version_to_semver_segments(__version__),
                'date': datetime.now().isoformat(),
                'count': len(self.entries),
                'distro': Distributions.get_local().value,
            }
        }

        return json.dumps(document, indent=((indent * 2) or None))
Exemplo n.º 4
0
    def __init__(self, **kwargs):
        # Fetches passed arguments.
        self._format_to_json = kwargs.get('format_to_json')
        preferred_logo_style = (kwargs.get('preferred_logo_style')
                                or '').upper()

        try:
            # If set, force the distribution to `preferred_distribution` argument.
            self._distribution = Distributions(
                kwargs.get('preferred_distribution'))
        except ValueError:
            # If not (or unknown), run distribution detection.
            self._distribution = Distributions.get_local()

        # Retrieve distribution's logo module before copying and DRY-ing its attributes.
        logo_module = lazy_load_logo_module(self._distribution.value)

        # If set and available, fetch an alternative logo style from module.
        if preferred_logo_style and hasattr(logo_module,
                                            f"LOGO_{preferred_logo_style}"):
            self._logo = getattr(logo_module,
                                 f"LOGO_{preferred_logo_style}").copy()
            self._colors = getattr(logo_module,
                                   f"COLORS_{preferred_logo_style}").copy()
        else:
            self._logo, self._colors = logo_module.LOGO.copy(
            ), logo_module.COLORS.copy()

        configuration = Configuration()

        # If `os-release`'s `ANSI_COLOR` option is set, honor it.
        ansi_color = Distributions.get_ansi_color()
        if ansi_color and configuration.get("honor_ansi_color"):
            # Replace each Archey integrated colors by `ANSI_COLOR`.
            self._colors = len(
                self._colors) * [Colors.escape_code_from_attrs(ansi_color)]

        entries_color = configuration.get("entries_color")
        self._entries_color = (Colors.escape_code_from_attrs(entries_color)
                               if entries_color else self._colors[0])

        # Each entry will be added to this list
        self._entries = []
        # Each class output will be added in the list below afterwards
        self._results = []
Exemplo n.º 5
0
 def test_get_local_distro_like_second(self, _, __, ___, ____):
     """Test distribution matching from the `os-release`'s `ID_LIKE` option (second candidate)"""
     self.assertEqual(Distributions.get_local(), Distributions.ARCH)
Exemplo n.º 6
0
 def test_get_local_known_distro_like(self, _, __, ___, ____):
     """Test distribution matching from the `os-release`'s `ID_LIKE` option"""
     self.assertEqual(Distributions.get_local(), Distributions.UBUNTU)
Exemplo n.º 7
0
 def test_get_local_unknown_distro_id(self, _, __, ___, ____, _____):
     """Test unknown distribution output"""
     self.assertEqual(Distributions.get_local(), Distributions.LINUX)
Exemplo n.º 8
0
 def test_get_local_windows_subsystem(self, _, __):
     """Test output for Windows Subsystem Linux"""
     self.assertEqual(Distributions.get_local(), Distributions.WINDOWS)
Exemplo n.º 9
0
 def test_get_local_windows(self, _):
     """Test output for Windows"""
     self.assertEqual(Distributions.get_local(), Distributions.WINDOWS)