-
Notifications
You must be signed in to change notification settings - Fork 6
/
configureToolbox.py
executable file
·72 lines (54 loc) · 2.03 KB
/
configureToolbox.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
64
65
66
67
68
69
70
71
72
#!/bin/python3
#
# configureToolbox.py
#
# D. Clarke
#
# Set up your AnalysisToolbox. Depending on your preferences, this either
#
# 1. Adds a line to your .bashrc
# 2. Sets up a venv for you with the name AnalysisToolbox
#
# ----------------------------------------------------------------------- USER PREFERENCES
SHELL = "BASH" # POWERSHELL to be added later
STRATEGY = "BASIC" # Options are "VENV" and "BASIC"
# ------------------------------------------------------------------------------ MAIN CODE
import os
from subprocess import run, PIPE
def shell(*args):
""" Carry out the passed arguments args in the shell. Can be passed as a single
string or as a list. Captures and returns output of shell command. E.g.
shell('ls -lah')
"""
args = [str(s) for s in args]
process = run(' '.join(args),shell=True,check=True,stdout=PIPE,universal_newlines=True)
return process.stdout
TOOLBOXDIR = os.getcwd()
BASHRCFILE = os.path.expanduser("~")+"/.bashrc"
ALLOWED_STRATEGIES = ["VENV","BASIC"]
ALLOWED_SHELLS = ["BASH"]
if not SHELL in ALLOWED_SHELLS:
print("Unrecognized shell",SHELL)
exit(-1)
if not STRATEGY in ALLOWED_STRATEGIES:
print("Unrecognized strategy",STRATEGY)
exit(-1)
if STRATEGY=="VENV":
shell('mkdir -p venv; cd venv; python3 -m venv AnalysisToolbox')
found = False
# Read the file and check if it contains a line starting with "export PYTHONPATH="
with open(BASHRCFILE, 'r') as file:
lines = file.readlines()
for i, line in enumerate(lines):
if line.startswith("export PYTHONPATH="):
found = True
print("Appending AnalysisToolbox to PYTHONPATH.")
lines[i] = line.strip()[:-1] + ':'+TOOLBOXDIR+'"\n'
break
# If the line is not found, create a new line
if not found:
print("Adding PYTHONPATH line for the AnalysisToolbox.")
lines.append('export PYTHONPATH="${PYTHONPATH}:'+TOOLBOXDIR+'"\n')
# Write the updated content back to the file
with open(BASHRCFILE, 'w') as file:
file.writelines(lines)