gws.lib.osx

Utilities for os/shell scripting

Source code: gws.lib.osx

Package Contents

exception gws.lib.osx.Error

Bases: gws.Error

GWS error.

exception gws.lib.osx.TimeoutError

Bases: Error

GWS error.

gws.lib.osx.getenv(key: str, default: str = None) str | None

Returns the value for a given environment-variable.

Parameters:
  • key – An environment-variable.

  • default – The default return.

Returns:

default if no key has been found, if there is such key then the value for the environment-variable is returned.

gws.lib.osx.run_nowait(cmd: str, **kwargs) subprocess.Popen

Run a process and return immediately.

Parameters:
  • cmd – A process to run.

  • kwargs

Returns:

The output of the command.

gws.lib.osx.run(cmd: str | list, input: str = None, echo: bool = False, strict: bool = True, timeout: float = None, **kwargs) str

Run an external command.

Parameters:
  • cmd – Command to run.

  • input – Input data.

  • echo – Echo the output instead of capturing it.

  • strict – Raise an error on a non-zero exit code.

  • timeout – Timeout.

  • kwargs – Arguments to pass to subprocess.Popen.

Returns:

The command output.

Deletes a given path.

Parameters:

path – Filepath.

gws.lib.osx.rename(src: _Path, dst: _Path) bool

Moves and renames the source path according to the given destination.

Parameters:
  • src – Path to source.

  • dst – Destination.

gws.lib.osx.chown(path: _Path, user: int = None, group: int = None) bool

Changes the UID or GID for a given path.

Parameters:
  • path – Filepath.

  • user – UID.

  • group – GID.

gws.lib.osx.mkdir(path: _Path, mode: int = 493, user: int = None, group: int = None) bool

Check a (possibly nested) directory.

Parameters:
  • path – Path to a directory.

  • mode – Directory creation mode.

  • user – Directory user (defaults to gws.c.UID)

  • group – Directory group (defaults to gws.c.GID)

gws.lib.osx.rmdir(path: _Path) bool

Remove a directory or a directory tree.

Parameters:

path – Path to a directory. Can be non-empty

gws.lib.osx.file_mtime(path: _Path) float

Returns the time from epoch when the path was recently changed.

Parameters:

path – File-/directory-path.

Returns:

Time since epoch in seconds until most recent change in file.

gws.lib.osx.file_age(path: _Path) int

Returns the amount of seconds since the path has been changed.

Parameters:

path – Filepath.

Returns:

Amount of seconds since most recent change in file, if the path is invalid -1 is returned.

gws.lib.osx.file_size(path: _Path) int

Returns the file size.

Parameters:

path – Filepath.

Returns:

Amount of characters in the file or -1 if the path is invalid.

gws.lib.osx.file_checksum(path: _Path) str

Returns the checksum of the file.

Parameters:

path – Filepath.

Returns:

Empty string if the path is invalid, otherwise the file’s checksum.

gws.lib.osx.kill_pid(pid: int, sig_name='TERM') bool

Kills a process.

Parameters:
  • pid – Process ID.

  • sig_name

Returns:

True if the process with the given PID is killed or does not exist.``False `` if the process could not be killed.

gws.lib.osx.running_pids() dict[int, str]

Returns the current pids and the corresponding process’ name.

gws.lib.osx.process_rss_size(unit: str = 'm') float

Returns the Resident Set Size.

Parameters:

unitm | k | g

Returns:

The Resident Set Size with the given unit.

gws.lib.osx.find_files(dirname: _Path, pattern=None, ext=None, deep: bool = True)

Finds files in a given directory.

Parameters:
  • dirname – Path to directory.

  • pattern – Pattern to match.

  • ext – extension to match.

  • deep – If true then searches through all subdirectories for files, otherwise it returns the files only in the given directory.

Returns:

A generator object.

gws.lib.osx.find_directories(dirname: _Path, pattern=None, deep: bool = True)

Finds all directories in a given directory.

Parameters:
  • dirname – Path to directory.

  • pattern – Pattern to match.

  • deep – If true then searches through all subdirectories for directories, otherwise it returns the directories only in the given directory.

Returns:

A generator object.

gws.lib.osx.parse_path(path: _Path) dict[str, str]

Parse a path into a dict(path,dirname,filename,name,extension).

Parameters:

path – Path.

Returns:

A dict(path,dirname,filename,name,extension).

gws.lib.osx.file_name(path: _Path) str

Returns the filename.

Parameters:

path – Filepath.

Returns:

The filename.

gws.lib.osx.is_abs_path(path: _Path) bool
gws.lib.osx.abs_path(path: _Path, base: _Path) str

Absolutize a relative path with respect to a base directory or file path.

Parameters:
  • path – A path.

  • base – A path to the base.

Raises:

ValueError – If base is empty

Returns:

The absolute path.

gws.lib.osx.abs_web_path(path: str, basedir: str) str | None

Return an absolute path in a base dir and ensure the path is correct.

Parameters:
  • path – Path to absolutize.

  • basedir – Path to base directory.

Returns:

Absolute path with respect to base directory.

gws.lib.osx.rel_path(path: _Path, base: _Path) str

Relativize an absolute path with respect to a base directory or file path.

Parameters:
  • path – Path to relativize.

  • base – Path to base directory.

Returns:

Relativized path with respect to base directory.