Skip to content
Closed
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
45 changes: 24 additions & 21 deletions dexi/commands/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import random
import shutil
import zipfile
from pathlib import Path
from typing import cast

import requests
Expand Down Expand Up @@ -50,9 +51,9 @@ def uninstall_package(package: str):
if data.app is not None and not app_operations_supported():
return

desination = f"{os.getcwd()}/ballsdex/packages/{data.package.target}"
destination = Path.cwd() / "ballsdex" / "packages" / data.package.target

if not os.path.isdir(desination):
if not destination.is_dir():
return

if data.app is not None:
Expand All @@ -65,7 +66,7 @@ def uninstall_package(package: str):

remove_list_entry("packages", f"ballsdex.packages.{data.package.target}")

shutil.rmtree(desination)
shutil.rmtree(destination)


def install_package(
Expand Down Expand Up @@ -93,16 +94,16 @@ def install_package(
author, repository = repository.split("/")

zip_url = f"https://github.com/{author}/{repository}/archive/refs/heads/{branch}.zip"
desination = f"{os.getcwd()}/ballsdex/packages/{data.package.target}"
destination = Path.cwd() / "ballsdex" / "packages" / data.package.target

name = package_name(repository, branch)

if os.path.isdir(desination):
if destination.is_dir():
if cancel_if_exists:
return False

replaced = True
shutil.rmtree(desination)
shutil.rmtree(destination)

if data.app is not None:
if not app_operations_supported():
Expand All @@ -111,15 +112,15 @@ def install_package(
f"on Ballsdex v$BD_V, please update to v{SUPPORTED_APP_VERSION}+"
)

app_desination = f"{os.getcwd()}/admin_panel/{data.app.target}"
app_destination = Path.cwd() / "admin_panel" / data.app.target

if os.path.isdir(app_desination):
if app_destination.is_dir():
replaced = True
shutil.rmtree(app_desination)
shutil.rmtree(app_destination)

os.makedirs(app_desination, exist_ok=True)
app_destination.mkdir(parents=True, exist_ok=True)

os.makedirs(desination, exist_ok=True)
destination.mkdir(parents=True, exist_ok=True)

response = requests.get(zip_url)

Expand All @@ -131,10 +132,7 @@ def install_package(

for member in z.namelist():
if member[-7:] in ["LICENSE", "LICENCE"]:
with (
z.open(member) as src,
open(f"{desination}/{member[-7:]}", "wb") as dst,
):
with z.open(member) as src, (destination / member[-7:]).open("wb") as dst:
shutil.copyfileobj(src, dst)

continue
Expand All @@ -147,7 +145,7 @@ def install_package(
if not relative_path or relative_path in data.package.exclude:
continue

target_path = os.path.join(desination, relative_path)
target_path = destination / relative_path

if member.endswith("/"):
os.makedirs(target_path, exist_ok=True)
Expand All @@ -159,7 +157,12 @@ def install_package(
shutil.copyfileobj(src, dst)

if data.app is not None: # I'll refactor this later
app_desination = cast(str, app_desination) # type: ignore
if not app_destination:
# something has gone remarkably wrong
# (shouldnt be possible)
raise Exception(
"Somehow app_destination has gone missing while copying files"
)

for member in z.namelist():
if not member.startswith(f"{base_folder}{data.app.source}/"):
Expand All @@ -170,15 +173,15 @@ def install_package(
if not relative_path:
continue

target_path = os.path.join(app_desination, relative_path)
target_path = app_destination / relative_path

if member.endswith("/"):
os.makedirs(target_path, exist_ok=True)
target_path.mkdir(parents=True, exist_ok=True)
continue

os.makedirs(os.path.dirname(target_path), exist_ok=True)
target_path.parent.mkdir(parents=True, exist_ok=True)

with z.open(member) as src, open(target_path, "wb") as dst:
with z.open(member) as src, target_path.open("wb") as dst:
shutil.copyfileobj(src, dst)

add_list_entry(
Expand Down
6 changes: 3 additions & 3 deletions dexi/core/errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from dataclasses import dataclass, field
from pathlib import Path

from packaging.version import parse as parse_version

Expand All @@ -18,14 +18,14 @@ class Errors:

@staticmethod
def invalid_project() -> None:
if os.path.isdir("ballsdex") and os.path.isfile("pyproject.toml"):
if Path("ballsdex").is_dir() and Path("pyproject.toml").is_file():
return

error("Attempted to use DexI command on an invalid project")

@staticmethod
def no_config_found() -> None:
if os.path.isfile("config.yml"):
if Path("config.yml").is_file():
return

error("No 'config.yml' file detected")
Expand Down
42 changes: 23 additions & 19 deletions dexi/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import re
import sys
from pathlib import Path
from typing import cast

import requests
Expand Down Expand Up @@ -50,7 +50,7 @@ def fetch_pyproject(package: str, branch: str) -> dict:
return data


def parse_pyproject(path: str | None = None) -> TOMLDocument:
def parse_pyproject(path: Path | None = None) -> TOMLDocument:
"""
Parses a pyproject file and returns it.

Expand All @@ -60,14 +60,14 @@ def parse_pyproject(path: str | None = None) -> TOMLDocument:
The path that holds the pyproject file.
"""
if path is None:
path = os.getcwd()
path = Path.cwd()

path = f"{path}/pyproject.toml"
path = path / "pyproject.toml"

if not os.path.isfile(path):
if not path.is_file():
error("Failed to find [red]pyproject.toml[/red] in the current directory")

with open(path) as file:
with path.open() as file:
return parse(file.read())


Expand All @@ -78,7 +78,7 @@ def app_operations_supported() -> bool:
return parse_version(fetch_ballsdex_version()) >= parse_version(SUPPORTED_APP_VERSION)


def fetch_ballsdex_version(path: str | None = None) -> str:
def fetch_ballsdex_version(path: Path | None = None) -> str:
"""
Returns the Ballsdex version.

Expand All @@ -88,14 +88,14 @@ def fetch_ballsdex_version(path: str | None = None) -> str:
The path that will be checked.
"""
if path is None:
path = os.getcwd()
path = Path.cwd()

path = f"{path}/ballsdex/__init__.py"
path = path / "ballsdex/__init__.py"

if not os.path.isfile(path):
if not path.is_file():
error("Failed to find [red]ballsdex/__init__.py[/red] in the current directory")

with open(path) as file:
with path.open() as file:
return file.read().replace('__version__ = "', "").rstrip()[:-1]


Expand Down Expand Up @@ -150,7 +150,7 @@ def fetch_all_packages() -> list[PackageEntry]:
return cast(list[PackageEntry], packages)


def add_list_entry(section: str, entry: str, path: str | None = None):
def add_list_entry(section: str, entry: str, path: Path | None = None):
"""
Adds an item to a list in the config file.

Expand All @@ -164,9 +164,11 @@ def add_list_entry(section: str, entry: str, path: str | None = None):
The config file path.
"""
if path is None:
path = os.getcwd()
path = Path.cwd()

with open(f"{path}/config.yml") as file:
path = path / "config.yml"

with path.open() as file:
lines = file.readlines()

item = f" - {entry}\n"
Expand All @@ -179,11 +181,11 @@ def add_list_entry(section: str, entry: str, path: str | None = None):
lines.insert(i + 1, item)
break

with open(f"{path}/config.yml", "w") as file:
with path.open("w") as file:
file.writelines(lines)


def remove_list_entry(section: str, entry: str, path: str | None = None):
def remove_list_entry(section: str, entry: str, path: Path | None = None):
"""
Removes an item from a list in the config file.

Expand All @@ -197,9 +199,11 @@ def remove_list_entry(section: str, entry: str, path: str | None = None):
The config file path.
"""
if path is None:
path = os.getcwd()
path = Path.cwd()

path = path / "config.yml"

with open(f"{path}/config.yml") as file:
with path.open() as file:
lines = file.readlines()

item = f" - {entry}\n"
Expand All @@ -209,7 +213,7 @@ def remove_list_entry(section: str, entry: str, path: str | None = None):

lines.remove(item)

with open(f"{path}/config.yml", "w") as file:
with path.open("w") as file:
file.writelines(lines)


Expand Down
Loading