Skip to content

Commit

Permalink
add new quiet arg when getting versions
Browse files Browse the repository at this point in the history
  • Loading branch information
pirate committed Oct 4, 2024
1 parent 19fbbd6 commit 5bb4205
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions pydantic_pkgr/binprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ class BinProvider(BaseModel):
PATH: PATHStr = Field(default=str(Path(sys.executable).parent)) # e.g. '/opt/homebrew/bin:/opt/archivebox/bin'
INSTALLER_BIN: BinName = 'env'

abspath_handler: ProviderLookupDict = Field(default={'*': 'self.on_get_abspath'}, exclude=True)
version_handler: ProviderLookupDict = Field(default={'*': 'self.on_get_version'}, exclude=True)
abspath_handler: ProviderLookupDict = Field(default={'*': 'self.on_get_abspath'}, exclude=True)
packages_handler: ProviderLookupDict = Field(default={'*': 'self.on_get_packages'}, exclude=True)
install_handler: ProviderLookupDict = Field(default={'*': 'self.on_install'}, exclude=True)

Expand Down Expand Up @@ -348,8 +348,8 @@ def on_get_abspath(self, bin_name: BinName | HostBinPath, **context) -> HostBinP
return None

return bin_abspath(bin_name, PATH=self.PATH)

def on_get_version(self, bin_name: BinName, abspath: Optional[HostBinPath]=None, **context) -> SemVer | None:

abspath = abspath or self._abspath_cache.get(bin_name) or self.get_abspath(bin_name, quiet=True)
if not abspath: return None

Expand All @@ -358,7 +358,7 @@ def on_get_version(self, bin_name: BinName, abspath: Optional[HostBinPath]=None,
validation_err = None

# Attempt 1: $ <bin_name> --version
dash_dash_version_result = self.exec(bin_name=abspath, cmd=['--version'])
dash_dash_version_result = self.exec(bin_name=abspath, cmd=['--version'], timeout=10, quiet=True)
dash_dash_version_out = dash_dash_version_result.stdout.strip()
try:
version = SemVer.parse(dash_dash_version_out)
Expand All @@ -368,7 +368,7 @@ def on_get_version(self, bin_name: BinName, abspath: Optional[HostBinPath]=None,
validation_err = err

# Attempt 2: $ <bin_name> -version
dash_version_out = self.exec(bin_name=abspath, cmd=["-version"]).stdout.strip()
dash_version_out = self.exec(bin_name=abspath, cmd=["-version"], timeout=10, quiet=True).stdout.strip()
try:
version = SemVer.parse(dash_version_out)
assert version, f"Could not parse version from $ {bin_name} -version: {dash_version_out}".strip()
Expand All @@ -377,7 +377,7 @@ def on_get_version(self, bin_name: BinName, abspath: Optional[HostBinPath]=None,
validation_err = validation_err or err

# Attempt 3: $ <bin_name> -v
dash_v_out = self.exec(bin_name=abspath, cmd=["-v"]).stdout.strip()
dash_v_out = self.exec(bin_name=abspath, cmd=["-v"], timeout=10, quiet=True).stdout.strip()
try:
version = SemVer.parse(dash_v_out)
assert version, f"Could not parse version from $ {bin_name} -v: {dash_v_out}".strip()
Expand Down Expand Up @@ -455,8 +455,8 @@ def get_version(self, bin_name: BinName, abspath: Optional[HostBinPath]=None, ov
try:
version = self.call_handler_for_action(
bin_name=bin_name,
handler_type='version',
default_handler=self.on_get_version,
handler_type='version',
overrides=overrides,
abspath=abspath,
timeout=timeout,
Expand Down
2 changes: 1 addition & 1 deletion pydantic_pkgr/binprovider_apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def load_PATH_from_dpkg_install_location(self):
return self

PATH = self.PATH
dpkg_install_dirs = self.exec(bin_name=dpkg_abspath, cmd=["-L", "bash"]).stdout.strip().split("\n")
dpkg_install_dirs = self.exec(bin_name=dpkg_abspath, cmd=["-L", "bash"], quiet=True).stdout.strip().split("\n")
dpkg_bin_dirs = [path for path in dpkg_install_dirs if path.endswith("/bin")]
for bin_dir in dpkg_bin_dirs:
if str(bin_dir) not in PATH:
Expand Down
4 changes: 2 additions & 2 deletions pydantic_pkgr/binprovider_brew.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def on_get_abspath(self, bin_name: BinName | HostBinPath, **context) -> HostBinP
# fallback to using brew info to get the Cellar bin path
for package in (self.on_get_packages(str(bin_name)) or [str(bin_name)]):
try:
info_lines = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['info', '--quiet', package], timeout=5).stdout.strip().split('\n')
info_lines = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['info', '--quiet', package], timeout=5, quiet=True).stdout.strip().split('\n')
# /opt/homebrew/Cellar/curl/8.10.0 (530 files, 4MB)
cellar_path = [line for line in info_lines if '/Cellar/' in line][0].rsplit(' (', 1)[0]
abspath = bin_abspath(bin_name, PATH=f'{cellar_path}/bin')
Expand All @@ -123,7 +123,7 @@ def on_get_version(self, bin_name: BinName, abspath: Optional[HostBinPath]=None,
# fallback to using brew info to get the version
package = (self.on_get_packages(str(bin_name)) or [str(bin_name)])[-1] # assume last package in list is the main one
try:
version_str = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['info', '--quiet', package], timeout=5).stdout.strip()
version_str = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['info', '--quiet', package], quiet=True, timeout=5).stdout.strip()
return SemVer.parse(version_str)
except Exception:
return None
Expand Down
8 changes: 4 additions & 4 deletions pydantic_pkgr/binprovider_npm.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def load_PATH_from_npm_prefix(self):
npm_bin_dirs = {str(self.npm_prefix / 'node_modules/.bin')}
else:
# find all local and global npm PATHs
npm_local_dir = _CACHED_LOCAL_NPM_PREFIX or self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['prefix']).stdout.strip()
npm_local_dir = _CACHED_LOCAL_NPM_PREFIX or self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['prefix'], quiet=True).stdout.strip()
_CACHED_LOCAL_NPM_PREFIX = npm_local_dir

# start at npm_local_dir and walk up to $HOME (or /), finding all npm bin dirs along the way
Expand All @@ -61,7 +61,7 @@ def load_PATH_from_npm_prefix(self):
search_dir = search_dir.parent
num_hops += 1

npm_global_dir = _CACHED_GLOBAL_NPM_PREFIX or self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['prefix', '-g']).stdout.strip() + '/bin' # /opt/homebrew/bin
npm_global_dir = _CACHED_GLOBAL_NPM_PREFIX or self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['prefix', '-g'], quiet=True).stdout.strip() + '/bin' # /opt/homebrew/bin
_CACHED_GLOBAL_NPM_PREFIX = npm_global_dir
npm_bin_dirs.add(npm_global_dir)

Expand Down Expand Up @@ -115,7 +115,7 @@ def on_get_abspath(self, bin_name: BinName | HostBinPath, **context) -> HostBinP
# fallback to using npm show to get alternate binary names based on the package
try:
package = (self.on_get_packages(str(bin_name)) or [str(bin_name)])[-1] # assume last package in list is the main one
output_lines = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['show', package], timeout=5).stdout.strip().split('\n')
output_lines = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['show', package], timeout=5, quiet=True).stdout.strip().split('\n')
bin_name = [line for line in output_lines if line.startswith('bin: ')][0].split('bin: ', 1)[-1].split(', ')[0]
abspath = bin_abspath(bin_name, PATH=self.PATH)
if abspath:
Expand Down Expand Up @@ -144,7 +144,7 @@ def on_get_version(self, bin_name: BinName, abspath: Optional[HostBinPath]=None,
f'--prefix={self.npm_prefix}' if self.npm_prefix else '--global',
'--depth=0',
package,
], timeout=5).stdout.strip()
], timeout=5, quiet=True).stdout.strip()
# /opt/homebrew/lib
# └── @postlight/parser@2.2.3
version_str = output_line.split('\n')[1].rsplit('@', 1)[-1]
Expand Down
6 changes: 3 additions & 3 deletions pydantic_pkgr/binprovider_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def load_PATH_from_pip_sitepackages(self):
elif self.INSTALLER_BIN == "pipx":
# restrict PATH to only use global pipx bin path
if self.INSTALLER_BIN_ABSPATH and shutil.which(self.INSTALLER_BIN_ABSPATH):
proc = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=["environment"]) # run $ pipx environment
proc = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=["environment"], quiet=True, timeout=5) # run $ pipx environment
if proc.returncode == 0:
PIPX_BIN_DIR = proc.stdout.strip().split("PIPX_BIN_DIR=")[-1].split("\n", 1)[0]
pip_bin_dirs = {PIPX_BIN_DIR}
Expand Down Expand Up @@ -159,7 +159,7 @@ def on_get_abspath(self, bin_name: BinName | HostBinPath, **context) -> HostBinP

# fallback to using pip show to get the site-packages bin path
packages = self.on_get_packages(str(bin_name)) or [str(bin_name)]
output_lines = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['show', *packages], timeout=5).stdout.strip().split('\n')
output_lines = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['show', *packages], timeout=5, quiet=True).stdout.strip().split('\n')
try:
location = [line for line in output_lines if line.startswith('Location: ')][0].split('Location: ', 1)[-1]
except IndexError:
Expand All @@ -185,7 +185,7 @@ def on_get_version(self, bin_name: BinName, abspath: Optional[HostBinPath]=None,

# fallback to using pip show to get the version
package = (self.on_get_packages(str(bin_name)) or [str(bin_name)])[-1] # assume last package in list is the main one
output_lines = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['show', package], timeout=5).stdout.strip().split('\n')
output_lines = self.exec(bin_name=self.INSTALLER_BIN_ABSPATH, cmd=['show', package], timeout=5, quiet=True).stdout.strip().split('\n')
try:
version_str = [line for line in output_lines if line.startswith('Version: ')][0].split('Version: ', 1)[-1]
return SemVer.parse(version_str)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pydantic-pkgr"
version = "0.4.0"
version = "0.4.2"
description = "System package manager APIs in strongly typed Python"
authors = [
{name = "Nick Sweeting", email = "pydantic-pkgr-pyproject-toml@sweeting.me"},
Expand Down

0 comments on commit 5bb4205

Please sign in to comment.