Skip to content

Commit

Permalink
Add logic to identify system
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteOtter committed Oct 24, 2023
1 parent 25440ad commit 37d1500
Showing 3 changed files with 56 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/nester/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
This module provides Nester with various custom exceptions to handle run-time errors.
"""
8 changes: 8 additions & 0 deletions src/nester/exceptions/management_excs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Custom Exceptions for Nester's project management utility."""


class UnknownDistroException(Exception):
"""Exception to handle in the event that a linux distribution cannot be identified or does not have a known package manager."""

def __init__(self, message: str):
super().__init__(message)
48 changes: 45 additions & 3 deletions src/nester/management/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
"""This module provides frequently used code to the wider management module."""

import os
import platform
import subprocess

import nester.exceptions as exceptions

PACKAGE_MANAGERS: list[str] = ["zypper", "apt", "apt-get", "dnf", "packman"]


def check_pip_installed() -> bool:
"""Verify pip is installed on the current system."""
@@ -14,9 +20,44 @@ def check_pip_installed() -> bool:
return True


def get_package_manager() -> str:
"""Check what package manager is installed."""
pass
def get_package_manager() -> str | None:
"""Check what package manager is installed.
Attempts to identify the system Nester is installed on.
If it cannot identify the system it will raise an exception.
:return package_manager: name of the system's package manager
"""
package_manager: str | None = None
system: str = platform.system()

match system:
case "Linux":
package_manager = identify_linux_package_manager()
case "Darwin":
package_manager = "brew"
case "Windows":
package_manager = "winget"

return package_manager


def identify_linux_package_manager() -> str | None:
"""
Will attempt to identify the distribution's package manager by brute forcing all known package manager.
Will raise a UnknownDistroExcpetion if the package manager cannot be identified.
:return pkgmgr: The Package Manager from the list of known package managers
"""
for pkgmgr in PACKAGE_MANAGERS:
try:
subprocess.run(pkgmgr)
except subprocess.CalledProcessError:
raise excpetions.UnknownDistroException(
"Error: Distribution could not be identified."
)
return pkgmgr


def check_for_virtualenv(project_name: str) -> bool:
@@ -41,3 +82,4 @@ def select_dependency_source(language: str) -> None:
:param language: The language the project is written in.
"""
pass

0 comments on commit 37d1500

Please sign in to comment.