-
Notifications
You must be signed in to change notification settings - Fork 1
/
newproject.py
executable file
·192 lines (143 loc) · 5.66 KB
/
newproject.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
import os
import pwd
import shutil
import sys
# Configuration section:
#
# environment:
# $SRCROOT is the basedir to use for all source. Projects go in
# $SRCROOT/$projname by default.
# $PACKAGE_ROOT is the base package (e.g., 'com.example') under which
# your project lives.
# Where are we in the file system.
basedir = "."
templatedir = "."
def env(var, default):
""" Return environment variable '$var', or 'default' if unset. """
val = os.getenv(var)
if None == val:
return default
return val
def input_string(prompt, default=""):
""" Gets a string value from the user after offering
the specified prompt. The default value is used if
the user enters an empty string.
"""
print prompt
user_response = raw_input("[" + str(default) + "]> ")
if user_response == '':
return default
else:
return user_response
TYPE_JAVA = 1
TYPE_MAPREDUCE = 2
TYPE_MAPRED = 3
TYPE_MINIMUM = TYPE_JAVA
TYPE_MAXIMUM = TYPE_MAPRED
def get_project_type():
""" Return the type of the project
1 - java
2 - hadoop mapreduce (new API)
3 - hadoop mapred (old API)
"""
print "Project type:"
print " [*] 1 - Java"
print " 2 - Hadoop MapReduce (new API)"
print " 3 - Hadoop mapred (old API)"
typstr = input_string("What type of project are you creating", 1)
v = int(typstr)
if v < TYPE_MINIMUM or v > TYPE_MAXIMUM:
return get_project_type()
return v
def get_project_name():
""" Return a project name """
projname = input_string("What is the project name?", "meep")
if "" == projname:
print "Please enter a project name."
return get_project_name()
return projname
def get_project_dir(name):
""" Return the directory to use for the project """
srcroot = env("SRCROOT", os.path.join(os.path.expanduser("~"), "src"))
defdir = os.path.join(srcroot, name)
return input_string("Enter the project root directory:", defdir)
def copy_base_files(projname, projdir):
""" Copy all files from common dir into the new project """
global templatedir
common_files = os.path.join(templatedir, "common")
shutil.copytree(common_files, projdir)
def copy_src_files(srcnature, main_package, main_package_dir, proj_main_name,
projdir, projname):
""" Copy files of a specific source type into the appropriate directories
in the project.
"""
global templatedir
srcdir = os.path.join(templatedir, srcnature + "-src")
dest_java_dir = os.path.join(projdir, "src", "java", main_package_dir)
if not os.path.exists(dest_java_dir):
os.makedirs(dest_java_dir)
shutil.copy(os.path.join(srcdir, "mainclass.java"), \
os.path.join(dest_java_dir, proj_main_name + ".java"))
if os.path.exists(os.path.join(srcdir, "mapper.java")):
shutil.copy(os.path.join(srcdir, "mapper.java"), \
os.path.join(dest_java_dir, proj_main_name + "Mapper.java"))
if os.path.exists(os.path.join(srcdir, "reducer.java")):
shutil.copy(os.path.join(srcdir, "reducer.java"), \
os.path.join(dest_java_dir, proj_main_name + "Reducer.java"))
if os.path.exists(os.path.join(srcdir, "maintest.java")):
dest_test_dir = os.path.join(projdir, "src", "test", main_package_dir)
if not os.path.exists(dest_test_dir):
os.makedirs(dest_test_dir)
shutil.copy(os.path.join(srcdir, "maintest.java"), \
os.path.join(dest_test_dir, "Test" + proj_main_name + ".java"))
if os.path.exists(os.path.join(srcdir, "mainbin")):
dest_bin_dir = os.path.join(projdir, "bin")
if not os.path.exists(dest_bin_dir):
os.makedirs(dest_bin_dir)
shutil.copy(os.path.join(srcdir, "mainbin"), \
os.path.join(dest_bin_dir, projname))
def substitute_vars(projdir, varname, value):
""" Replace %varname% in all files in projdir with value. """
cmd = "find \"" + projdir + "\" -type f -exec sed -i -e 's/%" + varname \
+ "%/" + value + "/g' {} \\;"
os.system(cmd)
def main(argv):
global basedir, templatedir
# Where does this script live in the filesystem.
basedir = os.path.dirname(os.path.realpath(argv[0]))
templatedir = os.path.join(basedir, "template")
projname = get_project_name()
projdir = get_project_dir(projname)
projtype = get_project_type()
default_class_name = projname[0].upper() + projname[1:]
proj_main_name = input_string("Main class name", default_class_name)
github_username = input_string("github username", pwd.getpwuid(os.getuid())[0])
# TODO: Make this parameterized input
license = "Apache 2.0"
# TODO: Make this parameterized input
copyright = "(c) Copyright 2010."
package_root = env("PACKAGE_ROOT", "org." + projname)
main_package = input_string("Main package for the project", \
package_root + "." + projname)
main_package_dir = main_package.replace(".", "/")
# TODO: Determine the actual dependencies here. Allow config of ivy.xml.
copy_base_files(projname, projdir)
copy_src_files("java", main_package, main_package_dir, \
proj_main_name, projdir, projname)
if projtype == TYPE_MAPREDUCE:
copy_src_files("mapreduce", main_package, main_package_dir, \
proj_main_name, projdir, projname)
elif projtype == TYPE_MAPRED:
copy_src_files("mapred", main_package, main_package_dir, \
proj_main_name, projdir, projname)
substitute_vars(projdir, "github.user", github_username)
substitute_vars(projdir, "proj.name", projname)
substitute_vars(projdir, "ProjectName", proj_main_name)
substitute_vars(projdir, "PROJNAME", projname.upper())
substitute_vars(projdir, "license", license)
substitute_vars(projdir, "copyright", copyright)
substitute_vars(projdir, "main.package", main_package)
print "Created new project in " + projdir
if __name__ == "__main__":
sys.exit(main(sys.argv))