Implement globbing support
This commit is contained in:
parent
a517c4c5d0
commit
dece710399
2 changed files with 48 additions and 11 deletions
|
@ -30,10 +30,11 @@ class Dispatcher(object):
|
||||||
try:
|
try:
|
||||||
success &= plugin.handle(action, task[action])
|
success &= plugin.handle(action, task[action])
|
||||||
handled = True
|
handled = True
|
||||||
except Exception:
|
except Exception as err:
|
||||||
self._log.error(
|
self._log.error(
|
||||||
'An error was encountered while executing action %s' %
|
'An error was encountered while executing action %s' %
|
||||||
action)
|
action)
|
||||||
|
self._log.debug(err)
|
||||||
if not handled:
|
if not handled:
|
||||||
success = False
|
success = False
|
||||||
self._log.error('Action %s not handled' % action)
|
self._log.error('Action %s not handled' % action)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import os
|
import os
|
||||||
|
import glob
|
||||||
import shutil
|
import shutil
|
||||||
import dotbot
|
import dotbot
|
||||||
|
|
||||||
|
@ -27,23 +28,57 @@ class Link(dotbot.Plugin):
|
||||||
force = defaults.get('force', False)
|
force = defaults.get('force', False)
|
||||||
relink = defaults.get('relink', False)
|
relink = defaults.get('relink', False)
|
||||||
create = defaults.get('create', False)
|
create = defaults.get('create', False)
|
||||||
|
use_glob = defaults.get('use_glob', False)
|
||||||
if isinstance(source, dict):
|
if isinstance(source, dict):
|
||||||
# extended config
|
# extended config
|
||||||
relative = source.get('relative', relative)
|
relative = source.get('relative', relative)
|
||||||
force = source.get('force', force)
|
force = source.get('force', force)
|
||||||
relink = source.get('relink', relink)
|
relink = source.get('relink', relink)
|
||||||
create = source.get('create', create)
|
create = source.get('create', create)
|
||||||
|
use_glob = source.get('use_glob', use_glob)
|
||||||
path = self._default_source(destination, source.get('path'))
|
path = self._default_source(destination, source.get('path'))
|
||||||
else:
|
else:
|
||||||
path = self._default_source(destination, source)
|
path = self._default_source(destination, source)
|
||||||
path = os.path.expandvars(os.path.expanduser(path))
|
path = os.path.expandvars(os.path.expanduser(path))
|
||||||
|
if use_glob:
|
||||||
|
self._log.debug("Globbing with path: " + str(path))
|
||||||
|
glob_results = glob.glob(path)
|
||||||
|
if len(glob_results) is 0:
|
||||||
|
self._log.warning("Globbing couldn't find anything matching " + str(path))
|
||||||
|
success = False
|
||||||
|
continue
|
||||||
|
glob_star_loc = path.find('*')
|
||||||
|
if glob_star_loc is -1 and destination[-1] is '/':
|
||||||
|
self._log.error("Ambiguous action requested.")
|
||||||
|
self._log.error("No wildcard in glob, directory use undefined: " +
|
||||||
|
destination + " -> " + str(glob_results))
|
||||||
|
self._log.warning("Did you want to link the directory or into it?")
|
||||||
|
success = False
|
||||||
|
continue
|
||||||
|
elif glob_star_loc is -1 and len(glob_results) is 1:
|
||||||
|
# perform a normal link operation
|
||||||
|
if create:
|
||||||
|
success &= self._create(destination)
|
||||||
|
if force or relink:
|
||||||
|
success &= self._delete(path, destination, relative, force)
|
||||||
|
success &= self._link(path, destination, relative)
|
||||||
|
else:
|
||||||
|
self._log.lowinfo("Linking globbed items: " + str(glob_results))
|
||||||
|
glob_base = path[:glob_star_loc]
|
||||||
|
for glob_full_item in glob_results:
|
||||||
|
glob_item = glob_full_item[len(glob_base):]
|
||||||
|
glob_link_destination = destination + glob_item
|
||||||
|
if create:
|
||||||
|
success &= self._create(glob_link_destination)
|
||||||
|
success &= self._link(glob_full_item, glob_link_destination, relative)
|
||||||
|
else:
|
||||||
|
if create:
|
||||||
|
success &= self._create(destination)
|
||||||
if not self._exists(os.path.join(self._context.base_directory(), path)):
|
if not self._exists(os.path.join(self._context.base_directory(), path)):
|
||||||
success = False
|
success = False
|
||||||
self._log.warning('Nonexistent target %s -> %s' %
|
self._log.warning('Nonexistent target %s -> %s' %
|
||||||
(destination, path))
|
(destination, path))
|
||||||
continue
|
continue
|
||||||
if create:
|
|
||||||
success &= self._create(destination)
|
|
||||||
if force or relink:
|
if force or relink:
|
||||||
success &= self._delete(path, destination, relative, force)
|
success &= self._delete(path, destination, relative, force)
|
||||||
success &= self._link(path, destination, relative)
|
success &= self._link(path, destination, relative)
|
||||||
|
@ -87,6 +122,7 @@ class Link(dotbot.Plugin):
|
||||||
success = True
|
success = True
|
||||||
parent = os.path.abspath(os.path.join(os.path.expanduser(path), os.pardir))
|
parent = os.path.abspath(os.path.join(os.path.expanduser(path), os.pardir))
|
||||||
if not self._exists(parent):
|
if not self._exists(parent):
|
||||||
|
self._log.debug("Try to create parent: " + str(parent))
|
||||||
try:
|
try:
|
||||||
os.makedirs(parent)
|
os.makedirs(parent)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|
Loading…
Reference in a new issue