Repository navigation
Expand file tree
/
Copy pathcreate_case.py
More file actions
executable file
·76 lines (70 loc) · 3.22 KB
/
Copy pathcreate_case.py
File metadata and controls
executable file
·76 lines (70 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import argparse
import os
import sys
import shutil
import json
esmdroot = os.path.dirname(os.path.abspath(__file__))
toolroot = os.path.join(esmdroot, "tools")
sys.path.append(toolroot)
import loggers
import shell
import vc
parser = argparse.ArgumentParser(description='Create a new case with specified name')
parser.add_argument("casename", action="store", help="Name of case", type=str)
parser.add_argument("-p", "--proto", action="store", default=os.path.join(toolroot, "case.proto"),
help="Create case as a copy of prototype case")
parser.add_argument("-P", "--platform", action="store", default=None,
help="Platform for the case")
parser.add_argument("-D", "--user-cppdefs", action="store", default="",
help="User defined preprocessing variables")
parser.add_argument("-R", "--keep-root", action="store", default="",
help="Keep root the same as prototype case")
args = parser.parse_args(sys.argv[1:])
args.proto = os.path.abspath(args.proto)
casesdir = os.path.join(esmdroot, "cases")
caseroot = os.path.join(esmdroot, "cases", args.casename)
logger = loggers.get("create_case")
try:
if not os.path.exists(casesdir):
logger.info("%s does not exist, creating..." % casesdir)
logger.info("prototype case is: %s" % args.proto)
logger.info("copying prototype to case: %s" % caseroot)
shutil.copytree(args.proto, caseroot, ignore=shutil.ignore_patterns('bld', 'run', 'src', '.git'))
casegitdir = os.path.join(caseroot, "SourceMods", ".git")
protogitdir = os.path.join(args.proto, "SourceMods", ".git")
if not os.path.exists(protogitdir):
logger.info("prototype case has no SourceMods git, initializing...")
#shell.run(["git", "init", os.path.join(caseroot, "SourceMods")], shell=False)
vc.git_init(os.path.join(caseroot, "SourceMods"))
else:
logger.info("copying SourceMods git to %s" % casegitdir)
shutil.copytree(protogitdir, casegitdir, copy_function=shutil.copy)
logger.info("linking src to case")
os.symlink(os.path.join(esmdroot, "src"), os.path.join(os.path.join(caseroot, "src")), True)
logger.info("considering case.json")
caseconf = os.path.join(os.path.join(caseroot, "case.json"))
case_vars = {'PLATFORM' : 'openmpi_arch', "USER_CPPDEFS" : ""}
if os.path.exists(caseconf):
logger.info("case.json exists, loading")
case_vars.update(json.load(open(caseconf)))
if args.platform:
case_vars['PLATFORM'] = args.platform
if args.user_cppdefs:
case_vars['USER_CPPDEFS'] += args.user_cppdefs
if not args.keep_root:
case_vars['ESMDROOT'] = esmdroot
case_vars['TOOLROOT'] = toolroot
macros = os.path.join(caseroot, "Macros");
platform_macros = os.path.join(toolroot, "platforms", "Macros." + case_vars['PLATFORM'])
if not os.path.exists(macros):
logger.info("Macros does not exist, copying...")
shutil.copy2(platform_macros, macros)
else:
logger.info("Macros exists, do not overwrite...")
logger.info("writing case.json...")
output = open(caseconf, "w")
json.dump(case_vars, output, indent=2)
output.write("\n")
except Exception as e:
logger.exception(e)