discover.utils.venv_utils module

Utility functions to create virtual environments and execute commands in them

Author:

Dominik Schiller <dominik.schiller@uni-a.de>

Date:

06.09.2023

discover.utils.venv_utils.get_console_script_run_cmd(env_path, script, args=None, kwargs=None)[source]

Generate a command to run a console script within a virtual environment using direct execution. Uses argument files for large argument lists to avoid system limits.

Parameters:
  • env_path (Path) – Path to the virtual environment.

  • script (str) – Name of the console script to run.

  • args (list, optional) – List of arguments to pass to the script.

  • kwargs (dict, optional) – Dictionary of keyword arguments to pass to the script.

Returns:

(command_list, temp_file_path) where temp_file_path is None if no file was used

Return type:

tuple

Example

>>> get_console_script_run_cmd(Path('/path/to/venv'), 'my_script', ['arg1', 'arg2'], {'--flag': 'value'})
(['/path/to/venv/bin/my_script', 'arg1', 'arg2', '--flag', 'value'], None)
discover.utils.venv_utils.get_module_run_cmd(env_path, module, args=None, kwargs=None)[source]

Generate a command to run a Python module within a virtual environment.

Parameters:
  • env_path (Path) – Path to the virtual environment.

  • module (str) – Python module name.

  • args (list, optional) – List of arguments to pass to the module.

  • kwargs (dict, optional) – Dictionary of keyword arguments to pass to the module.

Returns:

Command argument list.

Return type:

list

Example

>>> get_module_run_cmd(Path('/path/to/venv'), 'mymodule', ['arg1', 'arg2'], {'--flag': 'value'})
['/path/to/venv/bin/python', '-m', 'mymodule', 'arg1', 'arg2', '--flag', 'value']
discover.utils.venv_utils.get_python_script_run_cmd(env_path, script, args=None, kwargs=None)[source]

Generate a command to run a Python script within a virtual environment.

Parameters:
  • env_path (Path) – Path to the virtual environment.

  • script (Path) – Path to the Python script.

  • args (list, optional) – List of arguments to pass to the script.

  • kwargs (dict, optional) – Dictionary of keyword arguments to pass to the script.

Returns:

Command argument list.

Return type:

list

Example

>>> get_python_script_run_cmd(Path('/path/to/venv'), Path('/path/to/script.py'), ['arg1', 'arg2'], {'--flag': 'value'})
['/path/to/venv/bin/python', '/path/to/script.py', 'arg1', 'arg2', '--flag', 'value']
discover.utils.venv_utils.get_shell_script_run_cmd(env_path, script, args=None, kwargs=None)[source]

Generate a command to run a shell script within a virtual environment. The path to the script musst be set in the path environment variable of the console session.

Parameters:
  • env_path (Path) – Path to the virtual environment.

  • script (Path) – Path to the Python script.

  • args (list, optional) – List of arguments to pass to the script.

  • kwargs (dict, optional) – Dictionary of keyword arguments to pass to the script.

Returns:

Run command (shell-based for script execution).

Return type:

str

Example

>>> get_shell_script_run_cmd(Path('/path/to/venv'), 'my_script', ['arg1', 'arg2'], {'--flag': 'value'})
'source /path/to/venv/bin/activate && my_script arg1 arg2 --flag value'
discover.utils.venv_utils.venv_dir_from_mod(module_dir)[source]

Returns the path to a virtual environment directory matchin a provided module directory.

Parameters:

module_dir (Path) – Path to the module directory.

Returns:

Virtual environment directory.

Return type:

Path

Raises:

ValueError – If the NOVA_CACHE_DIR environment variable is not set.

Example

>>> venv_dir_from_mod(Path('/path/to/my_module'))
Path('/path/to/venvs/my_module')