Skip to content

Commit

Permalink
fix file direct access wrap with os.access
Browse files Browse the repository at this point in the history
  • Loading branch information
pirate committed Oct 8, 2024
1 parent 300f3a4 commit caabcd3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions pydantic_pkgr/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def validate_binprovider_name(name: str) -> str:
def validate_bin_dir(path: Path) -> Path:
path = path.expanduser().absolute()
assert path.resolve()
assert path.is_dir(), f'path entries to add to $PATH must be absolute paths to directories {dir}'
assert os.access(path, os.R_OK) and path.is_dir(), f'path entries to add to $PATH must be absolute paths to directories {dir}'
return path

BinDirPath = Annotated[Path, AfterValidator(validate_bin_dir)]
Expand Down Expand Up @@ -78,7 +78,7 @@ def bin_name(bin_path_or_name: str | Path) -> str:
@validate_call
def path_is_file(path: Path | str) -> Path:
path = Path(path) if isinstance(path, str) else path
assert path.is_file(), f'Path is not a file: {path}'
assert os.access(path, os.F_OK), f'Path is not a file: {path}'
return path

HostExistsPath = Annotated[Path, AfterValidator(path_is_file)]
Expand Down Expand Up @@ -129,12 +129,12 @@ def bin_abspath(bin_path_or_name: str | BinName | Path, PATH: PATHStr | None=Non
for path in PATH.split(':'):
bin_dir = Path(path)
# print('BIN_DIR', bin_dir, bin_dir.is_dir())
if not bin_dir.is_dir():
if not (os.access(bin_dir, os.R_OK) and bin_dir.is_dir()):
# raise Exception(f'Found invalid dir in $PATH: {bin_dir}')
continue
bin_file = bin_dir / bin_path_or_name
# print(bin_file, path, bin_file.exists(), bin_file.is_file(), bin_file.is_symlink())
if bin_file.exists():
if os.access(bin_file, os.F_OK):
return bin_file

return None
Expand Down
6 changes: 3 additions & 3 deletions pydantic_pkgr/binprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def exec(
if bin_name == self.name:
assert self.loaded_abspath, "Binary must have a loaded_abspath, make sure to load_or_install() first"
assert self.loaded_version, "Binary must have a loaded_version, make sure to load_or_install() first"
assert Path(cwd).is_dir(), f"cwd must be a valid directory: {cwd}"
assert os.access(cwd, os.R_OK) and Path(cwd).is_dir(), f"cwd must be a valid directory: {cwd}"
cmd = [str(bin_name), *(str(arg) for arg in cmd)]
if not quiet:
print('$', ' '.join(cmd), file=sys.stderr)
Expand Down Expand Up @@ -227,7 +227,7 @@ def exec(self, bin_name: BinName | HostBinPath, cmd: Iterable[str | Path | int |
else:
bin_abspath = self.get_abspath(str(bin_name))
assert bin_abspath, f'BinProvider {self.name} cannot execute bin_name {bin_name} because it could not find its abspath. (Did {self.__class__.__name__}.load_or_install({bin_name}) fail?)'
assert Path(cwd).is_dir(), f'cwd must be a valid directory: {cwd}'
assert os.access(cwd, os.R_OK) and Path(cwd).is_dir(), f'cwd must be a valid directory: {cwd}'
cmd = [str(bin_abspath), *(str(arg) for arg in cmd)]
if not quiet:
print('$', ' '.join(cmd), file=sys.stderr)
Expand Down Expand Up @@ -416,7 +416,7 @@ def get_sha256(self, bin_name: BinName, abspath: Optional[HostBinPath]=None) ->
"""Get the sha256 hash of the binary at the given abspath (or equivalent hash of the underlying package)"""

abspath = abspath or self.get_abspath(bin_name)
if not abspath or not abspath.exists():
if not abspath or not os.access(abspath, os.R_OK):
return None

if sys.version_info >= (3, 11):
Expand Down
5 changes: 3 additions & 2 deletions pydantic_pkgr/binprovider_brew.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#!/usr/bin/env python3
__package__ = "pydantic_pkgr"

import os
import sys
import platform
from typing import Optional
Expand Down Expand Up @@ -34,9 +35,9 @@ def load_PATH(self):

PATHs = set()

if OS == 'darwin' and DEFAULT_MACOS_DIR.exists():
if OS == 'darwin' and os.access(DEFAULT_MACOS_DIR, os.R_OK):
PATHs.add(str(DEFAULT_MACOS_DIR))
if OS != 'darwin' and DEFAULT_LINUX_DIR.exists():
if OS != 'darwin' and os.access(DEFAULT_LINUX_DIR, os.R_OK):
PATHs.add(str(DEFAULT_LINUX_DIR))

if not PATHs:
Expand Down
4 changes: 2 additions & 2 deletions pydantic_pkgr/binprovider_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def INSTALLER_BIN_ABSPATH(self) -> HostBinPath | None:

# use venv pip
venv_pip_path = self.pip_venv / "bin" / self.INSTALLER_BIN
if venv_pip_path.is_file() and path_is_executable(venv_pip_path):
if os.access(venv_pip_path, os.R_OK) and path_is_executable(venv_pip_path):
abspath = str(venv_pip_path)
else:
# use system pip
Expand Down Expand Up @@ -112,7 +112,7 @@ def setup(self):

# create new venv in pip_venv if it doesnt exist
venv_pip_path = self.pip_venv / "bin" / "python"
if not venv_pip_path.is_file():
if not os.access(venv_pip_path, os.F_OK):
import venv

venv.create(
Expand Down

0 comments on commit caabcd3

Please sign in to comment.