forked from rcaloras/bashhub-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_bashhub.py
executable file
·63 lines (49 loc) · 1.89 KB
/
install_bashhub.py
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
#!/usr/bin/python
#
# Python version of install bashhub
#
import os
import sys
import shutil
from pkg_resources import resource_exists, resource_filename
def check_already_installed(home_dir):
installed = os.path.isdir(home_dir + "/.bashhub")
if installed:
raise RuntimeError("Looks like bashhub is already installed. " + \
"rm -r ~/.bashhub to install again.")
def setup_bashhub_files(home_dir):
bashhub_dir = home_dir + '.bashhub/'
# move .sh files to appropriate place.
exists = resource_exists(__name__, 'bashhub/shell')
if exists:
shell_scripts = resource_filename(__name__, 'bashhub/shell')
# Should create our bashhub directory and copy our files there.
shutil.copytree(shell_scripts, bashhub_dir)
# Add our file to our bash config if it's not present
bash_config = find_users_bash_config(home_dir)
with open(bash_config, "a+") as config:
source_hook = "source ~/.bashhub/bashhub.sh"
if source_hook not in config.read():
config.write(source_hook)
else:
raise RuntimeError("Couldn't install bashhub files in " + bashhub_dir)
def find_users_bash_config(home_dir):
bash_files = ["/.bashrc", "/.bash_profile", "/.profile"]
for file in bash_files:
if os.path.isfile(home_dir + file):
return home_dir + file
raise RuntimeError("Bummer looks, like we couldn't find a bash profile file. " + \
"Do you have a ~/.profile or ~/.bashhrc?")
def main():
try:
print "Installing needed scripts"
# Get our home directory from
home = os.environ["HOME"] + '/'
find_users_bash_config(home)
check_already_installed(home)
setup_bashhub_files(home)
except Exception, err:
sys.stderr.write('Setup Error:\n%s\n' % str(err))
sys.exit(1)
if __name__ == "__main__":
main()