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 |
jamesren | c4497e9 | 2010-02-12 00:46:56 +0000 | [diff] [blame] | 3 | import sys, os, shutil, errno, 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 |
| 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 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" ' |
| 20 | '%(extra_args)s %(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, |
| 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): |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 51 | logging.error('Unable to find GWT. ' |
| 52 | 'You can use utils/build_externals.py to install it.') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 53 | sys.exit(1) |
| 54 | |
| 55 | return _DEFAULT_GWT_DIR |
| 56 | |
| 57 | |
| 58 | def 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) |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 86 | logging.error('Copying old client: %s', err) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 87 | else: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 88 | logging.error('Compiled directory is gone, something went wrong') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 89 | |
| 90 | return False |
| 91 | |
| 92 | |
| 93 | def 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 | |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 109 | logging.info('Compiling client %s', project_client) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 110 | try: |
| 111 | utils.run(cmd, verbose=True) |
showard | 853d8f4 | 2009-10-20 23:48:30 +0000 | [diff] [blame] | 112 | if install_client: |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 113 | return install_completed_client(java_args['compile_dir'], |
| 114 | project_client) |
| 115 | return True |
| 116 | except error.CmdError: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 117 | logging.info('Error compiling %s, leaving old client', project_client) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 118 | |
| 119 | return False |
| 120 | |
| 121 | |
| 122 | def 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 | |
| 136 | def print_projects(): |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 137 | logging.info('Projects that can be compiled:') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 138 | for project,clients in enumerate_projects().iteritems(): |
| 139 | for client in clients: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 140 | logging.info('%s.%s', project, client) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 141 | |
| 142 | |
| 143 | def main(): |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 144 | logging_manager.configure_logging(CompileClientsLoggingConfig(), |
| 145 | verbose=True) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 146 | 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') |
showard | ed9f6fb | 2009-10-20 23:48:48 +0000 | [diff] [blame] | 162 | parser.add_option('-d', '--no-install', dest='install_client', |
| 163 | action='store_false', default=True, |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 164 | 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: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 174 | logging.error('Options -c and -a are mutually exclusive') |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 175 | 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, |
showard | ed9f6fb | 2009-10-20 23:48:48 +0000 | [diff] [blame] | 184 | options.install_client): |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 185 | failed_clients.append(client) |
| 186 | |
| 187 | if os.path.exists(_TMP_COMPILE_DIR): |
| 188 | shutil.rmtree(_TMP_COMPILE_DIR) |
| 189 | |
| 190 | if failed_clients: |
lmr | 1906226 | 2010-04-08 02:17:05 +0000 | [diff] [blame] | 191 | logging.error('The following clients failed: %s', |
| 192 | '\n'.join(failed_clients)) |
showard | 97b7f5a | 2009-10-14 16:17:30 +0000 | [diff] [blame] | 193 | sys.exit(1) |
| 194 | |
| 195 | |
| 196 | if __name__ == '__main__': |
| 197 | main() |