showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 2 | import common |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 3 | import sys, os, shutil, optparse, logging |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 4 | from autotest_lib.client.common_lib import error, utils |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 5 | from autotest_lib.client.common_lib import logging_config, logging_manager |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 6 | """ |
| 7 | Compile All Autotest GWT Clients Living in autotest/frontend/client/src |
| 8 | """ |
| 9 | |
| 10 | _AUTOTEST_DIR = common.autotest_dir |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 11 | _DEFAULT_GWT_DIRS = ['/usr/local/lib/gwt', '/opt/google-web-toolkit'] |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 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 | |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 16 | _COMPILE_LINE = ('java -Xmx512M %(extra_args)s ' |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 17 | '-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] | 18 | ':%(gwt_dir)s/gwt-dev.jar" -Djava.awt.headless=true ' |
| 19 | 'com.google.gwt.dev.Compiler -war "%(compile_dir)s" ' |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 20 | '%(project_client)s') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 21 | |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 22 | class 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) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 27 | |
| 28 | def 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 | |
| 41 | def find_gwt_dir(): |
| 42 | """See if GWT is installed in site-packages or in the system, |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 43 | site-packages is favored over a system install or a /usr/local install. |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 44 | """ |
| 45 | site_gwt = os.path.join(_AUTOTEST_DIR, 'site-packages', 'gwt') |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 46 | gwt_dirs = [site_gwt] + _DEFAULT_GWT_DIRS |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 47 | |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 48 | for gwt_dir in gwt_dirs: |
| 49 | if os.path.isdir(gwt_dir): |
| 50 | return gwt_dir |
| 51 | logging.error('Unable to find GWT. ' |
| 52 | 'You can use utils/build_externals.py to install it.') |
| 53 | sys.exit(1) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def install_completed_client(compiled_dir, project_client): |
| 57 | """Remove old client directory if it exists, move installed client to the |
| 58 | old directory and move newly compield client to the installed client |
| 59 | dir. |
| 60 | @param compiled_dir: Where the new client was compiled |
| 61 | @param project_client: project.client pair e.g. autotest.AfeClient |
| 62 | @returns True if installation was successful or False if it failed |
| 63 | """ |
| 64 | tmp_client_dir = os.path.join(_TMP_COMPILE_DIR, project_client) |
| 65 | install_dir = os.path.join(_DEFAULT_INSTALL_DIR, project_client) |
| 66 | old_install_dir = os.path.join(_DEFAULT_INSTALL_DIR, |
| 67 | project_client + '.old') |
| 68 | if not os.path.exists(_DEFAULT_INSTALL_DIR): |
| 69 | os.mkdir(_DEFAULT_INSTALL_DIR) |
| 70 | |
| 71 | if os.path.isdir(tmp_client_dir): |
| 72 | if os.path.isdir(old_install_dir): |
| 73 | shutil.rmtree(old_install_dir) |
| 74 | if os.path.isdir(install_dir): |
| 75 | os.rename(install_dir, old_install_dir) |
| 76 | try: |
| 77 | os.rename(tmp_client_dir, install_dir) |
| 78 | return True |
| 79 | except Exception, err: |
| 80 | # If we can't rename the client raise an exception |
| 81 | # and put the old client back |
| 82 | shutil.rmtree(install_dir) |
| 83 | shutil.copytree(old_install_dir, install_dir) |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 84 | logging.error('Copying old client: %s', err) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 85 | else: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 86 | logging.error('Compiled directory is gone, something went wrong') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 87 | |
| 88 | return False |
| 89 | |
| 90 | |
| 91 | def compile_and_install_client(project_client, extra_args='', |
| 92 | install_client=True): |
| 93 | """Compile the client into a temporary directory, if successful |
| 94 | call install_completed_client to install the new client. |
| 95 | @param project_client: project.client pair e.g. autotest.AfeClient |
| 96 | @param install_client: Boolean, if True install the clients |
| 97 | @return True if install and compile was successful False if it failed |
| 98 | """ |
| 99 | java_args = {} |
| 100 | java_args['compile_dir'] = _TMP_COMPILE_DIR |
| 101 | java_args['app_dir'] = _DEFAULT_APP_DIR |
| 102 | java_args['gwt_dir'] = find_gwt_dir() |
| 103 | java_args['extra_args'] = extra_args |
| 104 | java_args['project_client'] = project_client |
| 105 | cmd = _COMPILE_LINE % java_args |
| 106 | |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 107 | logging.info('Compiling client %s', project_client) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 108 | try: |
| 109 | utils.run(cmd, verbose=True) |
showard | 853d8f4 | 2009-10-20 23:48:30 +0000 | [diff] [blame] | 110 | if install_client: |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 111 | return install_completed_client(java_args['compile_dir'], |
| 112 | project_client) |
| 113 | return True |
| 114 | except error.CmdError: |
Simran Basi | 2017f90 | 2014-06-18 10:44:11 -0700 | [diff] [blame] | 115 | logging.error('Error compiling %s, leaving old client', project_client) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 116 | |
| 117 | return False |
| 118 | |
| 119 | |
Simran Basi | c0f6fd2 | 2014-02-20 11:12:09 -0800 | [diff] [blame] | 120 | def compile_all_projects(extra_args=''): |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 121 | """Compile all projects available as defined by enumerate_projects. |
| 122 | @returns list of failed client installations |
| 123 | """ |
| 124 | failed_clients = [] |
| 125 | for project,clients in enumerate_projects().iteritems(): |
| 126 | for client in clients: |
| 127 | project_client = '%s.%s' % (project, client) |
| 128 | if not compile_and_install_client(project_client, extra_args): |
| 129 | failed_clients.append(project_client) |
| 130 | |
| 131 | return failed_clients |
| 132 | |
| 133 | |
| 134 | def print_projects(): |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 135 | logging.info('Projects that can be compiled:') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 136 | for project,clients in enumerate_projects().iteritems(): |
| 137 | for client in clients: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 138 | logging.info('%s.%s', project, client) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 139 | |
| 140 | |
| 141 | def main(): |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 142 | logging_manager.configure_logging(CompileClientsLoggingConfig(), |
| 143 | verbose=True) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 144 | parser = optparse.OptionParser() |
| 145 | parser.add_option('-l', '--list-projects', |
| 146 | action='store_true', dest='list_projects', |
| 147 | default=False, |
| 148 | help='List all projects and clients that can be compiled') |
| 149 | parser.add_option('-a', '--compile-all', |
| 150 | action='store_true', dest='compile_all', |
| 151 | default=False, |
| 152 | help='Compile all available projects and clients') |
| 153 | parser.add_option('-c', '--compile', |
| 154 | dest='compile_list', action='store', |
| 155 | help='List of clients to compiled (e.g. -c "x.X c.C")') |
| 156 | parser.add_option('-e', '--extra-args', |
| 157 | dest='extra_args', action='store', |
| 158 | default='', |
| 159 | help='Extra arguments to pass to java') |
showard | ed9f6fb | 2009-10-20 23:48:48 +0000 | [diff] [blame] | 160 | parser.add_option('-d', '--no-install', dest='install_client', |
| 161 | action='store_false', default=True, |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 162 | help='Do not install the clients just compile them') |
| 163 | options, args = parser.parse_args() |
| 164 | |
| 165 | if len(sys.argv) < 2: |
| 166 | parser.print_help() |
| 167 | sys.exit(0) |
| 168 | elif options.list_projects: |
| 169 | print_projects() |
| 170 | sys.exit(0) |
| 171 | elif options.compile_all and options.compile_list: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 172 | logging.error('Options -c and -a are mutually exclusive') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 173 | parser.print_help() |
| 174 | sys.exit(1) |
| 175 | |
| 176 | failed_clients = [] |
| 177 | if options.compile_all: |
| 178 | failed_clients = compile_all_projects(options.extra_args) |
| 179 | elif options.compile_list: |
| 180 | for client in options.compile_list.split(): |
| 181 | if not compile_and_install_client(client, options.extra_args, |
showard | ed9f6fb | 2009-10-20 23:48:48 +0000 | [diff] [blame] | 182 | options.install_client): |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 183 | failed_clients.append(client) |
| 184 | |
| 185 | if os.path.exists(_TMP_COMPILE_DIR): |
| 186 | shutil.rmtree(_TMP_COMPILE_DIR) |
| 187 | |
| 188 | if failed_clients: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 189 | logging.error('The following clients failed: %s', |
| 190 | '\n'.join(failed_clients)) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 191 | sys.exit(1) |
| 192 | |
| 193 | |
| 194 | if __name__ == '__main__': |
| 195 | main() |