2019-10-06 01:13:51 -04:00
|
|
|
import os
|
|
|
|
|
2022-04-30 21:19:22 -04:00
|
|
|
from ..plugin import Plugin
|
2019-10-06 01:13:51 -04:00
|
|
|
|
2022-04-30 21:19:22 -04:00
|
|
|
|
|
|
|
class Create(Plugin):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2019-10-06 01:13:51 -04:00
|
|
|
Create empty paths.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2019-10-06 01:13:51 -04:00
|
|
|
|
2022-01-30 18:48:30 -05:00
|
|
|
_directive = "create"
|
2019-10-06 01:13:51 -04:00
|
|
|
|
|
|
|
def can_handle(self, directive):
|
|
|
|
return directive == self._directive
|
|
|
|
|
|
|
|
def handle(self, directive, data):
|
|
|
|
if directive != self._directive:
|
2022-01-30 18:48:30 -05:00
|
|
|
raise ValueError("Create cannot handle directive %s" % directive)
|
2019-10-06 01:13:51 -04:00
|
|
|
return self._process_paths(data)
|
|
|
|
|
|
|
|
def _process_paths(self, paths):
|
|
|
|
success = True
|
2022-01-30 18:48:30 -05:00
|
|
|
defaults = self._context.defaults().get("create", {})
|
2021-02-25 06:25:17 -05:00
|
|
|
for key in paths:
|
2022-04-25 10:02:58 -04:00
|
|
|
path = os.path.abspath(os.path.expandvars(os.path.expanduser(key)))
|
2022-01-30 18:48:30 -05:00
|
|
|
mode = defaults.get("mode", 0o777) # same as the default for os.makedirs
|
2021-02-25 06:25:17 -05:00
|
|
|
if isinstance(paths, dict):
|
|
|
|
options = paths[key]
|
|
|
|
if options:
|
2022-01-30 18:48:30 -05:00
|
|
|
mode = options.get("mode", mode)
|
2021-02-25 06:25:17 -05:00
|
|
|
success &= self._create(path, mode)
|
2019-10-06 01:13:51 -04:00
|
|
|
if success:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.info("All paths have been set up")
|
2019-10-06 01:13:51 -04:00
|
|
|
else:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.error("Some paths were not successfully set up")
|
2019-10-06 01:13:51 -04:00
|
|
|
return success
|
|
|
|
|
|
|
|
def _exists(self, path):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2019-10-06 01:13:51 -04:00
|
|
|
Returns true if the path exists.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2019-10-06 01:13:51 -04:00
|
|
|
path = os.path.expanduser(path)
|
|
|
|
return os.path.exists(path)
|
|
|
|
|
2021-02-25 06:25:17 -05:00
|
|
|
def _create(self, path, mode):
|
2019-10-06 01:13:51 -04:00
|
|
|
success = True
|
|
|
|
if not self._exists(path):
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.debug("Trying to create path %s with mode %o" % (path, mode))
|
2019-10-06 01:13:51 -04:00
|
|
|
try:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.lowinfo("Creating path %s" % path)
|
2021-02-25 06:25:17 -05:00
|
|
|
os.makedirs(path, mode)
|
2022-04-25 10:02:58 -04:00
|
|
|
# On Windows, the *mode* argument to `os.makedirs()` is ignored.
|
|
|
|
# The mode must be set explicitly in a follow-up call.
|
|
|
|
os.chmod(path, mode)
|
2019-10-06 01:13:51 -04:00
|
|
|
except OSError:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.warning("Failed to create path %s" % path)
|
2019-10-06 01:13:51 -04:00
|
|
|
success = False
|
|
|
|
else:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.lowinfo("Path exists %s" % path)
|
2019-10-06 01:13:51 -04:00
|
|
|
return success
|