blob: f198d5bd4799963fefb34263bc9c82174046a45c [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
5"""
6Compile All Autotest GWT Clients Living in autotest/frontend/client/src
7"""
8
jamesrenc4497e92010-02-12 00:46:56 +00009
10logging.basicConfig(level=logging.DEBUG)
showard97b7f5a2009-10-14 16:17:30 +000011_AUTOTEST_DIR = common.autotest_dir
12_DEFAULT_GWT_DIR = '/usr/local/lib/gwt'
13_DEFAULT_APP_DIR = os.path.join(_AUTOTEST_DIR, 'frontend/client')
14_DEFAULT_INSTALL_DIR = os.path.join(_DEFAULT_APP_DIR, 'www')
15_TMP_COMPILE_DIR = _DEFAULT_INSTALL_DIR + '.new'
16
17_COMPILE_LINE = ('java -Xmx512M '
18 '-cp "%(app_dir)s/src:%(app_dir)s/bin:%(gwt_dir)s/gwt-user.jar'
19 ':%(gwt_dir)s/gwt-dev-linux.jar" '
20 '-Djava.awt.headless=true com.google.gwt.dev.Compiler '
21 '-war "%(compile_dir)s" %(extra_args)s %(project_client)s')
22
23
24def enumerate_projects():
25 """List projects in _DEFAULT_APP_DIR."""
26 src_path = os.path.join(_DEFAULT_APP_DIR, 'src')
27 projects = {}
28 for project in os.listdir(src_path):
29 projects[project] = []
30 project_path = os.path.join(src_path, project)
31 for file in os.listdir(project_path):
32 if file.endswith('.gwt.xml'):
33 projects[project].append(file[:-8])
34 return projects
35
36
37def find_gwt_dir():
38 """See if GWT is installed in site-packages or in the system,
39 site-packages is favored over a system install.
40 """
41 site_gwt = os.path.join(_AUTOTEST_DIR, 'site-packages', 'gwt')
42
43 if os.path.isdir(site_gwt):
44 return site_gwt
45
46 if not os.path.isdir(_DEFAULT_GWT_DIR):
47 print 'Error: Unable to find GWT, is GWT installed?\n'
48 sys.exit(1)
49
50 return _DEFAULT_GWT_DIR
51
52
53def install_completed_client(compiled_dir, project_client):
54 """Remove old client directory if it exists, move installed client to the
55 old directory and move newly compield client to the installed client
56 dir.
57 @param compiled_dir: Where the new client was compiled
58 @param project_client: project.client pair e.g. autotest.AfeClient
59 @returns True if installation was successful or False if it failed
60 """
61 tmp_client_dir = os.path.join(_TMP_COMPILE_DIR, project_client)
62 install_dir = os.path.join(_DEFAULT_INSTALL_DIR, project_client)
63 old_install_dir = os.path.join(_DEFAULT_INSTALL_DIR,
64 project_client + '.old')
65 if not os.path.exists(_DEFAULT_INSTALL_DIR):
66 os.mkdir(_DEFAULT_INSTALL_DIR)
67
68 if os.path.isdir(tmp_client_dir):
69 if os.path.isdir(old_install_dir):
70 shutil.rmtree(old_install_dir)
71 if os.path.isdir(install_dir):
72 os.rename(install_dir, old_install_dir)
73 try:
74 os.rename(tmp_client_dir, install_dir)
75 return True
76 except Exception, err:
77 # If we can't rename the client raise an exception
78 # and put the old client back
79 shutil.rmtree(install_dir)
80 shutil.copytree(old_install_dir, install_dir)
81 print 'Error: copying old client:', err
82 else:
83 print 'Error: Compiled directory is gone, something went wrong'
84
85 return False
86
87
88def compile_and_install_client(project_client, extra_args='',
89 install_client=True):
90 """Compile the client into a temporary directory, if successful
91 call install_completed_client to install the new client.
92 @param project_client: project.client pair e.g. autotest.AfeClient
93 @param install_client: Boolean, if True install the clients
94 @return True if install and compile was successful False if it failed
95 """
96 java_args = {}
97 java_args['compile_dir'] = _TMP_COMPILE_DIR
98 java_args['app_dir'] = _DEFAULT_APP_DIR
99 java_args['gwt_dir'] = find_gwt_dir()
100 java_args['extra_args'] = extra_args
101 java_args['project_client'] = project_client
102 cmd = _COMPILE_LINE % java_args
103
104 print 'Compiling client %s' % project_client
105 try:
106 utils.run(cmd, verbose=True)
showard853d8f42009-10-20 23:48:30 +0000107 if install_client:
showard97b7f5a2009-10-14 16:17:30 +0000108 return install_completed_client(java_args['compile_dir'],
109 project_client)
110 return True
111 except error.CmdError:
112 print 'Error compiling %s, leaving old client' % project_client
113
114 return False
115
116
117def compile_all_projects(projects, extra_args=''):
118 """Compile all projects available as defined by enumerate_projects.
119 @returns list of failed client installations
120 """
121 failed_clients = []
122 for project,clients in enumerate_projects().iteritems():
123 for client in clients:
124 project_client = '%s.%s' % (project, client)
125 if not compile_and_install_client(project_client, extra_args):
126 failed_clients.append(project_client)
127
128 return failed_clients
129
130
131def print_projects():
132 print 'Projects that can be compiled:'
133 for project,clients in enumerate_projects().iteritems():
134 for client in clients:
135 print '%s.%s' % (project, client)
136
137
138def main():
139 parser = optparse.OptionParser()
140 parser.add_option('-l', '--list-projects',
141 action='store_true', dest='list_projects',
142 default=False,
143 help='List all projects and clients that can be compiled')
144 parser.add_option('-a', '--compile-all',
145 action='store_true', dest='compile_all',
146 default=False,
147 help='Compile all available projects and clients')
148 parser.add_option('-c', '--compile',
149 dest='compile_list', action='store',
150 help='List of clients to compiled (e.g. -c "x.X c.C")')
151 parser.add_option('-e', '--extra-args',
152 dest='extra_args', action='store',
153 default='',
154 help='Extra arguments to pass to java')
showarded9f6fb2009-10-20 23:48:48 +0000155 parser.add_option('-d', '--no-install', dest='install_client',
156 action='store_false', default=True,
showard97b7f5a2009-10-14 16:17:30 +0000157 help='Do not install the clients just compile them')
158 options, args = parser.parse_args()
159
160 if len(sys.argv) < 2:
161 parser.print_help()
162 sys.exit(0)
163 elif options.list_projects:
164 print_projects()
165 sys.exit(0)
166 elif options.compile_all and options.compile_list:
167 print '-c and -a are mutually exclusive'
168 parser.print_help()
169 sys.exit(1)
170
171 failed_clients = []
172 if options.compile_all:
173 failed_clients = compile_all_projects(options.extra_args)
174 elif options.compile_list:
175 for client in options.compile_list.split():
176 if not compile_and_install_client(client, options.extra_args,
showarded9f6fb2009-10-20 23:48:48 +0000177 options.install_client):
showard97b7f5a2009-10-14 16:17:30 +0000178 failed_clients.append(client)
179
180 if os.path.exists(_TMP_COMPILE_DIR):
181 shutil.rmtree(_TMP_COMPILE_DIR)
182
183 if failed_clients:
184 print ('Error: The following clients failed: %s'
185 % '\n'.join(failed_clients))
186 sys.exit(1)
187
188
189if __name__ == '__main__':
190 main()