blob: e9aa86a6b62231aa2d81b11a4c1e757b3d92bfcc [file] [log] [blame]
showard97b7f5a2009-10-14 16:17:30 +00001#!/usr/bin/python
showard97b7f5a2009-10-14 16:17:30 +00002import common
jamesrenc4497e92010-02-12 00:46:56 +00003import sys, os, shutil, errno, optparse, logging
showard97b7f5a2009-10-14 16:17:30 +00004from autotest_lib.client.common_lib import error, utils
lmr19062262010-04-08 02:17:05 +00005from autotest_lib.client.common_lib import logging_config, logging_manager
showard97b7f5a2009-10-14 16:17:30 +00006"""
7Compile All Autotest GWT Clients Living in autotest/frontend/client/src
8"""
9
10_AUTOTEST_DIR = common.autotest_dir
11_DEFAULT_GWT_DIR = '/usr/local/lib/gwt'
12_DEFAULT_APP_DIR = os.path.join(_AUTOTEST_DIR, 'frontend/client')
13_DEFAULT_INSTALL_DIR = os.path.join(_DEFAULT_APP_DIR, 'www')
14_TMP_COMPILE_DIR = _DEFAULT_INSTALL_DIR + '.new'
15
16_COMPILE_LINE = ('java -Xmx512M '
17 '-cp "%(app_dir)s/src:%(app_dir)s/bin:%(gwt_dir)s/gwt-user.jar'
Dale Curtis74a314b2011-06-23 14:55:46 -070018 ':%(gwt_dir)s/gwt-dev.jar" -Djava.awt.headless=true '
19 'com.google.gwt.dev.Compiler -war "%(compile_dir)s" '
20 '%(extra_args)s %(project_client)s')
showard97b7f5a2009-10-14 16:17:30 +000021
lmr19062262010-04-08 02:17:05 +000022class CompileClientsLoggingConfig(logging_config.LoggingConfig):
23 def configure_logging(self, results_dir=None, verbose=False):
24 super(CompileClientsLoggingConfig, self).configure_logging(
25 use_console=True,
26 verbose=verbose)
showard97b7f5a2009-10-14 16:17:30 +000027
28def enumerate_projects():
29 """List projects in _DEFAULT_APP_DIR."""
30 src_path = os.path.join(_DEFAULT_APP_DIR, 'src')
31 projects = {}
32 for project in os.listdir(src_path):
33 projects[project] = []
34 project_path = os.path.join(src_path, project)
35 for file in os.listdir(project_path):
36 if file.endswith('.gwt.xml'):
37 projects[project].append(file[:-8])
38 return projects
39
40
41def find_gwt_dir():
42 """See if GWT is installed in site-packages or in the system,
43 site-packages is favored over a system install.
44 """
45 site_gwt = os.path.join(_AUTOTEST_DIR, 'site-packages', 'gwt')
46
47 if os.path.isdir(site_gwt):
48 return site_gwt
49
50 if not os.path.isdir(_DEFAULT_GWT_DIR):
lmr19062262010-04-08 02:17:05 +000051 logging.error('Unable to find GWT. '
52 'You can use utils/build_externals.py to install it.')
showard97b7f5a2009-10-14 16:17:30 +000053 sys.exit(1)
54
55 return _DEFAULT_GWT_DIR
56
57
58def install_completed_client(compiled_dir, project_client):
59 """Remove old client directory if it exists, move installed client to the
60 old directory and move newly compield client to the installed client
61 dir.
62 @param compiled_dir: Where the new client was compiled
63 @param project_client: project.client pair e.g. autotest.AfeClient
64 @returns True if installation was successful or False if it failed
65 """
66 tmp_client_dir = os.path.join(_TMP_COMPILE_DIR, project_client)
67 install_dir = os.path.join(_DEFAULT_INSTALL_DIR, project_client)
68 old_install_dir = os.path.join(_DEFAULT_INSTALL_DIR,
69 project_client + '.old')
70 if not os.path.exists(_DEFAULT_INSTALL_DIR):
71 os.mkdir(_DEFAULT_INSTALL_DIR)
72
73 if os.path.isdir(tmp_client_dir):
74 if os.path.isdir(old_install_dir):
75 shutil.rmtree(old_install_dir)
76 if os.path.isdir(install_dir):
77 os.rename(install_dir, old_install_dir)
78 try:
79 os.rename(tmp_client_dir, install_dir)
80 return True
81 except Exception, err:
82 # If we can't rename the client raise an exception
83 # and put the old client back
84 shutil.rmtree(install_dir)
85 shutil.copytree(old_install_dir, install_dir)
lmr19062262010-04-08 02:17:05 +000086 logging.error('Copying old client: %s', err)
showard97b7f5a2009-10-14 16:17:30 +000087 else:
lmr19062262010-04-08 02:17:05 +000088 logging.error('Compiled directory is gone, something went wrong')
showard97b7f5a2009-10-14 16:17:30 +000089
90 return False
91
92
93def compile_and_install_client(project_client, extra_args='',
94 install_client=True):
95 """Compile the client into a temporary directory, if successful
96 call install_completed_client to install the new client.
97 @param project_client: project.client pair e.g. autotest.AfeClient
98 @param install_client: Boolean, if True install the clients
99 @return True if install and compile was successful False if it failed
100 """
101 java_args = {}
102 java_args['compile_dir'] = _TMP_COMPILE_DIR
103 java_args['app_dir'] = _DEFAULT_APP_DIR
104 java_args['gwt_dir'] = find_gwt_dir()
105 java_args['extra_args'] = extra_args
106 java_args['project_client'] = project_client
107 cmd = _COMPILE_LINE % java_args
108
lmr19062262010-04-08 02:17:05 +0000109 logging.info('Compiling client %s', project_client)
showard97b7f5a2009-10-14 16:17:30 +0000110 try:
111 utils.run(cmd, verbose=True)
showard853d8f42009-10-20 23:48:30 +0000112 if install_client:
showard97b7f5a2009-10-14 16:17:30 +0000113 return install_completed_client(java_args['compile_dir'],
114 project_client)
115 return True
116 except error.CmdError:
lmr19062262010-04-08 02:17:05 +0000117 logging.info('Error compiling %s, leaving old client', project_client)
showard97b7f5a2009-10-14 16:17:30 +0000118
119 return False
120
121
122def compile_all_projects(projects, extra_args=''):
123 """Compile all projects available as defined by enumerate_projects.
124 @returns list of failed client installations
125 """
126 failed_clients = []
127 for project,clients in enumerate_projects().iteritems():
128 for client in clients:
129 project_client = '%s.%s' % (project, client)
130 if not compile_and_install_client(project_client, extra_args):
131 failed_clients.append(project_client)
132
133 return failed_clients
134
135
136def print_projects():
lmr19062262010-04-08 02:17:05 +0000137 logging.info('Projects that can be compiled:')
showard97b7f5a2009-10-14 16:17:30 +0000138 for project,clients in enumerate_projects().iteritems():
139 for client in clients:
lmr19062262010-04-08 02:17:05 +0000140 logging.info('%s.%s', project, client)
showard97b7f5a2009-10-14 16:17:30 +0000141
142
143def main():
lmr19062262010-04-08 02:17:05 +0000144 logging_manager.configure_logging(CompileClientsLoggingConfig(),
145 verbose=True)
showard97b7f5a2009-10-14 16:17:30 +0000146 parser = optparse.OptionParser()
147 parser.add_option('-l', '--list-projects',
148 action='store_true', dest='list_projects',
149 default=False,
150 help='List all projects and clients that can be compiled')
151 parser.add_option('-a', '--compile-all',
152 action='store_true', dest='compile_all',
153 default=False,
154 help='Compile all available projects and clients')
155 parser.add_option('-c', '--compile',
156 dest='compile_list', action='store',
157 help='List of clients to compiled (e.g. -c "x.X c.C")')
158 parser.add_option('-e', '--extra-args',
159 dest='extra_args', action='store',
160 default='',
161 help='Extra arguments to pass to java')
showarded9f6fb2009-10-20 23:48:48 +0000162 parser.add_option('-d', '--no-install', dest='install_client',
163 action='store_false', default=True,
showard97b7f5a2009-10-14 16:17:30 +0000164 help='Do not install the clients just compile them')
165 options, args = parser.parse_args()
166
167 if len(sys.argv) < 2:
168 parser.print_help()
169 sys.exit(0)
170 elif options.list_projects:
171 print_projects()
172 sys.exit(0)
173 elif options.compile_all and options.compile_list:
lmr19062262010-04-08 02:17:05 +0000174 logging.error('Options -c and -a are mutually exclusive')
showard97b7f5a2009-10-14 16:17:30 +0000175 parser.print_help()
176 sys.exit(1)
177
178 failed_clients = []
179 if options.compile_all:
180 failed_clients = compile_all_projects(options.extra_args)
181 elif options.compile_list:
182 for client in options.compile_list.split():
183 if not compile_and_install_client(client, options.extra_args,
showarded9f6fb2009-10-20 23:48:48 +0000184 options.install_client):
showard97b7f5a2009-10-14 16:17:30 +0000185 failed_clients.append(client)
186
187 if os.path.exists(_TMP_COMPILE_DIR):
188 shutil.rmtree(_TMP_COMPILE_DIR)
189
190 if failed_clients:
lmr19062262010-04-08 02:17:05 +0000191 logging.error('The following clients failed: %s',
192 '\n'.join(failed_clients))
showard97b7f5a2009-10-14 16:17:30 +0000193 sys.exit(1)
194
195
196if __name__ == '__main__':
197 main()