Created
October 24, 2019 14:25
-
-
Save yobayob/91ab9cc8ef1b02261ac0dc0dc48bdb28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import re | |
import os | |
import sys | |
from tempfile import _get_candidate_names | |
ENV_NAME = "SHULER_PATH" | |
REGEX = re.compile(r"export\s{}\=(\S+)".format(ENV_NAME)) | |
HOME = os.getenv("HOME") | |
def set_shuler_dir(path: str): | |
if not re.match(r"\S+", path): | |
raise Exception("{} is invalid path", path) | |
bashrc = os.path.join(HOME, ".bashrc") | |
if not os.path.exists(bashrc): | |
raise Exception("{} is not exists", bashrc) | |
with open(bashrc, "r") as f: | |
content = f.read() | |
search = re.search(REGEX, content) | |
old = None | |
if search: | |
old, = search.groups() | |
if old == path: | |
return | |
content = re.sub(REGEX, "", content) | |
if old: | |
content = re.sub(r"export\sPATH\=\$PATH\:{}".format(re.escape(old)), "", content).strip() | |
if old: | |
os.rename(old, path) | |
if not os.path.exists(path): | |
os.makedirs(path) | |
with open(bashrc, "w") as f: | |
f.write(content) | |
f.write("\n\nexport {}={}\n".format(ENV_NAME, path)) | |
f.write("\nexport PATH=$PATH:{}\n".format(path)) | |
# reload source | |
print("Please, activate shuler `source ~/.bashrc` or restart session") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment