-
Notifications
You must be signed in to change notification settings - Fork 5
/
LSDTTParamfileUpdater.py
176 lines (141 loc) · 6.84 KB
/
LSDTTParamfileUpdater.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# This is a python script that checks LSDTopoTools parameter files
# And then renames the read and write path to be the current path
# @author Simon M Mudd
# @date 30-10-2017
from __future__ import print_function
import os
from glob import glob
import subprocess
import sys
#=============================================================================
# This helper function looks for keywords in .driver and .LSDTT_driver files
# and replaces paths
#=============================================================================
def UpdatePathInParamfile(FileName,ThisPath):
# Get the contents of the parameter file
fo = open(FileName, "r")
lines = fo.readlines()
fo.close()
new_lines = []
for line in lines:
if "read path: " in line:
this_line = line.split(": ")
path = this_line[1]
#print("Path in this file is: "+path)
new_line = "read path: "+ThisPath+"\n"
new_lines.append(new_line)
elif "write path: " in line:
this_line = line.split(": ")
path = this_line[1]
#print("Path in this file is: "+path)
new_line = "write path: "+ThisPath+"\n"
new_lines.append(new_line)
else:
new_lines.append(line)
# Now print the new file
file_for_output = open(FileName,'w')
file_for_output.writelines(new_lines)
file_for_output.close()
#=============================================================================
#=============================================================================
# This is just a welcome screen that is displayed if no arguments are provided.
#=============================================================================
def print_welcome():
print("\n\n=======================================================================")
print("Hello there, I am the going to adjust your LSDTT parameter files.")
print("You will need to tell me what to do.")
print("If you don't give me arguments I'll adjust every .driver or .param file in this directory.")
print("If you give me one argument I'll adjust every .driver or .param file in the named directory.")
print("If you give me two arguments I'll adjust a specific file in a specific directory.")
print("=======================================================================\n\n ")
#=============================================================================
#=============================================================================
# This is the main function that runs the whole thing
#=============================================================================
def main(argv):
print("The arguments are: ")
print(argv)
print("This has length " +str(len(sys.argv)))
if not argv:
print("The list is empty")
# If there are no arguments, run in this directory
if not argv:
print_welcome()
# get the current directory
this_dir = os.getcwd()
this_dir = this_dir+os.sep
print("The directory is: "+this_dir)
for file in glob( this_dir+"*.driver"):
print("Im updating"+file)
UpdatePathInParamfile(file,this_dir)
for file in glob( this_dir+"*.param"):
print("Im updating"+file)
UpdatePathInParamfile(file,this_dir)
for file in glob( this_dir+"*.LSDTT_driver"):
print("Im updating"+file)
UpdatePathInParamfile(file,this_dir)
elif len(sys.argv) == 2:
this_dir = sys.argv[1]
print("I am going to try this directory: "+this_dir)
# Add the seperator at the end.
if this_dir[-1] != os.sep:
print("You forgot the slash at the end of your directory name. Adding.")
this_dir = this_dir+os.sep
print("The directory is: "+this_dir)
if not os.access(this_dir,os.F_OK):
print("The path doesn't exist. Check your spelling. I am exiting without doing anything.")
sys.exit()
for file in glob( this_dir+"*.driver"):
print("Im updating"+file)
UpdatePathInParamfile(file,this_dir)
for file in glob( this_dir+"*.param"):
print("Im updating"+file)
UpdatePathInParamfile(file,this_dir)
for file in glob( this_dir+"*.LSDTT_driver"):
print("Im updating"+file)
UpdatePathInParamfile(file,this_dir)
elif len(sys.argv) == 3:
this_dir = sys.argv[1]
print("I am going to try this directory: "+this_dir)
# Add the seperator at the end.
if this_dir[-1] != os.sep:
print("You forgot the slash at the end of your directory name. Adding.")
this_dir = this_dir+os.sep
print("The directory is: "+this_dir)
if not os.access(this_dir,os.F_OK):
print("The path doesn't exist. Check your spelling. I am exiting without doing anything.")
sys.exit()
this_file = sys.argv[2]
full_filename = this_dir+this_file
print("Testing the file: "+this_file)
if not os.access(full_filename,os.F_OK):
print("The file doesn't exist. Check your spelling. I am exiting without doing anything.")
sys.exit()
print("Okay, I am updating the file")
print("The filename is: "+full_filename)
print("The directory is: "+this_dir)
UpdatePathInParamfile(full_filename,this_dir)
else:
this_dir = sys.argv[1]
print("I am going to try this directory: "+this_dir)
# Add the seperator at the end.
if this_dir[-1] != os.sep:
print("You forgot the slash at the end of your directory name. Adding.")
this_dir = this_dir+os.sep
print("The directory is: "+this_dir)
if not os.access(this_dir,os.F_OK):
print("The path doesn't exist. Check your spelling. I am exiting without doing anything.")
sys.exit()
this_file = sys.argv[2]
full_filename = this_dir+this_file
print("Testing the file: "+this_file)
if not os.access(full_filename,os.F_OK):
print("The file doesn't exist. Check your spelling. I am exiting without doing anything.")
sys.exit()
print("Okay, I am updating the file")
print("The filename is: "+full_filename)
print("The directory is: "+this_dir)
UpdatePathInParamfile(full_filename,this_dir)
#=============================================================================
if __name__ == "__main__":
main(sys.argv[1:])