-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_kc.py
More file actions
84 lines (73 loc) · 2.8 KB
/
new_kc.py
File metadata and controls
84 lines (73 loc) · 2.8 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
77
78
79
80
81
82
83
84
import os
import argparse
import shutil
import sys
import subprocess
class chdir:
def __init__(self, path):
self.newpath=os.path.expanduser(path)
def __enter__(self):
self.oldpath=os.getcwd()
os.chdir(self.newpath)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
os.chdir(self.oldpath)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--p", help="path to project's parent directory (error if already exists)", type=str)
parser.add_argument("--c", help="name of the competition/project", type=str)
return parser.parse_args()
def build_root(root_name):
path = os.path.abspath(root_name)
if os.path.exists(path):
raise ValueError("directory already exists: {}".format(path))
os.makedirs(root_name)
return path
def build_src(proj_path, competition):
print("building src dirs")
main_java = os.path.join(proj_path, "src", "main", "java", competition)
Competition = competition[0].upper() + competition[1:]
os.makedirs(os.path.join(main_java))
os.makedirs(os.path.join(proj_path, "src", "main", "java", "water"))
os.makedirs(os.path.join(proj_path, "src", "test", "java"))
with open(os.path.join(main_java, "{}.java".format(Competition)), 'w') as f:
f.write("package {}\n".format(competition))
f.write("\npublic class {} {{\n".format(Competition))
f.write("}\n")
def build_lib(proj_path):
os.makedirs(os.path.join(proj_path, "lib"))
h2ojar = os.path.join(os.getenv('H2O_HOME'), "build", "h2o.jar")
path = os.path.join(proj_path, "lib", "h2o.jar")
print("Copying h2o.jar from {} to {}".format(h2ojar, path))
shutil.copyfile(h2ojar,path)
def build_dirs(proj_path):
print("building data, models, submissions, and py_utils")
init_dir(proj_path, "data", "data for competition")
init_dir(proj_path, "models", "models for competition")
init_dir(proj_path, "submissions", "submissions for competition")
init_dir(proj_path, "py_utils", "python utilities")
# file holding previous submission number, useful for automatically
# increasing submission numbers
with open( os.path.join(proj_path, "submissions", "next"), 'w') as f:
f.write("0")
def init_dir(proj_path, dirname, readme_msg):
p = os.path.join(proj_path, dirname)
os.makedirs(p)
with open(os.path.join(p, "README.md"), 'w') as f: f.write(readme_msg)
if __name__ == "__main__":
args = parse_args()
proj_parent = args.p
if os.path.exists(proj_parent):
with chdir(proj_parent) as p:
proj_path = build_root(args.c)
try:
build_src(proj_path, args.c)
build_lib(proj_path)
build_dirs(proj_path)
except Exception as e:
print e
print("Exception caught... Undoing project creation")
shutil.rmtree(proj_path)
sys.exit(1)
else:
raise ValueError("directory does not exist: {}.".format(args.p))