Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

[FEATURE:] Add nester management #52

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
1 change: 1 addition & 0 deletions src/nester/management/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""This module implements all aspects of Nester's project management."""
35 changes: 35 additions & 0 deletions src/nester/management/manage_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""This module provides logic to allow Nester to manage dependencies."""

import subprocess

from . import utils


def add_dependency(language: str, dependency: str) -> None:
"""
Add dependency to current project.

Adds a dependency to a project and adds it to the list of dependencies for the project's language.\n
E.g the pyproject.toml etc.

:param language: The language of the project.
:param dependency: The name of the dependency to add.
"""
match language:
case "py":
if utils.check_pip_installed():
subprocess.run("pip").args("install").args(dependency)
# TODO Add dependency logging!
else:
print("Error: Dependency cannot be added. Pip is not installed!")


def remove_dependency(dependency: str) -> None:
"""
Remove a given dependency.

Remove a given dependency from a project.

:param dependency: The dependency to remove.
"""
pass
94 changes: 94 additions & 0 deletions src/nester/management/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""This module provides frequently used code to the wider management module."""

import os
import platform
import subprocess

from ..exceptions import management_excs

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


def check_pip_installed() -> bool:
"""Verify pip is installed on the current system."""
try:
subprocess.run("pip", check=True)
except subprocess.CalledProcessError:
return False

return True


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 management_excs.UnknownDistroException(
"Error: Distribution could not be identified."
)
return pkgmgr

return None


def check_for_virtualenv(project_name: str) -> bool:
"""
Check a project for a virtual environment.

This function checks whether a given project has a virtualenv directory.

:param project_name: The name of the project to check.
:return: Whether the given project has a virtualenv.
"""

return False


def select_dependency_source(language: str) -> str:
"""
Select the source of the project's dependencies according to its language.

Different programming languages have different ways of installing dependencies.\n
Python uses ```pip```, Ruby uses ```gem``` etc. This function selects the appropriate way of installing dependencies
using a given programming language.

:param language: The language the project is written in.
"""
match language:
case "py":
return "pip"
case "rb":
return "gem"
# TODO
pass
2 changes: 1 addition & 1 deletion src/nester/tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def interactive_mode() -> None:
choices: List[str] = ["---Abort---"]
projects = nester_log.find_all_projects()

if projects is not None:
if projects != []:
for project in projects:
choices.append(project)
else:
Expand Down