snakeoil.osutils package

Submodules

Module contents

OS related functionality

snakeoil.osutils.abspath(path)[source]

resolve a path absolutely, including symlink resolving.

Note that if it’s a symlink and the target doesn’t exist, it’ll still return the target.

Parameters:

path – filepath to resolve.

Raises:

EnvironmentError – some errno other than an ENOENT or EINVAL is encountered

Returns:

the absolute path calculated against the filesystem

Return the absolute path of a symlink

Parameters:

path – filepath to resolve

Returns:

resolved path

Raises:

EnvironmentError – with errno=ENINVAL if the requested path isn’t a symlink

snakeoil.osutils.ensure_dirs(path, gid=-1, uid=-1, mode=511, minimal=True)[source]

ensure dirs exist, creating as needed with (optional) gid, uid, and mode.

Be forewarned- if mode is specified to a mode that blocks the euid from accessing the dir, this code will try to create the dir.

Parameters:
  • path – directory to ensure exists on disk

  • gid – a valid GID to set any created directories to

  • uid – a valid UID to set any created directories to

  • mode – permissions to set any created directories to

  • minimal – boolean controlling whether or not the specified mode must be enforced, or is the minimal permissions necessary. For example, if mode=0755, minimal=True, and a directory exists with mode 0707, this will restore the missing group perms resulting in 757.

Returns:

True if the directory could be created/ensured to have those permissions, False if not.

snakeoil.osutils.join(*a, **kw)
snakeoil.osutils.listdir(*a, **kw)
snakeoil.osutils.listdir_dirs(path, followSymlinks=True)[source]

Return a list of all subdirectories within a directory

Parameters:
  • path – directory to scan

  • followSymlinks – this controls if symlinks are resolved. If True and the symlink resolves to a directory, it is returned, else if False it isn’t returned.

Returns:

list of directories within path

snakeoil.osutils.listdir_files(path, followSymlinks=True)[source]

Return a list of all files within a directory

Parameters:
  • path – directory to scan

  • followSymlinks – this controls if symlinks are resolved. If True and the symlink resolves to a file, it is returned, else if False it isn’t returned.

Returns:

list of files within path

snakeoil.osutils.normpath(mypath: str) str[source]

normalize path- //usr/bin becomes /usr/bin, /usr/../bin becomes /bin

see os.path.normpath() for details- this function differs from os.path.normpath only in that it’ll convert leading ‘//’ into ‘/’

snakeoil.osutils.pjoin(*a, **kw)
snakeoil.osutils.supported_systems(*systems)[source]

Decorator limiting functions to specified systems.

Supported platforms are passed as string arguments. When run on any other system (determined using sys.platform), the function fails immediately with NotImplementedError.

Example usage:

>>> from snakeoil.osutils import supported_systems
>>> @supported_systems('linux', 'darwin')
>>> def func(param):
...     return True
>>>
>>> if sys.platform.startswith(('linux', 'darwin')):
>>>     assert func() == True

NotImplementedError is raised on platforms that aren’t supported.

>>> @supported_systems('nonexistent')
>>> def func2(param):
...     return False
>>>
>>> func2()
Traceback (most recent call last):
    ...
NotImplementedError: func2 not supported on nonexistent

wrap os.unlink, ignoring if the file doesn’t exist

Parameters:

path – a non directory target to ensure doesn’t exist