def mklink(task_name, to_path, from_path): if path.exists(from_path): log.notice("%s: symlink: '%s' already exists." % (task_name, from_path)) return os.symlink(to_path, from_path) log.success("%s: symlink: %s --> %s" % (task_name, from_path, to_path))
def finalize(self): if not self.exists: log.warn(str(self) + ": The component was not found.") msgs = self.messages if len(msgs) > 0: log.notice(" Log messages:") for msg in msgs: log.notice(" " + msg)
def mkdirp(task_name, path): try: os.makedirs(path) log.success("%s: mkdirp: %s" % (task_name, path)) except: if os.path.exists(path) and os.path.isdir(path): log.notice("%s: mkdirp: '%s' already exists." % (task_name, path)) else: raise
def per_line_patch(task_name, out_path, added_lines=[], removed_lines=[]): content = "" try: with open(out_path) as f: content = f.read() except: if path.exists(out_path): raise modified = False lines = content.splitlines() for line in added_lines: if line not in lines: lines.append(line) log.notice("%s: patch: adding '%s'" % (task_name, line)) modified = True removed_lines = set(removed_lines) for line in lines: if line in removed_lines: log.notice("%s: patch: removing '%s'" % (task_name, line)) modified = True lines = [line for line in lines if line not in removed_lines] if not modified: log.notice("%s: already patched: %s" % (task_name, out_path)) return new_content = "\n".join(lines) with open(out_path, "w") as f: f.write(new_content) log.success("%s: patched: %s" % (task_name, out_path))
def per_line_patch(task_name, out_path, added_lines=[], removed_lines=[]): content = "" try: with open(out_path) as f: content = f.read().decode("utf8") except: if path.exists(out_path): raise modified = False lines = content.splitlines() for line in added_lines: if line not in lines: lines.append(line) log.notice("%s: patch: adding '%s'" % (task_name, line)) modified = True removed_lines = set(removed_lines) for line in lines: if line in removed_lines: log.notice("%s: patch: removing '%s'" % (task_name, line)) modified = True lines = [line for line in lines if line not in removed_lines] if not modified: log.notice("%s: already patched: %s" % (task_name, out_path)) return new_content = "\n".join(lines) with open(out_path, "w") as f: f.write(new_content) log.success("%s: patched: %s" % (task_name, out_path))
def copy_no_overwrite(task_name, orig_path, out_path): if path.exists(out_path): log.notice("%s: copy: '%s' already exists." % (task_name, out_path)) return shutil.copy(orig_path, out_path) log.success("%s: copy: %s --> %s" % (task_name, orig_path, out_path))
def git_clone(task_name, fs_path, repo): if path.exists(fs_path): log.notice("%s: git-clone: '%s' already exists." % (task_name, fs_path)) return exec_cmd(task_name, 'git clone "%s" "%s"' % (repo, fs_path))
def exec_cmd(task_name, cmd): log.notice("%s: exec: %s" % (task_name, cmd)) ret = os.system(cmd) if ret != 0: log.error("%s: command '%s' failed with error code %d." % (task_name, cmd, ret))
from optparse import OptionParser from texthon.parser import Parser import dotfiler.logger as log import dotfiler.paths as paths from dotfiler.tasks import (mkdirp, git_clone, translate, per_line_patch, copy_no_overwrite, concatenate) base_path = paths.dotfiles # is the location of `.dotfiles` valid? home_dir = os.getenv('HOME') expected_path = path.realpath(path.join(home_dir, ".dotfiles")) actual_path = path.realpath(base_path) if expected_path != actual_path: log.fatal("The location of '.dotfiles' is invalid.") log.notice(" Expected: " + expected_path) log.notice(" Got: " + actual_path) sys.exit() os.chdir(base_path) paths.dotfiles = base_path # Parse options parser = OptionParser() parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="be verbose") (options, args) = parser.parse_args() if options.verbose: log.notice_enabled = True
from optparse import OptionParser from texthon.parser import Parser import dotfiler.logger as log import dotfiler.paths as paths from dotfiler.tasks import (mkdirp, git_clone, translate, per_line_patch, copy_no_overwrite, concatenate) base_path = paths.dotfiles # is the location of `.dotfiles` valid? home_dir = os.getenv('HOME') expected_path = path.realpath(path.join(home_dir, ".dotfiles")) actual_path = path.realpath(base_path) if expected_path != actual_path: log.fatal("The location of '.dotfiles' is invalid.") log.notice(" Expected: " + expected_path) log.notice(" Got: " + actual_path) sys.exit() os.chdir(base_path) paths.dotfiles = base_path # Parse options parser = OptionParser() parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="be verbose") (options, args) = parser.parse_args()