os_helper.system_utils module

System Utilities

Cross-platform helpers for operating-system detection, worker-count resolution, executing external commands, and opening files in the system default application.

Author: - Warith HARCHAOUI, https://linkedin.com/in/warith-harchaoui

os_helper.system_utils.get_nb_workers(workers=-1)[source]

Resolve a worker count, following scikit-learn’s n_jobs convention.

The default pool size is os.cpu_count() (or 1 if that returns None), overridable via the NB_WORKERS environment variable.

Parameters:

workers (int, optional) –

  • 0 : use the full pool size.

  • > 0: use exactly that many workers.

  • < 0: use pool_size + workers + 1 (e.g. -1 → all CPUs, -2 → all but one), clamped to at least 1.

Returns:

The resolved worker count (always >= 1).

Return type:

int

Example

>>> get_nb_workers()  # -1 → all available CPU cores
4
os_helper.system_utils.getpid()[source]

Return the current process ID as a string.

Returns:

str(os.getpid()).

Return type:

str

os_helper.system_utils.linux()[source]

Determine if the current operating system is Linux.

Returns:

True if the operating system is Linux, False otherwise.

Return type:

bool

os_helper.system_utils.macos()[source]

Determine if the current operating system is macOS.

Returns:

True if the operating system is macOS, False otherwise.

Return type:

bool

os_helper.system_utils.openfile(filename)[source]

Open a file in the platform’s default application.

Uses os.startfile on Windows, open on macOS, and xdg-open on Linux. Exceptions from the underlying call are propagated as-is so the caller can react to them.

Parameters:

filename (str) – The path to the file to open.

Raises:

OSError – If the platform is unsupported or the underlying open call fails.

Return type:

None

os_helper.system_utils.system(cmd, expected_output='', check_exitcode=True, check_empty=False)[source]

Run a shell-style command via subprocess and capture its output.

The command string is parsed with shlex.split() and executed without spawning an actual shell (shell=False), which avoids shell-injection pitfalls while still accepting a familiar command string.

Parameters:
  • cmd (str) – Command line to execute (e.g., "ffmpeg -i in.mp4 out.mp3").

  • expected_output (str, optional) – If non-empty, a file or directory path expected to be present once the command completes successfully.

  • check_exitcode (bool, optional) – If True, assert that the process exit code is 0.

  • check_empty (bool, optional) – If True, also assert that expected_output is non-empty (file size > 0 / directory not empty).

Returns:

{"out": <stdout as str>, "err": <stderr as str>}.

Return type:

dict

Raises:

AssertionError – If the exit code check or the expected-output check fails.

os_helper.system_utils.unix()[source]

Determine if the current operating system is Unix-based (Linux or macOS).

Returns:

True if the operating system is Unix-based (Linux or macOS), False otherwise.

Return type:

bool

os_helper.system_utils.windows()[source]

Determine if the current operating system is Windows.

Returns:

True if the operating system is Windows, False otherwise.

Return type:

bool