apply_DAMASK_modifications script needs to be independent from damask installation (except solver variables)

This commit is contained in:
Daniel Otto de Mentock 2022-06-08 15:50:35 +02:00
parent 5a284648ed
commit ab2072bf2f
1 changed files with 40 additions and 8 deletions

View File

@ -1,20 +1,51 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import sys import sys
import glob import glob
import argparse import argparse
import shutil import shutil
from pathlib import Path from pathlib import Path
import importlib.machinery
import signal
import subprocess
import shlex
from functools import partial
solver = importlib.machinery.SourceFileLoader(
'damask',f'{str(Path(__file__).parent.absolute().parents[1])}/python/damask/solver/_marc.py').load_module()
def run(cmd, wd='./', env=None, timeout=None):
def pass_signal(sig,_,proc,default):
proc.send_signal(sig)
signal.signal(sig,default)
signal.raise_signal(sig)
signals = [signal.SIGINT,signal.SIGTERM]
print(f"running '{cmd}' in '{wd}'")
process = subprocess.Popen(shlex.split(cmd),
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
env = os.environ if env is None else env,
cwd = wd,
encoding = 'utf-8')
sig_states = [signal.signal(sig,partial(pass_signal,proc=process,default=signal.getsignal(sig))) for sig in signals]
try:
stdout,stderr = process.communicate(timeout=timeout)
finally:
for sig,state in zip(signals,sig_states):
signal.signal(sig,state)
if process.returncode != 0:
print(stdout)
print(stderr)
raise RuntimeError(f"'{cmd}' failed with returncode {process.returncode}")
return stdout, stderr
import damask
def copy_and_patch(patch,orig,editor): def copy_and_patch(patch,orig,editor):
try: try:
shutil.copyfile(orig,orig.parent/patch.stem) shutil.copyfile(orig,orig.parent/patch.stem)
except shutil.SameFileError: except shutil.SameFileError:
pass pass
damask.util.run(f'patch {orig.parent/patch.stem} {patch} --backup --forward') run(f'patch {orig.parent/patch.stem} {patch} --backup --forward')
with open(orig.parent/patch.stem) as f_in: with open(orig.parent/patch.stem) as f_in:
content = f_in.read() content = f_in.read()
with open(orig.parent/patch.stem,'w') as f_out: with open(orig.parent/patch.stem,'w') as f_out:
@ -28,15 +59,16 @@ parser = argparse.ArgumentParser(
parser.add_argument('--editor', dest='editor', metavar='string', default='vi', parser.add_argument('--editor', dest='editor', metavar='string', default='vi',
help='Name of the editor (executable) used by Marc Mentat') help='Name of the editor (executable) used by Marc Mentat')
parser.add_argument('--marc-root', dest='marc_root', metavar='string', parser.add_argument('--marc-root', dest='marc_root', metavar='string',
default=damask.solver._marc._marc_root, default=solver._marc_root,
help='Marc root directory') help='Marc root directory')
parser.add_argument('--marc-version', dest='marc_version', metavar='string', parser.add_argument('--marc-version', dest='marc_version', metavar='string',
default=damask.solver._marc._marc_version, default=solver._marc_version,
help='Marc version') help='Marc version')
parser.add_argument('--damask-root', dest='damask_root', metavar = 'string', parser.add_argument('--damask-root', dest='damask_root', metavar = 'string',
default=damask.solver._marc._damask_root, default=solver._damask_root,
help='DAMASK root directory') help='DAMASK root directory')
args = parser.parse_args() args = parser.parse_args()
marc_root = Path(args.marc_root).expanduser() marc_root = Path(args.marc_root).expanduser()
damask_root = Path(args.damask_root).expanduser() damask_root = Path(args.damask_root).expanduser()
@ -52,7 +84,7 @@ matches = {'Marc_tools': [['comp_user','comp_damask_*mp'],
for cmd in ['patch','xvfb-run']: for cmd in ['patch','xvfb-run']:
try: try:
damask.util.run(f'{cmd} --help') run(f'{cmd} --help')
except FileNotFoundError: except FileNotFoundError:
print(f'"{cmd}" not found, please install') print(f'"{cmd}" not found, please install')
sys.exit() sys.exit()
@ -71,7 +103,7 @@ print('compiling Mentat menu binaries...')
executable = marc_root/f'mentat{marc_version}/bin/mentat' executable = marc_root/f'mentat{marc_version}/bin/mentat'
menu_file = marc_root/f'mentat{marc_version}/menus/linux64/main.msb' menu_file = marc_root/f'mentat{marc_version}/menus/linux64/main.msb'
damask.util.run(f'xvfb-run -a {executable} -compile {menu_file}') run(f'xvfb-run -a {executable} -compile {menu_file}')
print('setting file access rights...') print('setting file access rights...')