/usr/lib/python3/dist-packages/provisioningserver/path.py is in python3-maas-provisioningserver 2.0.0~beta3+bzr4941-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # Copyright 2014-2015 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Compute paths relative to root."""
__all__ = [
'get_path',
'get_tentative_path',
]
import os
from provisioningserver.utils.fs import ensure_dir
def get_root():
"""Return ``MAAS_ROOT`` if set, else "/"."""
root = os.getenv('MAAS_ROOT')
if root is None:
return "/"
elif len(root) == 0:
return "/"
else:
return root
def get_tentative_path(*path_elements):
"""Return an absolute path based on the `MAAS_ROOT` environment variable.
Use this to compute paths like ``/var/lib/maas/gnupg``, so that demo and
development environments can redirect them to a playground location.
For example:
* If ``MAAS_ROOT`` is set to ``/tmp/maasroot``, then ``get_path()`` will
return ``/tmp/maasroot`` and ``get_path('/var/lib/maas')`` will return
``/tmp/maasroot/var/lib/maas``.
* If ``MAAS_ROOT`` is not set, you just get (a normalised version of) the
location you passed in; just ``get_path()`` will always return the root
directory.
This call may have minor side effects: it reads environment variables and
the current working directory. Side effects during imports are bad, so
avoid using this in global variables. Instead of exporting a variable that
holds your path, export a getter function that returns your path. Add
caching if it becomes a performance problem.
"""
# Strip off a leading slash from the given path, if any. If it were left
# in, it would override preceding path elements and MAAS_ROOT would be
# ignored later on. The dot is there to make the call work even with zero
# path elements.
path = os.path.join('.', *path_elements).lstrip('/')
path = os.path.join(get_root(), path)
return os.path.abspath(path)
def get_path(*path_elements):
"""Return an absolute path based on the `MAAS_ROOT` environment variable.
This also ensures that the parent directory of the resultant path exists
and is a directory.
See `get_tentative_path` for details.
"""
path = get_tentative_path(*path_elements)
# Make sure that the path to the file actually exists.
ensure_dir(os.path.dirname(path))
return path
|