Skip to content

Commit 728debe

Browse files
committed
final touches on change_env
1 parent 2d5a3ad commit 728debe

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

change_env.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,13 @@
44
import os
55
import re
66
import sys
7-
import fileinput
8-
9-
if (len(sys.argv) < 2):
10-
print "You must give a new environment name"
11-
sys.exit()
12-
else:
13-
new_env=sys.argv[1]
147

158
def get_current_env():
169
# Use regex to determine what MAXIS_ENV is currently set to
1710
etc_profile=open("/etc/profile","r")
18-
regex=re.compile('MAXIS_ENV=(\w+)')
11+
regex=re.compile('MAXIS_ENV=(\w+)$')
1912
for line in etc_profile:
2013
env=regex.findall(line)
21-
#etc_profile.close()
22-
#print 'MAXIS_ENV is currently set to "%s"' % env[0]
2314
return env[0]
2415

2516
def update_file(file,current_env,new_env):
@@ -32,18 +23,45 @@ def update_file(file,current_env,new_env):
3223
# Also open a file handle on the same file for writing
3324
new_file=open(file,'w')
3425
# Search and replace
35-
changed_data=current_file.replace(current_env,new_env)
26+
full_current_env="export MAXIS_ENV=" + current_env
27+
full_new_env="export MAXIS_ENV=" + new_env
28+
changed_data=current_file.replace(full_current_env,full_new_env)
3629
# Write out the changed data to the file
3730
new_file.write(changed_data)
3831
#current_file.close()
3932
new_file.close()
4033
current_env=get_current_env()
4134
print 'MAXIS_ENV in %s has been changed to "%s"' % (file,current_env)
4235

36+
def usage():
37+
print ' ---------------------------------------------------------------------------'
38+
print ''
39+
print ' This script is used to change the environment settings to your particular '
40+
print ' needs. By default, any instances built from this AMI are named "standard".'
41+
print ' Using this script you can change it to whatever is needed or desired.'
42+
print ' '
43+
print ' Usage : ./change_env.py newEnvironmentName'
44+
print ' '
45+
print ' '
46+
print ' ---------------------------------------------------------------------------'
47+
print ' '
48+
49+
def main(new_env):
50+
current_env=get_current_env()
51+
print ""
52+
print " ---------------------------------------------------------------------------"
53+
print ""
54+
print 'Currently MAXIX_ENV is set to "%s"' % current_env
55+
print ""
56+
update_file("/etc/profile",current_env,new_env)
57+
update_file("/usr/bin/maxisenv",current_env,new_env)
58+
print ""
59+
print " ---------------------------------------------------------------------------"
60+
print ""
61+
4362

44-
current_env=get_current_env()
45-
print ""
46-
print 'Currently MAXIX_ENV is set to "%s"' % current_env
47-
update_file("/etc/profile",current_env,new_env)
48-
update_file("/usr/bin/maxisenv",current_env,new_env)
49-
print ""
63+
if (len(sys.argv) < 2):
64+
usage()
65+
else:
66+
new_env=sys.argv[1]
67+
main(new_env)

0 commit comments

Comments
 (0)