Skip to content

Commit ec379ab

Browse files
committed
More protections for editing files.
Now check if the lines to be added are already present before adding them. In Makefile and main.cpp, just check if the first line to be added is present and then assume the rest are the same. Warnings are issued if it is found and the rest are being skipped.
1 parent 7d73a90 commit ec379ab

File tree

1 file changed

+95
-70
lines changed

1 file changed

+95
-70
lines changed

helpers_for_get_and_add.py

Lines changed: 95 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -140,101 +140,126 @@ def update_physicell_files(DIR, project_loaded=False):
140140
if project_loaded:
141141
makefile = f'{DIR}/Makefile'
142142
makefile_temp = append_suffix(f'{DIR}/Makefile-TEMP')
143+
add_to_makefile = True
143144
with open(makefile, 'r') as f:
144145
lines = f.readlines()
145146
for line_number, line in enumerate(lines):
146-
if "PhysiCell_custom_module_OBJECTS :=" in line:
147-
lines.insert(line_number,"PhysiPKPD_OBJECTS := PhysiPKPD_PK.o PhysiPKPD_PD.o\n\n")
147+
if "PhysiPKPD_OBJECTS := PhysiPKPD_PK.o PhysiPKPD_PD.o" in line:
148+
print(f"WARNING: Found PhysiPKPD_OBJECTS already defined in {makefile}. Skipping the rest of Makefile additions.")
149+
print(f"\tIf compilation errors occur, make sure PhysiPKPD_PK.o and PhysiPKPD_PD.o are defined")
150+
print(f"\tAnd make sure that $(PhysiPKPD_OBJECTS) is added to PhysiCell_OBJECTS.")
151+
add_to_makefile = False
148152
break
153+
if add_to_makefile:
154+
for line_number, line in enumerate(lines):
155+
if "PhysiCell_custom_module_OBJECTS :=" in line:
156+
lines.insert(line_number,"PhysiPKPD_OBJECTS := PhysiPKPD_PK.o PhysiPKPD_PD.o\n\n")
157+
break
158+
159+
if add_to_makefile:
160+
source_file = f'{DIR}/addons/PhysiPKPD/Makefile-PhysiPKPD-Objects.txt'
161+
with open(source_file, 'r') as sf:
162+
new_lines = sf.readlines()
149163

150-
source_file = f'{DIR}/addons/PhysiPKPD/Makefile-PhysiPKPD-Objects.txt'
151-
with open(source_file, 'r') as sf:
152-
new_lines = sf.readlines()
153-
154-
for line_number, line in enumerate(lines):
155-
if "custom_modules/custom.cpp" in line:
156-
break
157-
158-
with open(makefile_temp,'w') as f:
159-
for i in range(line_number):
160-
print(lines[i].rstrip('\n'), file=f)
161-
for new_line in new_lines:
162-
print(new_line.rstrip('\n'), file=f)
163-
print("\n", file=f)
164-
for i in range(i+1,len(lines)):
165-
print(lines[i].rstrip('\n'), file=f)
166-
167-
with open(makefile_temp,'r+') as f:
168-
lines = f.readlines()
164+
for line_number, line in enumerate(lines):
165+
if "custom_modules/custom.cpp" in line:
166+
break
169167

170-
for i, line in enumerate(lines):
171-
if "PhysiCell_OBJECTS :=" in line:
172-
line = line.rstrip('\n') + ' $(PhysiPKPD_OBJECTS)\n'
173-
break
174-
lines[i] = line
168+
with open(makefile_temp,'w') as f:
169+
for i in range(line_number):
170+
print(lines[i].rstrip('\n'), file=f)
171+
for new_line in new_lines:
172+
print(new_line.rstrip('\n'), file=f)
173+
print("\n", file=f)
174+
for i in range(i+1,len(lines)):
175+
print(lines[i].rstrip('\n'), file=f)
176+
177+
with open(makefile_temp,'r+') as f:
178+
lines = f.readlines()
179+
180+
for i, line in enumerate(lines):
181+
if "PhysiCell_OBJECTS :=" in line:
182+
line = line.rstrip('\n') + ' $(PhysiPKPD_OBJECTS)\n'
183+
break
184+
lines[i] = line
175185

176-
with open(makefile_temp,'w') as f:
177-
f.writelines(lines)
186+
with open(makefile_temp,'w') as f:
187+
f.writelines(lines)
178188

179-
print(f"{makefile_temp} is ready for PhysiPKPD. Editing other files first before overwriting {makefile}")
189+
print(f"{makefile_temp} is ready for PhysiPKPD. Editing other files first before overwriting {makefile}")
180190

181-
def get_line_number(s, lines):
191+
def get_line_number(s, lines, print_missing_message=True):
182192
for line_number, line in enumerate(lines):
183193
if s in line:
184194
return line_number
185-
print(f"{f.name} does not have a line containing {s}")
195+
if print_missing_message:
196+
print(f"{f.name} does not have a line containing {s}")
186197
return None
187198

199+
add_to_main = True
188200
main_temp = append_suffix(f'{DIR}/main','.cpp')
189201
with open(main_file, 'r+') as f:
190202
lines = f.readlines()
191-
line_number = get_line_number("setup_tissue", lines)
192-
if line_number is not None:
193-
lines.insert(line_number+1, "\n\tsetup_pharmacodynamics();\n") # new_string should end in a newline
194-
with open(main_temp,'w') as f_temp:
195-
f_temp.writelines(lines) # No need to truncate as we are increasing filesize
203+
if get_line_number("setup_pharmacodynamics", lines, print_missing_message=False) is not None:
204+
print(f"WARNING: Found setup_pharmacodynamics already defined in {main_file}. Skipping the rest of main.cpp additions.")
205+
print(f"\tIf errors occur, make sure PK_model(PhysiCell_globals.current_time); and PD_model(PhysiCell_globals.current_time); are on either side of microenvironment.simulate_diffusion_decay.")
206+
add_to_main = False
196207
else:
197-
print(f"{DIR}/main.cpp does not include `setup_tissue();`???")
198-
exit()
208+
line_number = get_line_number("setup_tissue", lines)
209+
if line_number is not None:
210+
lines.insert(line_number+1, "\n\tsetup_pharmacodynamics();\n") # new_string should end in a newline
211+
with open(main_temp,'w') as f_temp:
212+
f_temp.writelines(lines) # No need to truncate as we are increasing filesize
213+
else:
214+
print(f"{DIR}/main.cpp does not include `setup_tissue();`???")
215+
exit()
199216

200-
with open(main_temp, 'r+') as f:
201-
lines = f.readlines()
202-
line_number = get_line_number("microenvironment.simulate_diffusion_decay", lines)
203-
if line_number is not None:
204-
lines.insert(line_number+1, "\n\t\t\tPD_model( PhysiCell_globals.current_time );\n") # new_string should end in a newline
205-
lines.insert(line_number, "\t\t\tPK_model( PhysiCell_globals.current_time );\n\n") # new_string should end in a newline
206-
f.seek(0) # readlines consumes the iterator, so we need to start over
207-
f.writelines(lines) # No need to truncate as we are increasing filesize
208-
else:
209-
print(f"{DIR}/main.cpp does not include `microenvironment.simulate_diffusion_decay(diffusion_dt);`???")
210-
exit()
211-
212-
print(f"{main_temp} is ready for PhysiPKPD. Editing other files first before overwriting {main_file}")
217+
if add_to_main:
218+
with open(main_temp, 'r+') as f:
219+
lines = f.readlines()
220+
line_number = get_line_number("microenvironment.simulate_diffusion_decay", lines)
221+
if line_number is not None:
222+
lines.insert(line_number+1, "\n\t\t\tPD_model( PhysiCell_globals.current_time );\n") # new_string should end in a newline
223+
lines.insert(line_number, "\t\t\tPK_model( PhysiCell_globals.current_time );\n\n") # new_string should end in a newline
224+
f.seek(0) # readlines consumes the iterator, so we need to start over
225+
f.writelines(lines) # No need to truncate as we are increasing filesize
226+
else:
227+
print(f"{DIR}/main.cpp does not include `microenvironment.simulate_diffusion_decay(diffusion_dt);`???")
228+
exit()
229+
230+
print(f"{main_temp} is ready for PhysiPKPD. Editing other files first before overwriting {main_file}")
213231

214232
with open(f"{DIR}/custom_modules/custom.h", "r+") as f:
215233
lines = f.readlines()
216-
line_number = get_line_number("#include", lines)
217-
if line_number is not None:
218-
lines.insert(line_number,'#include "../addons/PhysiPKPD/src/PhysiPKPD.h"\n')
219-
f.seek(0)
220-
f.writelines(lines)
234+
if get_line_number("addons/PhysiPKPD/src/PhysiPKPD.h", lines, print_missing_message=False):
235+
print("Found addons/PhysiPKPD/src/PhysiPKPD.h in {DIR}/custom_modules/custom.h. Not going to add it again here.")
221236
else:
222-
print(f"{DIR}/custom_modules/custom.h does not have any `#include` statements???")
223-
exit()
224-
225-
with open(makefile, 'w') as f:
226-
with open(makefile_temp, 'r') as f_temp:
227-
lines = f_temp.readlines()
228-
f.writelines(lines)
229-
230-
with open(main_file, 'w') as f:
231-
with open(main_temp, 'r') as f_temp:
232-
lines = f_temp.readlines()
233-
f.writelines(lines)
237+
line_number = get_line_number("#include", lines)
238+
if line_number is not None:
239+
lines.insert(line_number,'#include "../addons/PhysiPKPD/src/PhysiPKPD.h"\n')
240+
f.seek(0)
241+
f.writelines(lines)
242+
else:
243+
print(f"{DIR}/custom_modules/custom.h does not have any `#include` statements???")
244+
exit()
245+
246+
if add_to_makefile:
247+
with open(makefile, 'w') as f:
248+
with open(makefile_temp, 'r') as f_temp:
249+
lines = f_temp.readlines()
250+
f.writelines(lines)
251+
252+
if add_to_main:
253+
with open(main_file, 'w') as f:
254+
with open(main_temp, 'r') as f_temp:
255+
lines = f_temp.readlines()
256+
f.writelines(lines)
234257

235258
print(f"All files (Makefile, main.cpp, and custom_modules/custom.h) updated and overwritten now. Removing temporary files...")
236-
os.remove(makefile_temp)
237-
os.remove(main_temp)
259+
if os.path.exists(makefile):
260+
os.remove(makefile_temp)
261+
if os.path.exists(main_temp):
262+
os.remove(main_temp)
238263
else:
239264
with open(f'{DIR}/addons/PhysiPKPD/Makefile-PhysiPKPD-Samples.txt', "r") as f:
240265
add_samples_to_makefile(f, f"{DIR}/Makefile")

0 commit comments

Comments
 (0)