1
0
Fork 0
mirror of synced 2024-11-17 14:05:35 -05:00
This commit is contained in:
Ben Klein 2018-03-29 13:36:54 +00:00 committed by GitHub
commit e5afdd1489
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 19 deletions

View file

@ -165,17 +165,22 @@ files if necessary. Environment variables in paths are automatically expanded.
Link commands are specified as a dictionary mapping targets to source
locations. Source locations are specified relative to the base directory (that
is specified when running the installer). Directory names should *not* contain
a trailing "/" character.
is specified when running the installer). If linking directories, *do not* include a trailing slash.
Link commands support an (optional) extended configuration. In this type of
configuration, instead of specifying source locations directly, targets are
mapped to extended configuration dictionaries. These dictionaries map `path` to
the source path, specify `create` as `true` if the parent directory should be
created if necessary, specify `relink` as `true` if incorrect symbolic links
should be automatically overwritten, specify `force` as `true` if the file or
directory should be forcibly linked, and specify `relative` as `true` if the
symbolic link should have a relative path.
mapped to extended configuration dictionaries.
Available extended configuration parameters:
| Link Option | Explanation |
| -- | -- |
| `path` | The target for the symlink, the same as in the shortcut syntax (default:null, automatic (see below)) |
| `create` | When true, create parent directories to the link as needed. (default:false) |
| `relink` | Removes the old target if it's a symlink (default:false) |
| `force` | Force removes the old target, file or folder, and forces a new link (default:false) |
| `relative` | Use a relative path when creating the symlink (default:false, absolute links) |
| `use_glob` | Treat a `*` character as a wildcard, and perform link operations on all of those matches (default:false) |
#### Example
@ -207,6 +212,10 @@ the following three config files equivalent:
~/.zshrc:
force: true
path: zshrc
~/.config/:
use_glob: true
path: config/*
relink: true
```
```yaml
@ -217,6 +226,10 @@ the following three config files equivalent:
relink: true
~/.zshrc:
force: true
~/.config/:
use_glob: true
path: config/*
relink: true
```
```json
@ -230,6 +243,11 @@ the following three config files equivalent:
},
"~/.zshrc": {
"force": true
},
"~/.config/": {
"use_glob": true,
"path": "config/*",
"relink": true
}
}
}

View file

@ -30,10 +30,11 @@ class Dispatcher(object):
try:
success &= plugin.handle(action, task[action])
handled = True
except Exception:
except Exception as err:
self._log.error(
'An error was encountered while executing action %s' %
action)
self._log.debug(err)
if not handled:
success = False
self._log.error('Action %s not handled' % action)

View file

@ -1,4 +1,5 @@
import os
import glob
import shutil
import dotbot
@ -27,26 +28,62 @@ class Link(dotbot.Plugin):
force = defaults.get('force', False)
relink = defaults.get('relink', False)
create = defaults.get('create', False)
use_glob = defaults.get('use_glob', False)
if isinstance(source, dict):
# extended config
relative = source.get('relative', relative)
force = source.get('force', force)
relink = source.get('relink', relink)
create = source.get('create', create)
use_glob = source.get('use_glob', use_glob)
path = self._default_source(destination, source.get('path'))
else:
path = self._default_source(destination, source)
path = os.path.expandvars(os.path.expanduser(path))
if not self._exists(os.path.join(self._context.base_directory(), path)):
success = False
self._log.warning('Nonexistent target %s -> %s' %
(destination, path))
continue
if create:
success &= self._create(destination)
if force or relink:
success &= self._delete(path, destination, relative, force)
success &= self._link(path, destination, relative)
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("Globs from '" + path + "': " + 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)
if force or relink:
success &= self._delete(glob_full_item, glob_link_destination, relative, force)
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)):
success = False
self._log.warning('Nonexistent target %s -> %s' %
(destination, path))
continue
if force or relink:
success &= self._delete(path, destination, relative, force)
success &= self._link(path, destination, relative)
if success:
self._log.info('All links have been set up')
else:
@ -87,6 +124,7 @@ class Link(dotbot.Plugin):
success = True
parent = os.path.abspath(os.path.join(os.path.expanduser(path), os.pardir))
if not self._exists(parent):
self._log.debug("Try to create parent: " + str(parent))
try:
os.makedirs(parent)
except OSError: