# loki.tools.files

Module Attributes

`TMP_DIR` | An instance of `LokiTempdir` representing the temporary directory that the current Loki instance uses.

Functions

`delete`(filename[, force]) |
`filehash`(source[, prefix, suffix]) | Generate a filename from a hash of `source` with an optional `prefix`.
`find_files`(pattern[, srcdir]) | Case-insensitive alternative for glob patterns that recursively walks all sub-directories and matches a case-insensitive regex pattern.
`find_paths`(directory, pattern[, ignore, sort]) | Utility function to generate a list of file paths based on include and exclude patterns applied to a root directory.
`gettempdir`() | Get a Loki-specific tempdir
`load_module`(module[, path]) | Handle import paths and load the compiled module
`local_loki_cleanup`(target, backup) | Companion utility to `local_loki_setup` to revert the changes.
`local_loki_setup`(here) | Utility method that is used to determine paths for injecting the currently running source code of Loki into an ecbundle-based worktree This is used for regression tests to facilitate the use of a local Loki source copy in the build.
`write_env_launch_script`(here, binary, args) | Utility method that is used for regression tests that require activating an environment file before running `binary`.

Classes

`LokiTempdir`() | Data structure to hold an instance of `tempfile.TemporaryDirectory` to provide a Loki-specific temporary directory that is automatically cleaned up when the Python interpreter is terminated

class LokiTempdir

  Bases: `object`

  Data structure to hold an instance of `tempfile.TemporaryDirectory` to provide a Loki-specific temporary directory that is automatically cleaned up when the Python interpreter is terminated

  This class provides the temporary directory creation that `gettempdir` relies upon.

  create()

    Create the temporary directory

  get()

    Get the temporary directory path

    Return type:

      pathlib.Path

  cleanup()

    Clean up the temporary directory

gettempdir()

  Get a Loki-specific tempdir

  Throughout the lifetime of the Python interpreter process, this will always return the same temporary directory.

  The base directory, under which the temporary directory resides, can be specified by setting the environment variable `LOKI_TMP_DIR`. Otherwise the platform default will be used, observing the rules specified by `tempfile.gettempdir`.

  The temporary directory is created, managed, and cleaned up by an instance of `LokiTempdir`. Loki will choose a process-specific temporary directory under the base directory to avoid race conditions between concurrently running Loki instances. The initialisation mechanism is lazy, creating the temporary directory only when this function is called for the first time.

filehash(source, prefix=None, suffix=None)

  Generate a filename from a hash of `source` with an optional `prefix`.

delete(filename, force=False)

find_paths(directory, pattern, ignore=None, sort=True)

  Utility function to generate a list of file paths based on include and exclude patterns applied to a root directory.

  Parameters:

    - directory (str or `pathlib.Path`) – Root directory from which to glob files.

    - pattern (list of str) – A list of glob patterns generating files to include in the list.

    - ignore (list of str, optional) – A list of glob patterns generating files to exclude from the list.

    - sort (bool, optional) – Flag to indicate alphabetic ordering of files

  Returns:

    The list of file names

  Return type:

    list

find_files(pattern, srcdir='.')

  Case-insensitive alternative for glob patterns that recursively walks all sub-directories and matches a case-insensitive regex pattern.

  Basic idea from: http://stackoverflow.com/questions/8151300/ignore-case-in-glob-on-linux

load_module(module, path=None)

  Handle import paths and load the compiled module

write_env_launch_script(here, binary, args)

  Utility method that is used for regression tests that require activating an environment file before running `binary`.

  This writes a simple script of the form

      source env.sh
      bin/<binary> <args>
      exit $?

  Parameters:

    - here (pathlib.Path or str) – The directory in which the script is created

    - binary (str) – The name of the binary

    - args (list of str) – List of arguments to pass to the binary

  Returns:

    The path to the created script file

  Return type:

    pathlib.Path

local_loki_setup(here)

  Utility method that is used to determine paths for injecting the currently running source code of Loki into an ecbundle-based worktree This is used for regression tests to facilitate the use of a local Loki source copy in the build. In particular, any existing Loki source copy in the bundle worktree is moved to a backup location.

  Warning

  If a backup copy exists already at the backup location, this is removed before moving the existing Loki copy to the backup location.

  Note that injecting the currently running Loki installation only works if it has been installed in editable mode. However, this utility also does not take care of the actual injection of the currently running installation, therefore making this also useful if the purpose is to trigger a Loki download via the bundle create mechanism.

  The companion utility `local_loki_cleanup` can be used to revert these changes.

  Parameters:

    here (pathlib.Path) – The root path of the bundle worktree.

  Returns:

    The absolute path to the base directory of the currently running Loki installation, the `target` path where Loki needs to be injected in the bundle directory, and the `backup` path where an existing Loki copy in the bundle has been moved to.

  Return type:

    tuple of (str, pathlib.Path, pathlib.Path)

local_loki_cleanup(target, backup)

  Companion utility to `local_loki_setup` to revert the changes.

  This removes a symlink at `target`, if it exists, and moves the `backup` path in its original location.

  Parameters:

    - target (pathlib.Path) – The target injection path as returned by `local_loki_setup`

    - backup (pathlib.Path) – The backup path as created by `local_loki_setup`