DAMASK_EICMD/install/MarcMentat/MSC_modifications.py

112 lines
4.5 KiB
Python
Raw Permalink Normal View History

2020-07-18 13:42:22 +05:30
#!/usr/bin/env python3
2022-06-09 03:52:04 +05:30
2020-07-18 13:42:22 +05:30
import os
2022-03-11 16:09:31 +05:30
import sys
2020-07-18 13:42:22 +05:30
import glob
import argparse
2021-07-14 15:45:39 +05:30
import shutil
2020-07-18 13:42:22 +05:30
from pathlib import Path
import subprocess
import shlex
2022-11-17 12:42:27 +05:30
sys.path.append(str(Path(__file__).resolve().parents[2]/'python/damask'))
2022-06-09 03:52:04 +05:30
import solver
2022-11-17 12:42:27 +05:30
APPLY = 'install'
RESTORE = 'uninstall'
2021-12-01 14:33:02 +05:30
def copy_and_patch(patch,orig,editor):
2021-07-14 15:45:39 +05:30
try:
2021-07-14 13:43:08 +05:30
shutil.copyfile(orig,orig.parent/patch.stem)
2021-07-14 15:45:39 +05:30
except shutil.SameFileError:
pass
2022-06-09 03:42:19 +05:30
subprocess.run(shlex.split(f'patch {orig.parent/patch.stem} {patch} --backup --forward'))
2021-12-01 16:16:48 +05:30
with open(orig.parent/patch.stem) as f_in:
content = f_in.read()
with open(orig.parent/patch.stem,'w') as f_out:
f_out.write(content.replace('%EDITOR%',editor))
2020-07-18 13:42:22 +05:30
parser = argparse.ArgumentParser(
2022-11-17 12:42:27 +05:30
description=f'{APPLY.capitalize()} or {RESTORE} DAMASK modifications to MSC Marc/Mentat',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
2022-11-17 12:42:27 +05:30
parser.add_argument('command', metavar='command', nargs='?', default=APPLY, choices=[APPLY,RESTORE],
help=f'Mode of operation {[APPLY,RESTORE]}')
2020-07-18 13:42:22 +05:30
parser.add_argument('--editor', dest='editor', metavar='string', default='vi',
2022-03-14 11:52:09 +05:30
help='Name of the editor (executable) used by Marc Mentat')
parser.add_argument('--marc-root', dest='marc_root', metavar='string',
2022-06-09 03:52:04 +05:30
default=solver._marc._marc_root,
help='Marc root directory')
2021-11-25 22:00:22 +05:30
parser.add_argument('--marc-version', dest='marc_version', metavar='string',
2022-06-09 03:52:04 +05:30
default=solver._marc._marc_version,
help='Marc version')
2021-04-28 12:23:40 +05:30
parser.add_argument('--damask-root', dest='damask_root', metavar = 'string',
2022-06-09 03:52:04 +05:30
default=solver._marc._damask_root,
help='DAMASK root directory')
args = parser.parse_args()
2022-11-17 12:42:27 +05:30
damask_root = Path(args.damask_root).expanduser()
marc_root = Path(args.marc_root).expanduser()
marc_version = args.marc_version
2021-07-14 15:45:39 +05:30
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']]}
2022-11-17 12:42:27 +05:30
for cmd in ['xvfb-run','patch'] if args.command == APPLY else ['xvfb-run'] if args.command == RESTORE else []:
2022-03-11 16:09:31 +05:30
try:
2022-11-17 12:42:27 +05:30
subprocess.run([cmd,'--help'], capture_output=True)
2022-03-11 16:09:31 +05:30
except FileNotFoundError:
print(f'"{cmd}" not found, please install')
sys.exit()
2022-11-17 12:42:27 +05:30
if args.command == APPLY:
print('patching files...')
for directory in glob.glob(str(damask_root/'install/MarcMentat'/marc_version/'*')):
for orig, mods in matches[Path(directory).name]:
product,subfolder = (marc_root/directory).name.split('_')
orig = marc_root/f'{product.lower()}{marc_version}/{subfolder}/{orig}'
for patch in glob.glob(f'{directory}/{mods}.patch'):
copy_and_patch(Path(patch),orig,args.editor)
elif args.command == RESTORE:
print('deleting modified files...')
for file in (glob.glob(str(marc_root/f'marc{marc_version}/tools/*_damask*')) +
glob.glob(str(marc_root/f'mentat{marc_version}/bin/kill[4-6]')) +
glob.glob(str(marc_root/f'mentat{marc_version}/bin/submit[4-6]*'))):
os.remove(file)
2021-07-14 15:45:39 +05:30
2022-11-17 12:42:27 +05:30
print('restoring original files...')
2021-07-14 15:45:39 +05:30
2022-11-17 12:42:27 +05:30
for file in (glob.glob(str(marc_root/f'marc{marc_version}/tools/include_linux64.orig')) +
glob.glob(str(marc_root/f'mentat{marc_version}/bin/edit_window.orig')) +
glob.glob(str(marc_root/f'mentat{marc_version}/menus/job_run.ms.orig'))):
shutil.copyfile(file,Path(file).with_suffix(''))
os.remove(file)
else:
print('skipping file modifications...')
2020-07-18 13:42:22 +05:30
print('compiling Mentat menu binaries...')
executable = marc_root/f'mentat{marc_version}/bin/mentat'
menu_file = marc_root/f'mentat{marc_version}/menus/linux64/main.msb'
2022-06-09 03:42:19 +05:30
subprocess.run(shlex.split(f'xvfb-run -a {executable} -compile {menu_file}'))
print('setting file access rights...')
2022-11-17 12:42:27 +05:30
for file in (
(glob.glob(str(marc_root/f'marc{marc_version}/tools/*_damask*')) +
glob.glob(str(marc_root/f'mentat{marc_version}/bin/kill[4-6]')) +
glob.glob(str(marc_root/f'mentat{marc_version}/bin/submit[4-6]')) if args.command == APPLY else []) +
glob.glob(str(marc_root/f'mentat{marc_version}/bin/edit_window'))
):
os.chmod(file , 0o755)