def proc(files=None): HOME = os.environ['HOME'] TESTHOME = os.path.join(HOME, 'tmp/testhome') HOME = TESTHOME # comment out to operate on real directory DOTMATRIX = os.path.join(HOME, 'repos/dotmatrix/cfg') if not files: files = {'vimrc': ('.vimrc', HOME), 'zshrc': ('.zshrc', HOME), 'inputrc': ('.inputrc', HOME), 'zpreztorc': ('.zpreztorc', HOME), 'nvimrc': ('.nvimrc', HOME)} def _ask(fname): while True: ans = input("{} already exists. Overwrite? [y/n]".format(_s(fname))) if ans in 'nN': return False elif ans in 'yY': return True else: print("Only 'y' or 'n' allowed.") def _s(string, strip=HOME): return '~~/' + string.lstrip(strip) def _move(**kwargs): for key, obj in kwargs.items(): source = os.path.join(obj[1], obj[0]) dest = os.path.join(DOTMATRIX, key) if not os.path.islink(source): if os.path.isfile(dest): if _ask(dest): os.rename(source, dest) print("Existing file {} has been REPLACED.".format(_s(dest))) else: print("Existing file {} has been KEPT.".format(_s(dest))) else: os.rename(source, dest) print("{} has been moved and linked.".format(_s(source))) print("Move operation completed.\n") def _link(**kwargs): for key, obj in kwargs.items(): source = os.path.join(obj[1], obj[0]) dest = os.path.join(DOTMATRIX, key) try: os.symlink(dest, source) print("Linked: {} -> {}".format(_s(source), _s(dest))) except OSError as e: if e.errno == 17: print("{} already exists. No link created".format(_s(source))) # CMDLIST: with cd(HOME): _move(**files) _link(**files)
def repos(repo_dict, repo_dir): """ Dive into every repo, pull the latest commit and execute install command. """ for path, command in repo_dict.items(): fullpath = os.path.join(repo_dir, path) try: with cd(fullpath): subprocess.call(gpull) subprocess.call(command) except OSError as e: if e.errno == errno.ENOENT: print(path, ' does not exist. Skipping.')
def tabbing(fname, content, path='/etc/', mode='+a', user=True): with cd(path), open(fname, mode) as f: for lines_to_add in content: f.seek(0) lines_to_add += '\n' if user: lines_to_add = lines_to_add.format(user=realuser) if lines_to_add in f: print('---\tLine already in file. Not writing:\n', lines_to_add) else: f.write(lines_to_add) print('+++\tNew line written:\n', lines_to_add)