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, 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
- galaxy.util.path.has_ext(path, ext, aliases=False, ignore=None)[source]¶
Determine whether
path
has extensionext
- 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 withext
separated by a singleos.extsep
- galaxy.util.path.safe_contains(prefix, path, allowlist=None, real=None)[source]¶
Ensure a path is contained within another path.
Given any two filesystem paths, ensure that
path
is contained inprefix
. Ifpath
exists (either as an absolute path or relative toprefix
), it is canonicalized withos.path.realpath()
to ensure it is not a symbolic link that points outside ofprefix
. If it is a symbolic link andallowlist
is set, the symbolic link may also point inside aallowlist
path.The
path
is checked againstallowlist
using either its absolute pathname (if passed in as absolute) or relative toprefix
and canonicalized (if applicable). It is notos.path.join()``ed with each ``allowlist
directory.- Parameters
prefix (string) – a directory under which
path
is to be checkedpath (string) – a filename to check
allowlist (list of strings) – list of additional paths under which
path
may be located
- Return type
- Returns
True
ifpath
is contained withinprefix
orallowlist
,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
- 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 underpath
- galaxy.util.path.unsafe_walk(path, allowlist=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. withsafe_relpath()
orsafe_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