blob: cc68f3f593f5594f7bdbd3e0f5561efce9ff7859 [file] [log] [blame]
showard97b7f5a2009-10-14 16:17:30 +00001#!/usr/bin/python
2import sys, os, shutil, errno, optparse
3import common
4from autotest_lib.client.common_lib import error, utils
5"""
6Compile All Autotest GWT Clients Living in autotest/frontend/client/src
7"""
8
9_AUTOTEST_DIR = common.autotest_dir
10_DEFAULT_GWT_DIR = '/usr/local/lib/gwt'
11_DEFAULT_APP_DIR = os.path.join(_AUTOTEST_DIR, 'frontend/client')
12_DEFAULT_INSTALL_DIR = os.path.join(_DEFAULT_APP_DIR, 'www')
13_TMP_COMPILE_DIR = _DEFAULT_INSTALL_DIR + '.new'
14
15_COMPILE_LINE = ('java -Xmx512M '
16 '-cp "%(app_dir)s/src:%(app_dir)s/bin:%(gwt_dir)s/gwt-user.jar'
17 ':%(gwt_dir)s/gwt-dev-linux.jar" '
18 '-Djava.awt.headless=true com.google.gwt.dev.Compiler '
19 '-war "%(compile_dir)s" %(extra_args)s %(project_client)s')
20
21
22def enumerate_projects():
23 """List projects in _DEFAULT_APP_DIR."""
24 src_path = os.path.join(_DEFAULT_APP_DIR, 'src')
25 projects = {}
26 for project in os.listdir(src_path):
27 projects[project] = []
28 project_path = os.path.join(src_path, project)
29 for file in os.listdir(project_path):
30 if file.endswith('.gwt.xml'):
31 projects[project].append(file[:-8])
32 return projects
33
34
35def find_gwt_dir():
36 """See if GWT is installed in site-packages or in the system,
37 site-packages is favored over a system install.
38 """
39 site_gwt = os.path.join(_AUTOTEST_DIR, 'site-packages', 'gwt')
40
41 if os.path.isdir(site_gwt):
42 return site_gwt
43
44 if not os.path.isdir(_DEFAULT_GWT_DIR):
45 print 'Error: Unable to find GWT, is GWT installed?\n'
46 sys.exit(1)
47
48 return _DEFAULT_GWT_DIR
49
50
51def install_completed_client(compiled_dir, project_client):
52 """Remove old client directory if it exists, move installed client to the
53 old directory and move newly compield client to the installed client
54 dir.
55 @param compiled_dir: Where the new client was compiled
56 @param project_client: project.client pair e.g. autotest.AfeClient
57 @returns True if installation was successful or False if it failed
58 """
59 tmp_client_dir = os.path.join(_TMP_COMPILE_DIR, project_client)
60 install_dir = os.path.join(_DEFAULT_INSTALL_DIR, project_client)
61 old_install_dir = os.path.join(_DEFAULT_INSTALL_DIR,
62 project_client + '.old')
63 if not os.path.exists(_DEFAULT_INSTALL_DIR):
64 os.mkdir(_DEFAULT_INSTALL_DIR)
65
66 if os.path.isdir(tmp_client_dir):
67 if os.path.isdir(old_install_dir):
68 shutil.rmtree(old_install_dir)
69 if os.path.isdir(install_dir):
70 os.rename(install_dir, old_install_dir)
71 try:
72 os.rename(tmp_client_dir, install_dir)
73 return True
74 except Exception, err:
75 # If we can't rename the client raise an exception
76 # and put the old client back
77 shutil.rmtree(install_dir)
78 shutil.copytree(old_install_dir, install_dir)
79 print 'Error: copying old client:', err
80 else:
81 print 'Error: Compiled directory is gone, something went wrong'
82
83 return False
84
85
86def compile_and_install_client(project_client, extra_args='',
87 install_client=True):
88 """Compile the client into a temporary directory, if successful
89 call install_completed_client to install the new client.
90 @param project_client: project.client pair e.g. autotest.AfeClient
91 @param install_client: Boolean, if True install the clients
92 @return True if install and compile was successful False if it failed
93 """
94 java_args = {}
95 java_args['compile_dir'] = _TMP_COMPILE_DIR
96 java_args['app_dir'] = _DEFAULT_APP_DIR
97 java_args['gwt_dir'] = find_gwt_dir()
98 java_args['extra_args'] = extra_args
99 java_args['project_client'] = project_client
100 cmd = _COMPILE_LINE % java_args
101
102 print 'Compiling client %s' % project_client
103 try:
104 utils.run(cmd, verbose=True)
showard853d8f42009-10-20 23:48:30 +0000105 if install_client:
showard97b7f5a2009-10-14 16:17:30 +0000106 return install_completed_client(java_args['compile_dir'],
107 project_client)
108 return True
109 except error.CmdError:
110 print 'Error compiling %s, leaving old client' % project_client
111
112 return False
113
114
115def compile_all_projects(projects, extra_args=''):
116 """Compile all projects available as defined by enumerate_projects.
117 @returns list of failed client installations
118 """
119 failed_clients = []
120 for project,clients in enumerate_projects().iteritems():
121 for client in clients:
122 project_client = '%s.%s' % (project, client)
123 if not compile_and_install_client(project_client, extra_args):
124 failed_clients.append(project_client)
125
126 return failed_clients
127
128
129def print_projects():
130 print 'Projects that can be compiled:'
131 for project,clients in enumerate_projects().iteritems():
132 for client in clients:
133 print '%s.%s' % (project, client)
134
135
136def main():
137 parser = optparse.OptionParser()
138 parser.add_option('-l', '--list-projects',
139 action='store_true', dest='list_projects',
140 default=False,
141 help='List all projects and clients that can be compiled')
142 parser.add_option('-a', '--compile-all',
143 action='store_true', dest='compile_all',
144 default=False,
145 help='Compile all available projects and clients')
146 parser.add_option('-c', '--compile',
147 dest='compile_list', action='store',
148 help='List of clients to compiled (e.g. -c "x.X c.C")')
149 parser.add_option('-e', '--extra-args',
150 dest='extra_args', action='store',
151 default='',
152 help='Extra arguments to pass to java')
showarded9f6fb2009-10-20 23:48:48 +0000153 parser.add_option('-d', '--no-install', dest='install_client',
154 action='store_false', default=True,
showard97b7f5a2009-10-14 16:17:30 +0000155 help='Do not install the clients just compile them')
156 options, args = parser.parse_args()
157
158 if len(sys.argv) < 2:
159 parser.print_help()
160 sys.exit(0)
161 elif options.list_projects:
162 print_projects()
163 sys.exit(0)
164 elif options.compile_all and options.compile_list:
165 print '-c and -a are mutually exclusive'
166 parser.print_help()
167 sys.exit(1)
168
169 failed_clients = []
170 if options.compile_all:
171 failed_clients = compile_all_projects(options.extra_args)
172 elif options.compile_list:
173 for client in options.compile_list.split():
174 if not compile_and_install_client(client, options.extra_args,
showarded9f6fb2009-10-20 23:48:48 +0000175 options.install_client):
showard97b7f5a2009-10-14 16:17:30 +0000176 failed_clients.append(client)
177
178 if os.path.exists(_TMP_COMPILE_DIR):
179 shutil.rmtree(_TMP_COMPILE_DIR)
180
181 if failed_clients:
182 print ('Error: The following clients failed: %s'
183 % '\n'.join(failed_clients))
184 sys.exit(1)
185
186
187if __name__ == '__main__':
188 main()