Mike Frysinger | d03e6b5 | 2019-08-03 12:49:01 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame^] | 2 | |
| 3 | from __future__ import print_function |
| 4 | from __future__ import division |
| 5 | from __future__ import absolute_import |
| 6 | |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 7 | import common |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 8 | import sys, os, shutil, optparse, logging |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame^] | 9 | import six |
| 10 | |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import error, utils |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 12 | from autotest_lib.client.common_lib import logging_config, logging_manager |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame^] | 13 | |
| 14 | |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 15 | """ |
| 16 | Compile All Autotest GWT Clients Living in autotest/frontend/client/src |
| 17 | """ |
| 18 | |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame^] | 19 | |
| 20 | |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 21 | _AUTOTEST_DIR = common.autotest_dir |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 22 | _DEFAULT_GWT_DIRS = ['/usr/local/lib/gwt', '/opt/google-web-toolkit'] |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 23 | _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 Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 27 | _COMPILE_LINE = ('java -Xmx512M %(extra_args)s ' |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 28 | '-cp "%(app_dir)s/src:%(app_dir)s/bin:%(gwt_dir)s/gwt-user.jar' |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 29 | ':%(gwt_dir)s/gwt-dev.jar" -Djava.awt.headless=true ' |
| 30 | 'com.google.gwt.dev.Compiler -war "%(compile_dir)s" ' |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 31 | '%(project_client)s') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 32 | |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 33 | class 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) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 38 | |
| 39 | def 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 | |
| 52 | def find_gwt_dir(): |
| 53 | """See if GWT is installed in site-packages or in the system, |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 54 | site-packages is favored over a system install or a /usr/local install. |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 55 | """ |
| 56 | site_gwt = os.path.join(_AUTOTEST_DIR, 'site-packages', 'gwt') |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 57 | gwt_dirs = [site_gwt] + _DEFAULT_GWT_DIRS |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 58 | |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 59 | 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) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def 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 Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame^] | 90 | except Exception as err: |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 91 | # 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) |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 95 | logging.error('Copying old client: %s', err) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 96 | else: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 97 | logging.error('Compiled directory is gone, something went wrong') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 98 | |
| 99 | return False |
| 100 | |
| 101 | |
| 102 | def 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 | |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 118 | logging.info('Compiling client %s', project_client) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 119 | try: |
| 120 | utils.run(cmd, verbose=True) |
showard | 853d8f4 | 2009-10-20 23:48:30 +0000 | [diff] [blame] | 121 | if install_client: |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 122 | return install_completed_client(java_args['compile_dir'], |
| 123 | project_client) |
| 124 | return True |
| 125 | except error.CmdError: |
Simran Basi | 2017f90 | 2014-06-18 10:44:11 -0700 | [diff] [blame] | 126 | logging.error('Error compiling %s, leaving old client', project_client) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 127 | |
| 128 | return False |
| 129 | |
| 130 | |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 131 | def compile_all_projects(extra_args=''): |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 132 | """Compile all projects available as defined by enumerate_projects. |
| 133 | @returns list of failed client installations |
| 134 | """ |
| 135 | failed_clients = [] |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame^] | 136 | for project,clients in six.iteritems(enumerate_projects()): |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 137 | 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 | |
| 145 | def print_projects(): |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 146 | logging.info('Projects that can be compiled:') |
Derek Beckett | db73511 | 2020-08-27 10:25:15 -0700 | [diff] [blame^] | 147 | for project,clients in six.iteritems(enumerate_projects()): |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 148 | for client in clients: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 149 | logging.info('%s.%s', project, client) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 150 | |
| 151 | |
| 152 | def main(): |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 153 | logging_manager.configure_logging(CompileClientsLoggingConfig(), |
| 154 | verbose=True) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 155 | 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') |
showard | ed9f6fb | 2009-10-20 23:48:48 +0000 | [diff] [blame] | 171 | parser.add_option('-d', '--no-install', dest='install_client', |
| 172 | action='store_false', default=True, |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 173 | 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: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 183 | logging.error('Options -c and -a are mutually exclusive') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 184 | 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, |
showard | ed9f6fb | 2009-10-20 23:48:48 +0000 | [diff] [blame] | 193 | options.install_client): |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 194 | failed_clients.append(client) |
| 195 | |
| 196 | if os.path.exists(_TMP_COMPILE_DIR): |
| 197 | shutil.rmtree(_TMP_COMPILE_DIR) |
| 198 | |
| 199 | if failed_clients: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 200 | logging.error('The following clients failed: %s', |
| 201 | '\n'.join(failed_clients)) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 202 | sys.exit(1) |
| 203 | |
| 204 | |
| 205 | if __name__ == '__main__': |
| 206 | main() |