galaxy.util.path package

Path manipulation functions.

galaxy.util.path.safe_contains(prefix, path, whitelist=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 whitelist is set, the symbolic link may also point inside a whitelist path.

The path is checked against whitelist 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 ``whitelist directory.

Parameters:
  • prefix (string) – a directory under which path is to be checked
  • path (string) – a filename to check
  • whitelist (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 whitelist, False otherwise.

galaxy.util.path.safe_makedirs(path)[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)[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, whitelist=None)[source]

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

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

Parameters:
  • path (string) – a directory to check for unsafe contents
  • whitelist (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, whitelist=None, username=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
  • whitelist (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

galaxy.util.path.joinext(root, ext)[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.has_ext(path, ext, 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.get_ext(path, ignore=None, canonicalize=True)[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

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