From b2998734cabc9e954f1d55f3431fbee578c1aa6d Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Sun, 2 Sep 2018 15:48:43 +0200 Subject: [PATCH] provide an option to install all dotfiles to a temporary directory --- README.md | 37 +++++++++++++++++++++++++++++++------ dotdrop/dotdrop.py | 37 +++++++++++++++++++++---------------- dotdrop/installer.py | 7 ++++++- 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 946724a..2a354ff 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,10 @@ $ dotdrop.sh compare The diffing is done by diff in the backend, one can provide specific options to diff using the `-o` switch. +It is also possible to install all dotfiles for a specific profile +in a temporary directory in order to manually compare them with +the local version by using `install` and the `-t` switch. + ## Import dotfiles Dotdrop allows to import dotfiles directly from the @@ -477,16 +481,37 @@ $ sudo pip3 install dotdrop --upgrade ## Update dotfiles Dotfiles managed by dotdrop can be updated using the `update` command. + There are two cases: - * the dotfile doesn't use [templating](#template): the new version of the dotfile is copied to the - *dotfiles* directory and overwrites the old version. If git is used to version the dotfiles stored - by dotdrop, the git command `diff` can be used to view the changes. - * the dotfile uses [templating](#template): the dotfile must be manually updated, the use of - the dotdrop command `compare` can be helpful to identify the changes to apply to the template. +**The dotfile doesn't use [templating](#template)** -``` +The new version of the dotfile is copied to the *dotfiles* directory and overwrites +the old version. If git is used to version the dotfiles stored by dotdrop, the git command +`diff` can be used to view the changes. + +```bash $ dotdrop.sh update ~/.vimrc +$ git diff +``` + +**The dotfile uses [templating](#template)** + +The dotfile must be manually updated, two solutions can be used to identify the +changes to apply to the template: + +* Use dotdrop's `compare` command +* Install the dotfiles to a temporary directory (using the `install` command and the + `-t` switch) and compare the generated dotfile with the local one. + +```bash +# use compare to identify change(s) +$ ./dotdrop.sh compare --files=~/.vimrc + +# use install to identify change(s) +$ ./dotdrop.sh install -t +Installed to tmp /tmp/dotdrop-6kix7555 +$ diff ~/.vimrc /tmp/dotdrop-6kix7555/home/user/.vimrc ``` ## Store sensitive dotfiles diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 16b620d..f772091 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -41,14 +41,14 @@ USAGE = """ {} Usage: - dotdrop install [-fndVb] [-c ] [-p ] - dotdrop import [-ldVb] [-c ] [-p ] ... - dotdrop compare [-Vb] [-c ] [-p ] - [-o ] [-i ...] - [--files=] - dotdrop update [-fdVb] [-c ] ... - dotdrop listfiles [-Vb] [-c ] [-p ] - dotdrop list [-Vb] [-c ] + dotdrop install [-tfndVb] [-c ] [-p ] + dotdrop import [-ldVb] [-c ] [-p ] ... + dotdrop compare [-Vb] [-c ] [-p ] + [-o ] [-i ...] + [--files=] + dotdrop update [-fdVb] [-c ] ... + dotdrop listfiles [-Vb] [-c ] [-p ] + dotdrop list [-Vb] [-c ] dotdrop --help dotdrop --version @@ -59,6 +59,7 @@ Options: -i --ignore= File name to ignore when diffing. -o --dopts= Diff options [default: ]. -n --nodiff Do not diff when installing. + -t --temp Install to a temporary directory for review. -l --link Import and link. -f --force Do not warn if exists. -V --verbose Be verbose. @@ -74,7 +75,7 @@ Options: ########################################################### -def install(opts, conf): +def install(opts, conf, temporary=False): """install all dotfiles for this profile""" dotfiles = conf.get_dotfiles(opts['profile']) if dotfiles == []: @@ -83,10 +84,13 @@ def install(opts, conf): return False t = Templategen(opts['profile'], base=opts['dotpath'], variables=opts['variables'], debug=opts['debug']) + tmpdir = None + if temporary: + tmpdir = get_tmpdir() inst = Installer(create=opts['create'], backup=opts['backup'], dry=opts['dry'], safe=opts['safe'], base=opts['dotpath'], workdir=opts['workdir'], diff=opts['installdiff'], - debug=opts['debug']) + debug=opts['debug'], totemp=tmpdir) installed = [] for dotfile in dotfiles: if dotfile.actions and Cfg.key_actions_pre in dotfile.actions: @@ -126,6 +130,8 @@ def install(opts, conf): LOG.dbg('executing post action {}'.format(action)) action.execute() installed.extend(r) + if temporary: + LOG.log('\nInstalled to tmp {}.'.format(tmpdir)) LOG.log('\n{} dotfile(s) installed.'.format(len(installed))) return True @@ -154,7 +160,6 @@ def apply_trans(opts, dotfile): def _select(selections, dotfiles): - ret = True selected = [] for selection in selections: df = next( @@ -166,8 +171,7 @@ def _select(selections, dotfiles): selected.append(df) else: LOG.err('no dotfile matches \"{}\"'.format(selection)) - ret = False - return selected, ret + return selected def compare(opts, conf, tmp, focus=None, ignore=[]): @@ -181,7 +185,7 @@ def compare(opts, conf, tmp, focus=None, ignore=[]): same = True selected = dotfiles if focus: - selected, ret = _select(focus.replace(' ', '').split(','), dotfiles) + selected = _select(focus.replace(' ', '').split(','), dotfiles) if len(selected) < 1: return False @@ -362,13 +366,14 @@ def main(): elif args['install']: # install the dotfiles stored in dotdrop - ret = install(opts, conf) + ret = install(opts, conf, temporary=args['--temp']) elif args['compare']: # compare local dotfiles with dotfiles stored in dotdrop tmp = get_tmpdir() opts['dopts'] = args['--dopts'] - ret = compare(opts, conf, tmp, args['--files'], args['--ignore']) + ret = compare(opts, conf, tmp, focus=args['--files'], + ignore=args['--ignore']) if os.listdir(tmp): LOG.raw('\ntemporary files available under {}'.format(tmp)) else: diff --git a/dotdrop/installer.py b/dotdrop/installer.py index 8557464..022dffb 100644 --- a/dotdrop/installer.py +++ b/dotdrop/installer.py @@ -20,7 +20,7 @@ class Installer: def __init__(self, base='.', create=True, backup=True, dry=False, safe=False, workdir='~/.config/dotdrop', - debug=False, diff=True): + debug=False, diff=True, totemp=None): self.create = create self.backup = backup self.dry = dry @@ -29,6 +29,7 @@ class Installer: self.base = base self.debug = debug self.diff = diff + self.totemp = totemp self.comparing = False self.log = Logger() @@ -36,6 +37,8 @@ class Installer: """install the src to dst using a template""" src = os.path.join(self.base, os.path.expanduser(src)) dst = os.path.expanduser(dst) + if self.totemp: + dst = self._pivot_path(dst, self.totemp) if utils.samefile(src, dst): # symlink loop self.log.err('dotfile points to itself: {}'.format(dst)) @@ -50,6 +53,8 @@ class Installer: """set src as the link target of dst""" src = os.path.join(self.base, os.path.expanduser(src)) dst = os.path.expanduser(dst) + if self.totemp: + return self.install(templater, src, dst) if Templategen.is_template(src): if self.debug: