1
0
Fork 0
mirror of synced 2024-06-25 18:31:09 -04:00
dotbot/dotbot/plugins/create.py

51 lines
1.4 KiB
Python
Raw Normal View History

import os
import dotbot
class Create(dotbot.Plugin):
2021-02-15 06:11:16 -05:00
"""
Create empty paths.
2021-02-15 06:11:16 -05:00
"""
2021-02-15 06:11:16 -05:00
_directive = "create"
def can_handle(self, directive):
return directive == self._directive
def handle(self, directive, data):
if directive != self._directive:
2021-02-15 06:11:16 -05:00
raise ValueError("Create cannot handle directive %s" % directive)
return self._process_paths(data)
def _process_paths(self, paths):
success = True
for path in paths:
2021-02-14 03:21:16 -05:00
path = os.path.normpath(os.path.expandvars(os.path.expanduser(path)))
success &= self._create(path)
if success:
2021-02-15 06:11:16 -05:00
self._log.info("All paths have been set up")
else:
2021-02-15 06:11:16 -05:00
self._log.error("Some paths were not successfully set up")
return success
def _exists(self, path):
2021-02-15 06:11:16 -05:00
"""
Returns true if the path exists.
2021-02-15 06:11:16 -05:00
"""
path = os.path.expanduser(path)
return os.path.exists(path)
def _create(self, path):
success = True
if not self._exists(path):
2021-02-15 06:11:16 -05:00
self._log.debug("Trying to create path %s" % path)
try:
2021-02-15 06:11:16 -05:00
self._log.lowinfo("Creating path %s" % path)
os.makedirs(path)
except OSError:
2021-02-15 06:11:16 -05:00
self._log.warning("Failed to create path %s" % path)
success = False
else:
2021-02-15 06:11:16 -05:00
self._log.lowinfo("Path exists %s" % path)
return success