Source code for apm.home
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Setup the home and role directories.
:copyright: (c) 2014 by Dalton Hubble
:license: MIT License, see LICENSE for details.
"""
import os
import sys
TOOL_PATH_ENVVAR = "APM_PATH" # envvar to check for custom path
TOOL_USER_PATH = ".apm" # default tool path, within user home dir
[docs]def home_path():
"""Gets the home directory path across platforms."""
return os.path.expanduser("~")
DEFAULT_TOOL_PATH = os.path.join(home_path(), TOOL_USER_PATH)
[docs]def apm_path():
"""Returns the path to the directory for the tool."""
path = os.environ.get(TOOL_PATH_ENVVAR)
if path:
return os.path.expanduser(path)
else:
return DEFAULT_TOOL_PATH
[docs]def roles_path():
"""Returns the path to the roles directory"""
return os.path.join(apm_path(), "roles")
[docs]def vars_path():
"""
Returns the path to the vars directory, whose structure mirrors that
of the roles directory.
"""
return os.path.join(apm_path(), "apmvars")
[docs]def upsert_directory(dir_path):
"""Create a directory at the given path, if it does not exist."""
if os.path.exists(dir_path):
return
else:
try:
os.makedirs(dir_path)
except OSError as e:
sys.exit("{} could not be created: {}".format(dir_path, e))