-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·68 lines (49 loc) · 1.97 KB
/
install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
import os
import sys
if sys.version_info[0] < 3:
from urllib import urlretrieve
else:
from urllib.request import urlretrieve
TGET_URL = "https://raw.githubusercontent.com/oakhan3/tget/master/tg-py"
INSTALL_PATH = "$HOME/.local/bin"
EXPANDED_INSTALL_PATH = os.path.expandvars(INSTALL_PATH)
ENV_PATHS = os.environ["PATH"].split(":")
ENV_SHELL = os.environ["SHELL"]
PATH_EXPORT = '\nPATH="{}:$PATH"\n'.format(INSTALL_PATH)
SHELL_FN = """
tg() {
tg-py "$@" | source /dev/stdin
}
"""
ADDED_TEMPLATE = "The following has been added to `{}`:\n{}"
if __name__ == "__main__":
# Download tget script
install = os.path.join(EXPANDED_INSTALL_PATH, "tg-py")
urlretrieve(TGET_URL, install)
# Change install permissions to `rwxr--r--``
os.chmod(install, 0o744)
# Automatically add the INSTALL_PATH to shell config if possible.
shell_config_path = None
home_dir = os.path.expandvars("$HOME")
shell_config_map = {"zsh": os.path.join(home_dir, ".zshrc"), "bash": os.path.join(home_dir, ".bash_profile")}
for shell in shell_config_map:
if shell in ENV_SHELL:
shell_config_path = shell_config_map[shell]
break
if shell_config_path:
shell_config = open(shell_config_path, "r").read()
with open(shell_config_path, "a") as shell_config_buffer:
if SHELL_FN not in shell_config:
shell_config_buffer.write(SHELL_FN)
print(ADDED_TEMPLATE.format(shell_config_path, SHELL_FN))
if EXPANDED_INSTALL_PATH not in ENV_PATHS:
shell_config_buffer.write(PATH_EXPORT)
print(ADDED_TEMPLATE.format(shell_config_path, PATH_EXPORT))
else:
print(
"Make sure to ADD THE FOLLOWING TO YOUR SHELL CONFIG FILE (.bashrc, .bash_profile...) "
"to enable the `tg` command:{}{}".format(PATH_EXPORT, SHELL_FN)
)
print("\nMake sure to restart your shell to enable `tg` commands!")
sys.exit()