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.utime() float

Returns the time in seconds since the Epoch.

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) tuple

Run a process, return a tuple (rc, output).

Parameters:
  • cmd – Command to run.

  • input

  • echo

  • strict

  • timeout

  • kwargs

Returns:

(rc,output)

Deletes a given path.

Parameters:

path – Filepath.

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

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)

Changes the UID or GID for a given path.

Parameters:
  • path – Filepath.

  • user – UID.

  • group – GID.

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

Reuturs 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.

Agrs:

unit: m | 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: str) 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 absolutized 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:

Absolutized path with respect to base directory.

gws.lib.osx.rel_path(path: _Path, base: str) 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.