2016-01-16 22:00:15 -05:00
|
|
|
import os, shutil, dotbot
|
2014-03-19 23:07:30 -04:00
|
|
|
|
2016-01-16 22:00:15 -05:00
|
|
|
class Link(dotbot.Plugin):
|
2014-03-19 23:07:30 -04:00
|
|
|
'''
|
|
|
|
Symbolically links dotfiles.
|
|
|
|
'''
|
|
|
|
|
2014-04-24 15:41:34 -04:00
|
|
|
_directive = 'link'
|
|
|
|
|
2014-03-19 23:07:30 -04:00
|
|
|
def can_handle(self, directive):
|
2014-04-24 15:41:34 -04:00
|
|
|
return directive == self._directive
|
2014-03-19 23:07:30 -04:00
|
|
|
|
|
|
|
def handle(self, directive, data):
|
2014-04-24 15:41:34 -04:00
|
|
|
if directive != self._directive:
|
2016-01-16 22:00:15 -05:00
|
|
|
raise ValueError('Link cannot handle directive %s' % directive)
|
2014-03-19 23:07:30 -04:00
|
|
|
return self._process_links(data)
|
|
|
|
|
|
|
|
def _process_links(self, links):
|
|
|
|
success = True
|
2016-03-02 20:53:19 -05:00
|
|
|
defaults = self._context.defaults().get('link', {})
|
2014-03-19 23:07:30 -04:00
|
|
|
for destination, source in links.items():
|
2015-05-12 20:04:42 -04:00
|
|
|
destination = os.path.expandvars(destination)
|
2016-03-02 20:53:19 -05:00
|
|
|
relative = defaults.get('relative', False)
|
|
|
|
force = defaults.get('force', False)
|
|
|
|
relink = defaults.get('relink', False)
|
|
|
|
create = defaults.get('create', False)
|
2014-10-22 13:27:34 -04:00
|
|
|
if isinstance(source, dict):
|
|
|
|
# extended config
|
2016-03-02 20:53:19 -05:00
|
|
|
relative = source.get('relative', relative)
|
|
|
|
force = source.get('force', force)
|
|
|
|
relink = source.get('relink', relink)
|
|
|
|
create = source.get('create', create)
|
2016-08-17 21:15:02 -04:00
|
|
|
path = source['path']
|
2014-10-22 13:27:34 -04:00
|
|
|
else:
|
2016-08-17 21:15:02 -04:00
|
|
|
path = source
|
|
|
|
path = os.path.expandvars(os.path.expanduser(path))
|
2016-03-02 20:53:19 -05:00
|
|
|
if create:
|
|
|
|
success &= self._create(destination)
|
|
|
|
if force or relink:
|
2016-11-17 14:12:22 -05:00
|
|
|
if self._exists(path):
|
|
|
|
success &= self._delete(path, destination, relative, force)
|
2016-02-14 22:39:48 -05:00
|
|
|
success &= self._link(path, destination, relative)
|
2014-03-19 23:07:30 -04:00
|
|
|
if success:
|
|
|
|
self._log.info('All links have been set up')
|
|
|
|
else:
|
|
|
|
self._log.error('Some links were not successfully set up')
|
|
|
|
return success
|
|
|
|
|
|
|
|
def _is_link(self, path):
|
|
|
|
'''
|
|
|
|
Returns true if the path is a symbolic link.
|
|
|
|
'''
|
|
|
|
return os.path.islink(os.path.expanduser(path))
|
|
|
|
|
|
|
|
def _link_destination(self, path):
|
|
|
|
'''
|
2016-04-05 18:04:42 -04:00
|
|
|
Returns the destination of the symbolic link.
|
2014-03-19 23:07:30 -04:00
|
|
|
'''
|
|
|
|
path = os.path.expanduser(path)
|
2016-04-05 18:04:42 -04:00
|
|
|
return os.readlink(path)
|
2014-03-19 23:07:30 -04:00
|
|
|
|
|
|
|
def _exists(self, path):
|
|
|
|
'''
|
|
|
|
Returns true if the path exists.
|
|
|
|
'''
|
|
|
|
path = os.path.expanduser(path)
|
|
|
|
return os.path.exists(path)
|
|
|
|
|
2014-10-22 14:44:40 -04:00
|
|
|
def _create(self, path):
|
|
|
|
success = True
|
|
|
|
parent = os.path.abspath(os.path.join(os.path.expanduser(path), os.pardir))
|
|
|
|
if not self._exists(parent):
|
|
|
|
try:
|
|
|
|
os.makedirs(parent)
|
|
|
|
except OSError:
|
|
|
|
self._log.warning('Failed to create directory %s' % parent)
|
|
|
|
success = False
|
|
|
|
else:
|
|
|
|
self._log.lowinfo('Creating directory %s' % parent)
|
|
|
|
return success
|
|
|
|
|
2016-04-05 18:04:42 -04:00
|
|
|
def _delete(self, source, path, relative, force):
|
2014-10-22 13:27:34 -04:00
|
|
|
success = True
|
2016-03-02 20:53:19 -05:00
|
|
|
source = os.path.join(self._context.base_directory(), source)
|
2016-04-05 18:04:42 -04:00
|
|
|
fullpath = os.path.expanduser(path)
|
|
|
|
if relative:
|
|
|
|
source = self._relative_path(source, fullpath)
|
2014-11-09 09:00:19 -05:00
|
|
|
if ((self._is_link(path) and self._link_destination(path) != source) or
|
|
|
|
(self._exists(path) and not self._is_link(path))):
|
2015-05-02 22:27:05 -04:00
|
|
|
removed = False
|
2014-10-22 13:27:34 -04:00
|
|
|
try:
|
2015-04-27 12:30:23 -04:00
|
|
|
if os.path.islink(fullpath):
|
|
|
|
os.unlink(fullpath)
|
2015-05-02 22:27:05 -04:00
|
|
|
removed = True
|
|
|
|
elif force:
|
|
|
|
if os.path.isdir(fullpath):
|
|
|
|
shutil.rmtree(fullpath)
|
|
|
|
removed = True
|
|
|
|
else:
|
|
|
|
os.remove(fullpath)
|
|
|
|
removed = True
|
2014-10-22 13:27:34 -04:00
|
|
|
except OSError:
|
|
|
|
self._log.warning('Failed to remove %s' % path)
|
|
|
|
success = False
|
|
|
|
else:
|
2015-05-02 22:27:05 -04:00
|
|
|
if removed:
|
|
|
|
self._log.lowinfo('Removing %s' % path)
|
2014-10-22 13:27:34 -04:00
|
|
|
return success
|
|
|
|
|
2016-04-05 18:04:42 -04:00
|
|
|
def _relative_path(self, source, destination):
|
|
|
|
'''
|
|
|
|
Returns the relative path to get to the source file from the
|
|
|
|
destination file.
|
|
|
|
'''
|
|
|
|
destination_dir = os.path.dirname(destination)
|
|
|
|
return os.path.relpath(source, destination_dir)
|
|
|
|
|
2016-02-14 22:39:48 -05:00
|
|
|
def _link(self, source, link_name, relative):
|
2014-03-19 23:07:30 -04:00
|
|
|
'''
|
|
|
|
Links link_name to source.
|
|
|
|
|
|
|
|
Returns true if successfully linked files.
|
|
|
|
'''
|
|
|
|
success = False
|
2016-04-05 18:04:42 -04:00
|
|
|
destination = os.path.expanduser(link_name)
|
|
|
|
absolute_source = os.path.join(self._context.base_directory(), source)
|
|
|
|
if relative:
|
|
|
|
source = self._relative_path(absolute_source, destination)
|
|
|
|
else:
|
|
|
|
source = absolute_source
|
2014-07-15 21:25:53 -04:00
|
|
|
if (not self._exists(link_name) and self._is_link(link_name) and
|
|
|
|
self._link_destination(link_name) != source):
|
2014-03-19 23:07:30 -04:00
|
|
|
self._log.warning('Invalid link %s -> %s' %
|
|
|
|
(link_name, self._link_destination(link_name)))
|
2016-04-05 18:04:42 -04:00
|
|
|
# we need to use absolute_source below because our cwd is the dotfiles
|
|
|
|
# directory, and if source is relative, it will be relative to the
|
|
|
|
# destination directory
|
|
|
|
elif not self._exists(link_name) and self._exists(absolute_source):
|
2014-07-17 13:02:53 -04:00
|
|
|
try:
|
2016-02-14 22:39:48 -05:00
|
|
|
os.symlink(source, destination)
|
2014-08-20 16:58:42 -04:00
|
|
|
except OSError:
|
2014-07-17 13:02:53 -04:00
|
|
|
self._log.warning('Linking failed %s -> %s' % (link_name, source))
|
|
|
|
else:
|
|
|
|
self._log.lowinfo('Creating link %s -> %s' % (link_name, source))
|
|
|
|
success = True
|
2014-03-19 23:07:30 -04:00
|
|
|
elif self._exists(link_name) and not self._is_link(link_name):
|
|
|
|
self._log.warning(
|
|
|
|
'%s already exists but is a regular file or directory' %
|
|
|
|
link_name)
|
2014-07-15 21:25:53 -04:00
|
|
|
elif self._is_link(link_name) and self._link_destination(link_name) != source:
|
2014-03-19 23:07:30 -04:00
|
|
|
self._log.warning('Incorrect link %s -> %s' %
|
|
|
|
(link_name, self._link_destination(link_name)))
|
2016-04-05 18:04:42 -04:00
|
|
|
# again, we use absolute_source to check for existence
|
|
|
|
elif not self._exists(absolute_source):
|
2014-07-15 21:25:53 -04:00
|
|
|
if self._is_link(link_name):
|
2015-01-26 10:36:11 -05:00
|
|
|
self._log.warning('Nonexistent target %s -> %s' %
|
2014-07-15 21:25:53 -04:00
|
|
|
(link_name, source))
|
|
|
|
else:
|
2015-01-26 10:36:11 -05:00
|
|
|
self._log.warning('Nonexistent target for %s : %s' %
|
2014-07-15 21:25:53 -04:00
|
|
|
(link_name, source))
|
2014-03-19 23:07:30 -04:00
|
|
|
else:
|
|
|
|
self._log.lowinfo('Link exists %s -> %s' % (link_name, source))
|
|
|
|
success = True
|
|
|
|
return success
|