From 05b78ca5fe43345f35b0ea1a6fbb5c4c8fc666c9 Mon Sep 17 00:00:00 2001 From: moyiz Date: Fri, 5 May 2017 17:02:18 +0300 Subject: [PATCH] Add support for importing dotfiles as links --- dotdrop/config.py | 4 +++- dotdrop/dotdrop.py | 16 +++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/dotdrop/config.py b/dotdrop/config.py index 597fdf8..213463c 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -87,7 +87,7 @@ class Cfg: return absconf return dotpath - def new(self, dotfile, profile): + def new(self, dotfile, profile, link=False): """ import new dotfile """ dots = self.content[self.key_dotfiles] if dots is None: @@ -103,6 +103,8 @@ class Cfg: self.key_dotfiles_dst: dotfile.dst, self.key_dotfiles_src: dotfile.src } + if link: + dots[dotfile.key][self.key_dotfiles_link] = True profiles = self.profiles if profile in profiles and profiles[profile] != [self.key_all]: if self.content[self.key_profiles][profile] is None: diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index a6096d4..23f108d 100755 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -34,7 +34,7 @@ Usage: [(-f | --force)] [--nodiff] [--dry] dotdrop.py compare [--profile=] [--cfg=] dotdrop.py list [--cfg=] - dotdrop.py import [--cfg=] [--profile=] [--dry] ... + dotdrop.py import [(-l | --link)] [--cfg=] [--profile=] [--dry] ... dotdrop.py (-h | --help) dotdrop.py (-v | --version) @@ -43,6 +43,7 @@ Options: --cfg= Path to the config [default: %s/config.yaml]. --dry Dry run. --nodiff Do not diff when installing [default: False]. + -l --link Import the file and add a link to it [default: False]. -f --force Do not warn if exists [default: False]. -v --version Show version. -h --help Show this screen. @@ -101,8 +102,7 @@ def importer(opts, conf, paths): key = dst.split(os.sep)[-1] if key == 'config': key = '_'.join(dst.split(os.sep)[-2:]) - key = key.lstrip('.') - key = key.lower() + key = key.lstrip('.').lower() if os.path.isdir(dst): key = 'd_%s' % (key) else: @@ -116,17 +116,22 @@ def importer(opts, conf, paths): if os.path.exists(srcf): LOG.err('\"%s\" already exists, ignored !' % (srcf)) continue - conf.new(dotfile, opts['profile']) + conf.new(dotfile, opts['profile'], opts['link']) cmd = ['mkdir', '-p', '%s' % (os.path.dirname(srcf))] if opts['dry']: LOG.dry('would run: %s' % (' '.join(cmd))) else: utils.run(cmd, raw=False, log=False) - cmd = ['cp', '-r', '%s' % (dst), '%s' % (srcf)] + if opts['link']: + cmd = ['mv', '%s' % (dst), '%s' % (srcf)] + else: + cmd = ['cp', '-r', '%s' % (dst), '%s' % (srcf)] if opts['dry']: LOG.dry('would run: %s' % (' '.join(cmd))) else: utils.run(cmd, raw=False, log=False) + if opts['link']: + os.symlink(srcf, dst) LOG.sub('\"%s\" imported' % (path)) cnt += 1 if opts['dry']: @@ -159,6 +164,7 @@ if __name__ == '__main__': opts['profile'] = args['--profile'] opts['safe'] = not args['--force'] opts['installdiff'] = not args['--nodiff'] + opts['link'] = args['--link'] header()