blob: 60470406062d485aa0a813907c4174cee4e5f1f6 [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/env python2
Derek Beckettdb735112020-08-27 10:25:15 -07002
3from __future__ import print_function
4from __future__ import division
5from __future__ import absolute_import
6
showard97b7f5a2009-10-14 16:17:30 +00007import common
Simran Basic0f6fd22014-02-20 11:12:09 -08008import sys, os, shutil, optparse, logging
Derek Beckettdb735112020-08-27 10:25:15 -07009import six
10
showard97b7f5a2009-10-14 16:17:30 +000011from autotest_lib.client.common_lib import error, utils
lmr19062262010-04-08 02:17:05 +000012from autotest_lib.client.common_lib import logging_config, logging_manager
Derek Beckettdb735112020-08-27 10:25:15 -070013
14
showard97b7f5a2009-10-14 16:17:30 +000015"""
16Compile All Autotest GWT Clients Living in autotest/frontend/client/src
17"""
18
Derek Beckettdb735112020-08-27 10:25:15 -070019
20
showard97b7f5a2009-10-14 16:17:30 +000021_AUTOTEST_DIR = common.autotest_dir
Simran Basic0f6fd22014-02-20 11:12:09 -080022_DEFAULT_GWT_DIRS = ['/usr/local/lib/gwt', '/opt/google-web-toolkit']
showard97b7f5a2009-10-14 16:17:30 +000023_DEFAULT_APP_DIR = os.path.join(_AUTOTEST_DIR, 'frontend/client')
24_DEFAULT_INSTALL_DIR = os.path.join(_DEFAULT_APP_DIR, 'www')
25_TMP_COMPILE_DIR = _DEFAULT_INSTALL_DIR + '.new'
26
Simran Basic0f6fd22014-02-20 11:12:09 -080027_COMPILE_LINE = ('java -Xmx512M %(extra_args)s '
showard97b7f5a2009-10-14 16:17:30 +000028 '-cp "%(app_dir)s/src:%(app_dir)s/bin:%(gwt_dir)s/gwt-user.jar'
Dale Curtis74a314b2011-06-23 14:55:46 -070029 ':%(gwt_dir)s/gwt-dev.jar" -Djava.awt.headless=true '
30 'com.google.gwt.dev.Compiler -war "%(compile_dir)s" '
Simran Basic0f6fd22014-02-20 11:12:09 -080031 '%(project_client)s')
showard97b7f5a2009-10-14 16:17:30 +000032
lmr19062262010-04-08 02:17:05 +000033class CompileClientsLoggingConfig(logging_config.LoggingConfig):
34 def configure_logging(self, results_dir=None, verbose=False):
35 super(CompileClientsLoggingConfig, self).configure_logging(
36 use_console=True,
37 verbose=verbose)
showard97b7f5a2009-10-14 16:17:30 +000038
39def enumerate_projects():
40 """List projects in _DEFAULT_APP_DIR."""
41 src_path = os.path.join(_DEFAULT_APP_DIR, 'src')
42 projects = {}
43 for project in os.listdir(src_path):
44 projects[project] = []
45 project_path = os.path.join(src_path, project)
46 for file in os.listdir(project_path):
47 if file.endswith('.gwt.xml'):
48 projects[project].append(file[:-8])
49 return projects
50
51
52def find_gwt_dir():
53 """See if GWT is installed in site-packages or in the system,
Simran Basic0f6fd22014-02-20 11:12:09 -080054 site-packages is favored over a system install or a /usr/local install.
showard97b7f5a2009-10-14 16:17:30 +000055 """
56 site_gwt = os.path.join(_AUTOTEST_DIR, 'site-packages', 'gwt')
Simran Basic0f6fd22014-02-20 11:12:09 -080057 gwt_dirs = [site_gwt] + _DEFAULT_GWT_DIRS
showard97b7f5a2009-10-14 16:17:30 +000058
Simran Basic0f6fd22014-02-20 11:12:09 -080059 for gwt_dir in gwt_dirs:
60 if os.path.isdir(gwt_dir):
61 return gwt_dir
62 logging.error('Unable to find GWT. '
63 'You can use utils/build_externals.py to install it.')
64 sys.exit(1)
showard97b7f5a2009-10-14 16:17:30 +000065
66
67def install_completed_client(compiled_dir, project_client):
68 """Remove old client directory if it exists, move installed client to the
69 old directory and move newly compield client to the installed client
70 dir.
71 @param compiled_dir: Where the new client was compiled
72 @param project_client: project.client pair e.g. autotest.AfeClient
73 @returns True if installation was successful or False if it failed
74 """
75 tmp_client_dir = os.path.join(_TMP_COMPILE_DIR, project_client)
76 install_dir = os.path.join(_DEFAULT_INSTALL_DIR, project_client)
77 old_install_dir = os.path.join(_DEFAULT_INSTALL_DIR,
78 project_client + '.old')
79 if not os.path.exists(_DEFAULT_INSTALL_DIR):
80 os.mkdir(_DEFAULT_INSTALL_DIR)
81
82 if os.path.isdir(tmp_client_dir):
83 if os.path.isdir(old_install_dir):
84 shutil.rmtree(old_install_dir)
85 if os.path.isdir(install_dir):
86 os.rename(install_dir, old_install_dir)
87 try:
88 os.rename(tmp_client_dir, install_dir)
89 return True
Derek Beckettdb735112020-08-27 10:25:15 -070090 except Exception as err:
showard97b7f5a2009-10-14 16:17:30 +000091 # If we can't rename the client raise an exception
92 # and put the old client back
93 shutil.rmtree(install_dir)
94 shutil.copytree(old_install_dir, install_dir)
lmr19062262010-04-08 02:17:05 +000095 logging.error('Copying old client: %s', err)
showard97b7f5a2009-10-14 16:17:30 +000096 else:
lmr19062262010-04-08 02:17:05 +000097 logging.error('Compiled directory is gone, something went wrong')
showard97b7f5a2009-10-14 16:17:30 +000098
99 return False
100
101
102def compile_and_install_client(project_client, extra_args='',
103 install_client=True):
104 """Compile the client into a temporary directory, if successful
105 call install_completed_client to install the new client.
106 @param project_client: project.client pair e.g. autotest.AfeClient
107 @param install_client: Boolean, if True install the clients
108 @return True if install and compile was successful False if it failed
109 """
110 java_args = {}
111 java_args['compile_dir'] = _TMP_COMPILE_DIR
112 java_args['app_dir'] = _DEFAULT_APP_DIR
113 java_args['gwt_dir'] = find_gwt_dir()
114 java_args['extra_args'] = extra_args
115 java_args['project_client'] = project_client
116 cmd = _COMPILE_LINE % java_args
117
lmr19062262010-04-08 02:17:05 +0000118 logging.info('Compiling client %s', project_client)
showard97b7f5a2009-10-14 16:17:30 +0000119 try:
120 utils.run(cmd, verbose=True)
showard853d8f42009-10-20 23:48:30 +0000121 if install_client:
showard97b7f5a2009-10-14 16:17:30 +0000122 return install_completed_client(java_args['compile_dir'],
123 project_client)
124 return True
125 except error.CmdError:
Simran Basi2017f902014-06-18 10:44:11 -0700126 logging.error('Error compiling %s, leaving old client', project_client)
showard97b7f5a2009-10-14 16:17:30 +0000127
128 return False
129
130
Simran Basic0f6fd22014-02-20 11:12:09 -0800131def compile_all_projects(extra_args=''):
showard97b7f5a2009-10-14 16:17:30 +0000132 """Compile all projects available as defined by enumerate_projects.
133 @returns list of failed client installations
134 """
135 failed_clients = []
Derek Beckettdb735112020-08-27 10:25:15 -0700136 for project,clients in six.iteritems(enumerate_projects()):
showard97b7f5a2009-10-14 16:17:30 +0000137 for client in clients:
138 project_client = '%s.%s' % (project, client)
139 if not compile_and_install_client(project_client, extra_args):
140 failed_clients.append(project_client)
141
142 return failed_clients
143
144
145def print_projects():
lmr19062262010-04-08 02:17:05 +0000146 logging.info('Projects that can be compiled:')
Derek Beckettdb735112020-08-27 10:25:15 -0700147 for project,clients in six.iteritems(enumerate_projects()):
showard97b7f5a2009-10-14 16:17:30 +0000148 for client in clients:
lmr19062262010-04-08 02:17:05 +0000149 logging.info('%s.%s', project, client)
showard97b7f5a2009-10-14 16:17:30 +0000150
151
152def main():
lmr19062262010-04-08 02:17:05 +0000153 logging_manager.configure_logging(CompileClientsLoggingConfig(),
154 verbose=True)
showard97b7f5a2009-10-14 16:17:30 +0000155 parser = optparse.OptionParser()
156 parser.add_option('-l', '--list-projects',
157 action='store_true', dest='list_projects',
158 default=False,
159 help='List all projects and clients that can be compiled')
160 parser.add_option('-a', '--compile-all',
161 action='store_true', dest='compile_all',
162 default=False,
163 help='Compile all available projects and clients')
164 parser.add_option('-c', '--compile',
165 dest='compile_list', action='store',
166 help='List of clients to compiled (e.g. -c "x.X c.C")')
167 parser.add_option('-e', '--extra-args',
168 dest='extra_args', action='store',
169 default='',
170 help='Extra arguments to pass to java')
showarded9f6fb2009-10-20 23:48:48 +0000171 parser.add_option('-d', '--no-install', dest='install_client',
172 action='store_false', default=True,
showard97b7f5a2009-10-14 16:17:30 +0000173 help='Do not install the clients just compile them')
174 options, args = parser.parse_args()
175
176 if len(sys.argv) < 2:
177 parser.print_help()
178 sys.exit(0)
179 elif options.list_projects:
180 print_projects()
181 sys.exit(0)
182 elif options.compile_all and options.compile_list:
lmr19062262010-04-08 02:17:05 +0000183 logging.error('Options -c and -a are mutually exclusive')
showard97b7f5a2009-10-14 16:17:30 +0000184 parser.print_help()
185 sys.exit(1)
186
187 failed_clients = []
188 if options.compile_all:
189 failed_clients = compile_all_projects(options.extra_args)
190 elif options.compile_list:
191 for client in options.compile_list.split():
192 if not compile_and_install_client(client, options.extra_args,
showarded9f6fb2009-10-20 23:48:48 +0000193 options.install_client):
showard97b7f5a2009-10-14 16:17:30 +0000194 failed_clients.append(client)
195
196 if os.path.exists(_TMP_COMPILE_DIR):
197 shutil.rmtree(_TMP_COMPILE_DIR)
198
199 if failed_clients:
lmr19062262010-04-08 02:17:05 +0000200 logging.error('The following clients failed: %s',
201 '\n'.join(failed_clients))
showard97b7f5a2009-10-14 16:17:30 +0000202 sys.exit(1)
203
204
205if __name__ == '__main__':
206 main()