From 846007ff781b0ff66956b1d7660949ef5bc2c40c Mon Sep 17 00:00:00 2001 From: ByteOtter Date: Mon, 9 Oct 2023 15:14:18 +0200 Subject: [PATCH] Add logic to identify system pm --- src/nester/core/opinionated/exceptions.py | 5 ++++ .../core/opinionated/opinionated_creation.py | 7 +++++- .../core/opinionated/supported_tools.py | 2 ++ src/nester/core/opinionated/utils.py | 23 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/nester/core/opinionated/utils.py diff --git a/src/nester/core/opinionated/exceptions.py b/src/nester/core/opinionated/exceptions.py index 05cf072..cabc82d 100644 --- a/src/nester/core/opinionated/exceptions.py +++ b/src/nester/core/opinionated/exceptions.py @@ -2,5 +2,10 @@ class UnsupportedLanguageException(Exception): + """ + This exception is raised, whenever the user tries to use opinionated creation for a language that does not + support this feature. + """ + def __str__(self) -> str: return "\033[91mError: The chosen language is not supported for opinionated creation!\033[0m" diff --git a/src/nester/core/opinionated/opinionated_creation.py b/src/nester/core/opinionated/opinionated_creation.py index 94b3b93..d2541e0 100644 --- a/src/nester/core/opinionated/opinionated_creation.py +++ b/src/nester/core/opinionated/opinionated_creation.py @@ -94,7 +94,12 @@ def install_build_system(language: str) -> None: match language: case "py": for tool in supported_tools.py_build: - subprocess.run(["pip", "install", tool]) + try: + subprocess.run(["pip", "install", tool]) + except subprocess.CalledProcessError: + print( + "\033[91mError: Pip does not seem to be installed on your system. Install it and try again.\033[0m" + ) case _: raise exceptions.UnsupportedLanguageException diff --git a/src/nester/core/opinionated/supported_tools.py b/src/nester/core/opinionated/supported_tools.py index 41d6b8f..f55b3ac 100644 --- a/src/nester/core/opinionated/supported_tools.py +++ b/src/nester/core/opinionated/supported_tools.py @@ -2,3 +2,5 @@ py_build = ["setuptools", "setuptools-scm", "wheel"] py_linters = ["black", "isort", "pylint"] + +package_managers = ["zypper", "apt", "dnf"] diff --git a/src/nester/core/opinionated/utils.py b/src/nester/core/opinionated/utils.py new file mode 100644 index 0000000..57f7b66 --- /dev/null +++ b/src/nester/core/opinionated/utils.py @@ -0,0 +1,23 @@ +""" +This module provides support functionality for the opinionated creation module. +""" + +import subprocess + +from . import supported_tools + + +def identify_package_manager() -> str: + """ + Identify which distribution's package manager is installed via trial and error. + + :return: The name of the package manager to be used to install dependencies. + :rtype: str + """ + for pm in supported_tools.package_managers: + try: + subprocess.run(pm) + except subprocess.CalledProcessError: + continue + return pm + return ""