/usr/lib/python3/dist-packages/provisioningserver/path.py is in python3-maas-provisioningserver 2.4.0~beta2-6865-gec43e47e6-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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | # Copyright 2014-2016 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_data_path',
'get_tentative_data_path',
]
from os import (
getenv,
makedirs,
)
from os.path import (
abspath,
dirname,
join,
)
def get_path_env(env):
"""Return ``env`` if set, else "/"."""
path = getenv(env)
if path is None:
return "/"
elif len(path) == 0:
return "/"
else:
return path
def get_tentative_data_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 snap, demo,
and development environments can redirect them to a playground location.
For example:
* If ``MAAS_ROOT`` is set to ``/tmp/maasroot``, then
``get_tentative_data_path()`` will return ``/tmp/maasroot`` and
``get_tentative_data_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_tentative_data_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 = join('.', *path_elements).lstrip('/')
path = join(get_path_env('MAAS_ROOT'), path)
return abspath(path)
def get_data_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_data_path` for details.
"""
path = get_tentative_data_path(*path_elements)
# Make sure that the path to the file actually exists.
makedirs(dirname(path), exist_ok=True)
return path
def get_path(*path_elements):
"""Return an absolute path based on the `MAAS_PATH` environment variable.
Use this to compute paths like ``/usr/bin/avahi-browse``, so that snap,
can redirect them to a playground location.
Unlike ``get_data_path`` the path returned by ``get_path`` does have to
exists and it will not try to force its existence.
For example:
* If ``MAAS_PATH`` is set to ``/snap/maas/current``, then
``get_tentative_path()`` will return ``/snap/maas/current`` and
``get_tentative_data_path('/usr/bin/avahi-browse')`` will return
``/snap/maas/current/usr/bin/avahi-browse``.
* If ``MAAS_PATH`` is not set, you just get (a normalised version of) the
location you passed in; just ``get_tentative_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 = join('.', *path_elements).lstrip('/')
path = join(get_path_env('MAAS_PATH'), path)
return abspath(path)
|