def cmd_update(o): """update the dotfile(s) from path(s) or key(s)""" cnt = 0 paths = o.update_path iskey = o.update_iskey if o.profile not in [p.key for p in o.profiles]: LOG.err('no such profile \"{}\"'.format(o.profile)) return False adapt_workers(o, LOG) if not paths: # update the entire profile if iskey: if o.debug: LOG.dbg('update by keys: {}'.format(paths)) paths = [d.key for d in o.dotfiles] else: if o.debug: LOG.dbg('update by paths: {}'.format(paths)) paths = [d.dst for d in o.dotfiles] msg = 'Update all dotfiles for profile \"{}\"'.format(o.profile) if o.safe and not LOG.ask(msg): LOG.log('\n{} file(s) updated.'.format(cnt)) return False if not paths: LOG.log('\nno dotfile to update') return True if o.debug: LOG.dbg('dotfile to update: {}'.format(paths)) # update each dotfile if o.workers > 1: # in parallel if o.debug: LOG.dbg('run with {} workers'.format(o.workers)) ex = futures.ThreadPoolExecutor(max_workers=o.workers) wait_for = [] for path in paths: j = ex.submit(_dotfile_update, o, path, key=iskey) wait_for.append(j) # check result for f in futures.as_completed(wait_for): if f.result(): cnt += 1 else: # sequentially for path in paths: if _dotfile_update(o, path, key=iskey): cnt += 1 LOG.log('\n{} file(s) updated.'.format(cnt)) return cnt == len(paths)
def cmd_install(o): """install dotfiles for this profile""" dotfiles = o.dotfiles prof = o.conf.get_profile() adapt_workers(o, LOG) pro_pre_actions = prof.get_pre_actions() if prof else [] pro_post_actions = prof.get_post_actions() if prof else [] if o.install_keys: # filtered dotfiles to install uniq = uniq_list(o.install_keys) dotfiles = [d for d in dotfiles if d.key in uniq] if not dotfiles: msg = 'no dotfile to install for this profile (\"{}\")' LOG.warn(msg.format(o.profile)) return False # the installer tmpdir = None if o.install_temporary: tmpdir = get_tmpdir() installed = [] # execute profile pre-action if o.debug: LOG.dbg('run {} profile pre actions'.format(len(pro_pre_actions))) t = _get_templater(o) ret, err = action_executor(o, pro_pre_actions, [], t, post=False)() if not ret: return False # install each dotfile if o.workers > 1: # in parallel if o.debug: LOG.dbg('run with {} workers'.format(o.workers)) ex = futures.ThreadPoolExecutor(max_workers=o.workers) wait_for = [] for dotfile in dotfiles: j = ex.submit(_dotfile_install, o, dotfile, tmpdir=tmpdir) wait_for.append(j) # check result for f in futures.as_completed(wait_for): r, key, err = f.result() if r: installed.append(key) elif err: LOG.err('installing \"{}\" failed: {}'.format(key, err)) else: # sequentially for dotfile in dotfiles: r, key, err = _dotfile_install(o, dotfile, tmpdir=tmpdir) # check result if r: installed.append(key) elif err: LOG.err('installing \"{}\" failed: {}'.format(key, err)) # execute profile post-action if len(installed) > 0 or o.install_force_action: if o.debug: msg = 'run {} profile post actions' LOG.dbg(msg.format(len(pro_post_actions))) ret, err = action_executor(o, pro_post_actions, [], t, post=False)() if not ret: return False if o.debug: LOG.dbg('install done: installed \"{}\"'.format(','.join(installed))) if o.install_temporary: LOG.log('\ninstalled to tmp \"{}\".'.format(tmpdir)) LOG.log('\n{} dotfile(s) installed.'.format(len(installed))) return True
def cmd_install(opts): """install dotfiles for this profile""" dotfiles = opts.dotfiles prof = opts.conf.get_profile() adapt_workers(opts, LOG) pro_pre_actions = prof.get_pre_actions() if prof else [] pro_post_actions = prof.get_post_actions() if prof else [] if opts.install_keys: # filtered dotfiles to install uniq = uniq_list(opts.install_keys) dotfiles = [d for d in dotfiles if d.key in uniq] if not dotfiles: msg = 'no dotfile to install for this profile (\"{}\")' LOG.warn(msg.format(opts.profile)) return False LOG.dbg('dotfiles registered for install: {}'.format( [k.key for k in dotfiles])) # the installer tmpdir = None if opts.install_temporary: tmpdir = get_tmpdir() installed = [] # clear the workdir if opts.install_clear_workdir and not opts.dry: LOG.dbg('clearing the workdir under {}'.format(opts.workdir)) for root, _, files in os.walk(opts.workdir): for file in files: fpath = os.path.join(root, file) removepath(fpath, logger=LOG) # execute profile pre-action LOG.dbg('run {} profile pre actions'.format(len(pro_pre_actions))) templ = _get_templater(opts) ret, _ = action_executor(opts, pro_pre_actions, [], templ, post=False)() if not ret: return False # install each dotfile if opts.workers > 1: # in parallel LOG.dbg('run with {} workers'.format(opts.workers)) ex = futures.ThreadPoolExecutor(max_workers=opts.workers) wait_for = [] for dotfile in dotfiles: j = ex.submit(_dotfile_install, opts, dotfile, tmpdir=tmpdir) wait_for.append(j) # check result for fut in futures.as_completed(wait_for): tmpret, key, err = fut.result() # check result if tmpret: installed.append(key) elif err: LOG.err('installing \"{}\" failed: {}'.format(key, err)) else: # sequentially for dotfile in dotfiles: tmpret, key, err = _dotfile_install(opts, dotfile, tmpdir=tmpdir) # check result if tmpret: installed.append(key) elif err: LOG.err('installing \"{}\" failed: {}'.format(key, err)) # execute profile post-action if len(installed) > 0 or opts.install_force_action: msg = 'run {} profile post actions' LOG.dbg(msg.format(len(pro_post_actions))) ret, _ = action_executor(opts, pro_post_actions, [], templ, post=False)() if not ret: return False LOG.dbg('install done: installed \"{}\"'.format(','.join(installed))) if opts.install_temporary: LOG.log('\ninstalled to tmp \"{}\".'.format(tmpdir)) LOG.log('\n{} dotfile(s) installed.'.format(len(installed))) return True