def install(self): """ install configuration to ~/.lki/ Examples: lki install """ check_executable("git") repo_path = os.path.join(HOME, ".lki") if not os.path.exists(repo_path): run("git clone -o o --recursive https://github.com/LKI/LKI.git {}".format(repo_path)) else: run("git -C {} pull --rebase".format(repo_path)) def _link(src, dst=None, **kwargs): target = os.path.join(HOME, dst or src) if not os.path.exists(target): os.symlink(os.path.join(repo_path, src), target, **kwargs) try: _link(".gitconfig") _link(".gitignore") _link(".ideavimrc") _link(".profile") _link(".tmux.conf") _link("dotvim/vimrc", ".vimrc") if IS_WIN32: _link("dotvim/vimrc", "_vimrc") _link("dotvim", "vimfiles", target_is_directory=True) _link("dotvim", ".vim", target_is_directory=True) except OSError as ex: print("OSError: {}".format(ex)) print("Please check your permissions.") print(" Hint: windows requires admin permission when creating symlink.")
def clone(self, url: str): """ clone a git repository to workspace Examples: lki clone [email protected]:zaihui/hutils.git Equals to: git clone -o o [email protected]:zaihui/hutils.git {workspace}/github/zaihui-hutils """ check_executable("git") if not url.startswith("git@") and not url.startswith("http"): if url.count("/") == 1: url = "[email protected]:{}.git".format(url) else: url = "https://{}".format(url) match = next((m for m in (e.search(url) for e in REGEX_GIT_URLS) if m), None) if not match: raise LKIError("lki can not understand git url: {}".format(url)) domain, project, _ = match.groups() # type: str slug_domain = domain.split(".", 1)[0] slug_project = project.replace("/", "-") workspace = self._config.get("workspace", ".") path = os.path.join(workspace, slug_domain, slug_project) run("git clone -o o {} {}".format(url, path)) for key, value in self._config.get(domain, {}).items(): run("git -C {} config {} {}".format(path, key, value))
def clone(self, url): """clone a git repository to workspace Examples: lki clone [email protected]:zaihui/hutils.git Equals to: git clone -o o [email protected]:zaihui/hutils.git {workspace}/github.com/zaihui/hutils """ check_executable("git") if not url.startswith("git@") and not url.startswith("http"): if url.count("/") == 1: if os.path.exists("{}/.ssh/id_rsa".format(HOME)): url = "[email protected]:{}.git".format(url) else: url = "https://github.com/{}.git".format(url) else: url = "https://{}".format(url) match = next((m for m in (e.search(url) for e in REGEX_GIT_URLS) if m), None) if not match: raise LKIError("lki can not understand git url: {}".format(url)) domain, project, _ = match.groups() # type: str workspace = self._config.get("workspace", os.path.join(HOME, "code/src")) path = os.path.join(workspace, domain, project) run("git clone -o o {} {}".format(url, path)) for key, value in self._config.get(domain, {}).items(): run("git -C {} config {} {}".format(path, key, value))
def boost_apt(self): """ boost apt speed by changing apt's source translate from https://raw.githubusercontent.com/ldsink/toolbox/master/ubuntu-set-aliyun-mirror.sh """ check_executable("apt") check_file("/etc/apt/sources.list") backup_file = datetime.datetime.now().strftime("/etc/apt/sources.list.%Y%m%d%H%M%S.bak") print("Backing up at {}".format(backup_file)) shutil.copyfile("/etc/apt/sources.list", backup_file) run( r'sed -i -E "s/deb (ht|f)tp(s?)\:\/\/[0-9a-zA-Z]' r"([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)" r"([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?\/ubuntu/deb" r' http\:\/\/mirrors\.aliyun\.com\/ubuntu/g" /etc/apt/sources.list' )
def install(self): """install configuration to ~/.lki/ Examples: lki install """ check_executable("git") repo_path = os.path.join(HOME, ".lki") if not os.path.exists(repo_path): run("git clone -o o --recursive https://github.com/LKI/LKI.git {}". format(repo_path)) else: run("git -C {} pull --rebase".format(repo_path)) def _link(src, dst=None): target = os.path.expanduser(os.path.join(HOME, dst or src)) if os.path.exists(target): os.remove(target) link(os.path.join(repo_path, src), target, force=True) try: _link(".gitconfig") _link(".gitignore") _link(".ideavimrc") _link(".inputrc") _link(".profile") _link(".tmux.conf") _link("starship.toml", ".config/starship.toml") # TODO: implement `lki install --vim` # _link("dotvim/vimrc", ".vimrc") # # if IS_WIN32: # _link("dotvim/vimrc", "_vimrc") # _link("dotvim", "vimfiles", target_is_directory=True) # _link("dotvim", ".vim", target_is_directory=True) except OSError as ex: sys.stderr.writelines([ "OSError: {}".format(ex), " Please check your permissions.", "Hint: windows requires admin permission when creating symlink.", ]) sys.exit(1)
def env(self, key=None, value=None): """list/grep/set environment variable (windows only)""" if not is_windows: raise LKIError("lki env only support windows") mute_keys = ("PS1", "_") data = { k.lower(): v for k, v in os.environ.items() if k not in mute_keys } if key is None: for k, v in data.items(): print(k, "=", v) elif value is None: if key.lower() in data: print(data[key.lower()]) else: for k, v in data.items(): if key.lower() in k: print(k, "=", v[:100]) else: if not value: value = "''" print("Setting environment {} to {}".format(key, value)) run("SETX {} {}".format(key, value))
def update_apt(self): """ perform apt update """ check_executable("apt") run("apt update", "apt upgrade --auto-remove -y")