Closed falkamelung closed 3 years ago
You should be able to do something like this: https://stackoverflow.com/questions/16162383/how-to-easily-write-a-multi-line-file-with-variables-python-2-6/16162599
Replace all of the job_file_lines.append("...")
with a triple quoted string formatted as you like. Then write that to the file using file.write()
.
Yes. this works. Thank you.
But it is quite a lot of work..... I may do later.
job_file_lines.append("""
# reference
cp -r """ + self.out_dir + """/reference /tmp
files="/tmp/reference/*.xml /tmp/reference/*/*.xml"
old=""" + self.out_dir + """
sed -i "s|$old|/tmp|g" $files
# geom_reference
cp -r """ + self.out_dir + """/geom_reference /tmp
files="/tmp/geom_reference/*/*.xml"
old=""" + self.out_dir + """
sed -i "s|$old|/tmp|g" $files
# secondarys
date_list=( $(awk '{printf "%s\\n",$3}' """ + batch_file + """ | awk -F _ '{printf "%s\\n",$NF}' ) )
mkdir -p /tmp/secondarys
for date in "${date_list[@]}"; do
cp -r """ + self.out_dir + """/secondarys/$date /tmp/secondarys
done
files1="/tmp/secondarys/????????/*.xml"
files2="/tmp/secondarys/????????/*/*.xml"
old=""" + self.out_dir + """
sed -i "s|$old|/tmp|g" $files1
sed -i "s|$old|/tmp|g" $files2
""")
After this we have to split according to '\n', similar to:
tmp1 = job_file_lines.split('\n')
tmp2 = []
for line in tmp:
#print(line, flush=True)
#print(line.strip(), flush=True)
tmp2.append(line.strip())
job_file_lines = tmp2
We are now writing a lot of bash code into our jobfiles. This is done in job_submission,py here:
https://github.com/geodesymiami/rsmas_insar/blob/master/minsar/job_submission.py#L712-L730
Is there any way to write in the python code exactly as it will show up when written? I mean without all this
It would be nice if we just have a line feed or newline character at the end of each line.
Here how ist shows up in the job file.