Skip to content

Commit

Permalink
util.py refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Obijuan committed Mar 15, 2024
1 parent 805f188 commit a99372d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"type": "debugpy",
"request": "launch",
"program": "${file}",
"args": ["examples", "--files", "leds", "--project-dir=tmp"],
"args": ["examples"],
"console": "internalConsole",
"justMyCode": true,
//-- Change to the folder with the example to test
Expand Down
2 changes: 1 addition & 1 deletion apio/managers/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self):
self.name = "examples"

# -- Folder where the example packages was installed
self.examples_dir = Path(util.get_package_dir(self.name))
self.examples_dir = util.get_package_dir(self.name)

# -- Get the example package version
self.version = util.get_package_version(self.name, profile)
Expand Down
13 changes: 10 additions & 3 deletions apio/managers/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import re
import platform
from pathlib import Path
import click

from apio import util
Expand Down Expand Up @@ -172,12 +171,20 @@ def _run_command(self, command: str, silent=False) -> dict:
# The system tools are locate in the
# oss-cad-suite package

# -- Get the package base dir (as a string)
# -- Get the package base dir
# -- Ex. "/home/obijuan/.apio/packages/tools-oss-cad-suite"
system_base_dir = util.get_package_dir("tools-oss-cad-suite")

# -- Package not found
if not system_base_dir:
# -- Show the error message and a hint
# -- on how to install the package
util.show_package_path_error(self.package_name)
util.show_package_install_instructions(self.package_name)
raise util.ApioException()

# -- Get the folder were the binary file is located (PosixPath)
system_bin_dir = Path(system_base_dir) / "bin"
system_bin_dir = system_base_dir / "bin"

# -- Get the executable filename
# -- Ex. Posix('/home/obijuan/.apio/packages/tools-oss-cad-suite/
Expand Down
57 changes: 31 additions & 26 deletions apio/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,16 @@ def get_home_dir() -> Path:
return home_dir


def get_package_dir(pkg_name: str) -> str:
def get_package_dir(pkg_name: str) -> Path:
"""Return the APIO package dir of a given package
Packages are installed in the following folder:
* Default: $APIO_HOME_DIR/packages
* $APIO_PKG_DIR/packages: if the APIO_PKG_DIR env variable is set
* Return a String
* Default: $APIO_HOME_DIR/packages
* $APIO_PKG_DIR/packages: if the APIO_PKG_DIR env variable is set
* Return a String
"""

# -- Get the apio home dir:
# -- Ex. '/home/obijuan/.apio'
apio_home_dir = get_home_dir()

# -- Get the APIO_PKG_DIR env variable
Expand All @@ -243,21 +244,18 @@ def get_package_dir(pkg_name: str) -> str:

# -- Default value
else:
pkg_home_dir = Path(apio_home_dir)
pkg_home_dir = apio_home_dir

# -- Create the package folder
# -- Ex '/home/obijuan/.apio/packages/tools-oss-cad-suite'
package_dir = pkg_home_dir / "packages" / pkg_name

# -- Return the folder if it exists
if package_dir.exists():
return str(package_dir)
return package_dir

# -- Show an error message (for debugging)
# click.secho(f"Folder does not exists: {package_dir}", fg="red")
# sys.exit(1)

# -- Return a null string if the folder does not exist
return ""
# -- No path...
return None


def call(cmd):
Expand Down Expand Up @@ -294,7 +292,7 @@ def setup_environment():
return bin_dir


def set_env_variables(base_dir, bin_dir):
def set_env_variables(base_dir: dict, bin_dir: dict):
"""Set the environment variables"""

# -- Get the current system PATH
Expand All @@ -307,24 +305,24 @@ def set_env_variables(base_dir, bin_dir):
# -- but only for windows platforms
if platform.system() == "Windows":
# -- Gtkwave package is installed
if bin_dir[GTKWAVE] != "":
path = os.pathsep.join([bin_dir.get(GTKWAVE), path])
if bin_dir[GTKWAVE]:
path = os.pathsep.join([str(bin_dir[GTKWAVE]), path])

# -- Add the binary folders of the installed packages
# -- to the path, except for the OSS_CAD_SUITE package
for pack in base_dir:
if base_dir[pack] != "" and pack != OSS_CAD_SUITE:
path = os.pathsep.join([bin_dir[pack], path])
if base_dir[pack] and pack != OSS_CAD_SUITE:
path = os.pathsep.join([str(bin_dir[pack]), path])

# -- Add the OSS_CAD_SUITE package to the path
# -- if installed (Maximum priority)
if base_dir[OSS_CAD_SUITE] != "":
if base_dir[OSS_CAD_SUITE]:
# -- Get the lib folder (where the shared libraries are located)
oss_cad_suite_lib = str(Path(base_dir[OSS_CAD_SUITE]) / "lib")

# -- Add the lib folder
path = os.pathsep.join([oss_cad_suite_lib, path])
path = os.pathsep.join([bin_dir[OSS_CAD_SUITE], path])
path = os.pathsep.join([str(bin_dir[OSS_CAD_SUITE]), path])

# Add the virtual python environment to the path
os.environ["PATH"] = path
Expand Down Expand Up @@ -378,7 +376,7 @@ def resolve_packages(
spec_version = spec_packages.get(package, "")

# -- Get the package binary dir as a PosixPath object
_bin = Path(bin_dir[package])
_bin = bin_dir[package]

# -- Check this package
check &= check_package(package, version, spec_version, _bin)
Expand Down Expand Up @@ -414,10 +412,17 @@ def get_bin_dir_table(base_dir: dict):
-base_dir: Table with the package base_dir
"""

bin_dir = {
OSS_CAD_SUITE: str(Path(base_dir[OSS_CAD_SUITE]) / BIN),
GTKWAVE: str(Path(base_dir[GTKWAVE]) / BIN),
}
if base_dir[GTKWAVE]:
gtkwave_path = base_dir[GTKWAVE] / BIN
else:
gtkwave_path = None

if base_dir[OSS_CAD_SUITE]:
oss_cad_suite_path = base_dir[OSS_CAD_SUITE] / BIN
else:
oss_cad_suite_path = None

bin_dir = {OSS_CAD_SUITE: oss_cad_suite_path, GTKWAVE: gtkwave_path}

return bin_dir

Expand All @@ -426,7 +431,7 @@ def check_package(
name: str, version: str, spec_version: str, path: Path
) -> bool:
"""Check if the given package is ok
(and can be installed without problemas)
(and can be installed without problems)
* INPUTS:
- name: Package name
- version: Package version
Expand All @@ -443,7 +448,7 @@ def check_package(
return True

# Check package path
if not path.is_dir():
if path and not path.is_dir():
show_package_path_error(name)
show_package_install_instructions(name)
return False
Expand Down

0 comments on commit a99372d

Please sign in to comment.