diff --git a/embutils/__init__.py b/embutils/__init__.py index d9081da..42fc2a2 100644 --- a/embutils/__init__.py +++ b/embutils/__init__.py @@ -9,4 +9,4 @@ :license: The MIT License (MIT) """ # ------------------------------------- -__version__ = '0.8.5' +__version__ = '0.8.6' diff --git a/embutils/repo/build.py b/embutils/repo/build.py index e4bc096..185a2e6 100644 --- a/embutils/repo/build.py +++ b/embutils/repo/build.py @@ -10,7 +10,6 @@ """ # ------------------------------------- -import shutil as su import subprocess as sp from ..utils.common import TPPath @@ -25,23 +24,6 @@ # -->> API <<-------------------------- -def get_exec(name: str) -> Path: - """ - Get executable path. - - :param str name: Executable name. - - :return: Executable path. - :rtype: Path - - :raises FileNotFoundError: Executable not found in PATH. - """ - find = su.which(name) - if find is None: - raise FileNotFoundError(f"Unable to find {name} executable on PATH!") - return Path(find) - - def build_cubeide( name: str, config: str, project: TPPath, workspace: TPPath, indexer: bool = False, clean: bool = True, log: TPPath = None, pipe: bool = False, cwd: TPPath = None @@ -73,7 +55,7 @@ def build_cubeide( prj = Path.validate_dir(path=project, must_exist=True) log = Path.validate_dir(path=log, none_ok=True) # Prepare - exe = get_exec(name="stm32cubeidec") + exe = Path.which(name="stm32cubeidec") idx = "" if indexer else "-no-indexer" bld = "-cleanBuild" if clean else "-build" # Execute @@ -113,7 +95,7 @@ def build_iar( # Prepare bld = "-build" if clean else "-make" # Execute - exe = get_exec(name="IarBuild") + exe = Path.which(name="IarBuild") cmd = f"{exe} \"{prj}\" {bld} \"{config}\"" res = execute(cmd=cmd, pipe=pipe, log=log, cwd=cwd) if not pipe and res.returncode: @@ -123,7 +105,6 @@ def build_iar( # -->> Export <<----------------------- __all__ = [ - "get_exec", "build_cubeide", "build_iar", ] diff --git a/embutils/utils/path.py b/embutils/utils/path.py index 48ddd62..bf1c39e 100644 --- a/embutils/utils/path.py +++ b/embutils/utils/path.py @@ -11,6 +11,7 @@ # ------------------------------------- import pathlib as pl +import shutil as su import typing as tp from .common import ENCODE, TPAny, TPByte, TPPath @@ -63,6 +64,7 @@ def __new__(cls, *args, **kwargs) -> 'Path': # Generate object and add extra functionalities obj = pl.Path(*tuple(path)) setattr(obj.__class__, Path.reachable.__name__, Path.reachable) + setattr(obj.__class__, Path.which.__name__, staticmethod(Path.which)) setattr(obj.__class__, Path.validate.__name__, staticmethod(Path.validate)) setattr(obj.__class__, Path.validate_dir.__name__, staticmethod(Path.validate_dir)) setattr(obj.__class__, Path.validate_file.__name__, staticmethod(Path.validate_file)) @@ -77,6 +79,23 @@ def reachable(self) -> bool: """ return self.exists() or self.parent.exists() + @staticmethod + def which(name: str) -> "Path": + """ + Get executable path. + + :param str name: Executable name. + + :return: Executable path. + :rtype: Path + + :raises FileNotFoundError: Executable not found in PATH. + """ + find = su.which(name) + if find is None: + raise FileNotFoundError(f"Unable to find {name} executable on PATH!") + return Path(find) + @staticmethod def validate( path: TPAny = None, diff --git a/embutils/utils/subprocess.py b/embutils/utils/subprocess.py index 907e8fb..0370f0f 100644 --- a/embutils/utils/subprocess.py +++ b/embutils/utils/subprocess.py @@ -26,7 +26,7 @@ # -->> API <<-------------------------- -def execute(cmd: str, cwd: TPPath = None, log: TPPath = None, pipe: bool = True) -> sp.CompletedProcess: +def execute(cmd: str, cwd: TPPath = None, log: TPPath = None, pipe: bool = True, **kwargs) -> sp.CompletedProcess: """ Execute the given command as a subprocess. @@ -42,7 +42,7 @@ def execute(cmd: str, cwd: TPPath = None, log: TPPath = None, pipe: bool = True) cwd = Path.validate_dir(path=cwd, none_ok=True) log = Path.validate_dir(path=log, none_ok=True) # Prepare - with sp.Popen(cmd, cwd=cwd, shell=True, close_fds=True, stdout=sp.PIPE, stderr=sp.PIPE) as proc: + with sp.Popen(cmd, cwd=cwd, shell=True, close_fds=True, stdout=sp.PIPE, stderr=sp.PIPE, **kwargs) as proc: # Execute if pipe: # Piping needed... diff --git a/pyproject.toml b/pyproject.toml index a56a23a..6f71a38 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [tool.poetry] -version = "0.8.5" +version = "0.8.6" name = "embutils" license = "MIT" readme = "README.md"