better patch

This commit is contained in:
Martin Diehl 2021-07-14 12:15:39 +02:00
parent 766db0524f
commit 1d348f0ccf
1 changed files with 26 additions and 24 deletions

View File

@ -3,12 +3,18 @@
import os import os
import glob import glob
import argparse import argparse
import shutil
from pathlib import Path from pathlib import Path
import damask import damask
def copy_and_replace(in_file,dst,msc_root,editor): def copy_and_replace(patch,orig,msc_root,editor):
with open(in_file) as f_in, open(dst/Path(in_file).name,'w') as f_out: try:
shutil.copyfile(orig,orig.parent/patch.stem)
except shutil.SameFileError:
pass
damask.execute(f'patch {orig.parent/patch.stem} {patch} -b}')
with open(orig.parent/patch.stem) as f_in, open(orig.parent/patch.stem,'w') as f_out:
f_out.write(f_in.read().replace('%INSTALLDIR%',msc_root).replace('%EDITOR%',editor)) f_out.write(f_in.read().replace('%INSTALLDIR%',msc_root).replace('%EDITOR%',editor))
@ -30,31 +36,27 @@ parser.add_argument('--damask-root', dest='damask_root', metavar = 'string',
help='DAMASK root directory') help='DAMASK root directory')
args = parser.parse_args() args = parser.parse_args()
msc_root = Path(args.msc_root) msc_root = Path(args.msc_root).expanduser()
damask_root = Path(args.damask_root) damask_root = Path(args.damask_root).expanduser()
msc_version = args.msc_version msc_version = int(args.msc_version) if str(args.msc_version).split('.')[1] == '0' else args.msc_version
matches = {'Marc_tools': [['comp_user','comp_damask_*mp'],
['run_marc','run_damask_*mp'],
['include_linux64','include_linux64']],
'Mentat_bin': [['edit_window','edit_window'],
['submit1','submit?'],
['kill1','kill?']],
'Mentat_menus':[['job_run.ms','job_run.ms']]}
print('adapting Marc tools...\n') print('patching files...\n')
src = damask_root/f'install/MarcMentat/{msc_version}/Marc_tools'
dst = msc_root/f'marc{msc_version}/tools'
for in_file in glob.glob(str(src/'*damask*')) + [str(src/'include_linux64')]:
copy_and_replace(in_file,dst,args.msc_root,args.editor)
print('adapting Mentat scripts and menus...\n')
src = damask_root/f'install/MarcMentat/{msc_version}/Mentat_bin'
dst = msc_root/f'mentat{msc_version}/bin'
for in_file in glob.glob(str(src/'*[!.original]')):
copy_and_replace(in_file,dst,args.msc_root,args.editor)
src = damask_root/f'install/MarcMentat/{msc_version}/Mentat_menus'
dst = msc_root/f'mentat{msc_version}/menus'
for in_file in glob.glob(str(src/'job_run.ms')):
copy_and_replace(in_file,dst,args.msc_root,args.editor)
for directory in glob.glob(str(damask_root/f'install/MarcMentat/{msc_version}/*')):
for orig, mods in matches[Path(directory).name]:
dirs = (msc_root/Path(directory)).name.lower().split('_')
orig = msc_root/f'{dirs[0]}{msc_version}/{dirs[1]}/{orig}'
for patch in glob.glob(f'{directory}/{mods}.patch'):
copy_and_replace(Path(patch),orig,msc_root,editor)
print('compiling Mentat menu binaries...') print('compiling Mentat menu binaries...')