Warning

This document is for an old release of Galaxy. You can alternatively view this page in the latest release if it exists or view the top of the latest release's documentation.

galaxy.util.path package

Path manipulation functions.

galaxy.util.path.get_ext(path: AnyStr, ignore=None, canonicalize=True) str[source]

Return the extension of path

Parameters:
  • path (string) – Path to check

  • ignore (string) – Ignore this extension at the end of the path (e.g. sample)

  • canonicalize (bool) – If the extension is known to this module, return the canonicalized extension instead of the file’s actual extension

Return type:

string

galaxy.util.path.has_ext(path: AnyStr, ext: str, aliases=False, ignore=None)[source]

Determine whether path has extension ext

Parameters:
  • path (string) – Path to check

  • ext (string) – Extension to check

  • aliases (bool) – Check any known aliases for the given extension

  • ignore (string) – Ignore this extension at the end of the path (e.g. sample)

Return type:

bool

Returns:

True if path is a YAML file, False otherwise.

galaxy.util.path.join(a, *p)[source]

Join two or more pathname components, inserting ‘/’ as needed. If any component is an absolute path, all previous path components will be discarded. An empty last part will result in a path that ends with a separator.

galaxy.util.path.joinext(root: str, ext: str) str[source]

Roughly the reverse of os.path.splitext.

Parameters:
  • root (string) – part of the filename before the extension

  • ext – the extension

Return type:

string

Returns:

root joined with ext separated by a single os.extsep

galaxy.util.path.safe_contains(prefix: Union[AnyStr, PathLike], path: Union[AnyStr, PathLike], allowlist: Optional[List[Union[AnyStr, PathLike]]] = None, real=None)[source]

Ensure a path is contained within another path.

Given any two filesystem paths, ensure that path is contained in prefix. If path exists (either as an absolute path or relative to prefix), it is canonicalized with os.path.realpath() to ensure it is not a symbolic link that points outside of prefix. If it is a symbolic link and allowlist is set, the symbolic link may also point inside a allowlist path.

The path is checked against allowlist using either its absolute pathname (if passed in as absolute) or relative to prefix and canonicalized (if applicable). It is not os.path.join()``ed with each ``allowlist directory.

Parameters:
  • prefix (string) – a directory under which path is to be checked

  • path (string) – a filename to check

  • allowlist (list of strings) – list of additional paths under which path may be located

Return type:

bool

Returns:

True if path is contained within prefix or allowlist, False otherwise.

galaxy.util.path.safe_makedirs(path: Union[AnyStr, PathLike]) None[source]

Safely make a directory, do not fail if it already exists or is created during execution.

Parameters:

path (string) – a directory to create

galaxy.util.path.safe_relpath(path: Union[AnyStr, PathLike]) bool[source]

Determine whether a relative path references a path outside its root.

This is a path computation: the filesystem is not accessed to confirm the existence or nature of path.

Parameters:

path (string) – a path to check

Return type:

bool

Returns:

True if path is relative and does not reference a path in a parent directory, False otherwise.

galaxy.util.path.safe_walk(path, allowlist=None)[source]

Walk a path and return only the contents that are not symlinks outside the path.

Symbolic links are followed if a allowlist is provided. The path itself cannot be a symbolic link unless the pointed to location is in the allowlist.

Parameters:
  • path (string) – a directory to check for unsafe contents

  • allowlist (list of strings) – list of additional paths under which contents may be located

Return type:

iterator

Returns:

Iterator of “safe” os.walk() tuples found under path

galaxy.util.path.unsafe_walk(path: Union[AnyStr, PathLike], allowlist: Optional[List[Union[AnyStr, PathLike]]] = None, username: Optional[str] = None)[source]

Walk a path and ensure that none of its contents are symlinks outside the path.

It is assumed that path itself has already been validated e.g. with safe_relpath() or safe_contains(). This function is most useful for the case where you want to test whether a directory contains safe paths, but do not want to actually walk the safe contents.

Parameters:
  • path (string) – a directory to check for unsafe contents

  • allowlist (list of strings) – list of additional paths under which contents may be located

Return type:

list of strings

Returns:

A list of “bad” files found under path

Submodules

galaxy.util.path.ntpath module

Galaxy “safe” path functions forced to work with Windows-style paths regardless of current platform

galaxy.util.path.posixpath module

Galaxy “safe” path functions forced to work with POSIX-style paths regardless of current platform