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 extensionext
- 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 withext
separated by a singleos.extsep
- galaxy.util.path.safe_contains(prefix: AnyStr | PathLike, path: AnyStr | PathLike, allowlist: List[AnyStr | PathLike] | None = 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.
- galaxy.util.path.safe_makedirs(path: 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: 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:
- 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: AnyStr | PathLike, allowlist: List[AnyStr | PathLike] | None = None, username: str | None = 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.
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