blob: 7e7d89552ac0188354add19b1f95234421a11ee6 [file] [log] [blame]
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -07001#!/usr/bin/python
2# Copyright (C) 2013 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
David Pursehouse7a16e852013-10-18 15:08:44 +090016from __future__ import print_function
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070017from optparse import OptionParser
David Pursehouse686bfea2014-12-04 14:23:30 +090018from os import chdir, makedirs, path, symlink
David Pursehouse15a9f532014-06-23 10:55:20 +090019from subprocess import check_call, check_output
David Pursehouse7a16e852013-10-18 15:08:44 +090020import sys
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070021
22opts = OptionParser()
23opts.add_option('-o', help='path to write WAR to')
24opts.add_option('--lib', action='append', help='target for WEB-INF/lib')
25opts.add_option('--pgmlib', action='append', help='target for WEB-INF/pgm-lib')
Shawn Pearce55583ac2013-10-17 20:29:05 -070026opts.add_option('--tmp', help='temporary directory')
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070027args, ctx = opts.parse_args()
28
Shawn Pearce55583ac2013-10-17 20:29:05 -070029war = args.tmp
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070030root = war[:war.index('buck-out')]
31jars = set()
32
David Pursehouse15a9f532014-06-23 10:55:20 +090033
David Pursehouse6c25ea82013-09-26 16:48:27 +090034def link_jars(libs, directory):
35 makedirs(directory)
David Ostrovskya52eca32014-03-23 19:42:20 -070036 while not path.isfile('.buckconfig'):
37 chdir('..')
David Pursehouse67c263f2014-09-01 11:21:29 +090038 try:
39 cp = check_output(['buck', 'audit', 'classpath'] + libs)
40 except Exception as e:
41 print('call to buck audit failed: %s' % e, file=sys.stderr)
42 exit(1)
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070043 for j in cp.strip().splitlines():
44 if j not in jars:
45 jars.add(j)
46 n = path.basename(j)
47 if j.startswith('buck-out/gen/gerrit-'):
48 n = j.split('/')[2] + '-' + n
David Pursehouse6c25ea82013-09-26 16:48:27 +090049 symlink(path.join(root, j), path.join(directory, n))
Shawn Pearcefd6bb9f2013-05-08 14:14:24 -070050
51if args.lib:
52 link_jars(args.lib, path.join(war, 'WEB-INF', 'lib'))
53if args.pgmlib:
54 link_jars(args.pgmlib, path.join(war, 'WEB-INF', 'pgm-lib'))
David Pursehouse7a16e852013-10-18 15:08:44 +090055try:
56 for s in ctx:
57 check_call(['unzip', '-q', '-d', war, s])
David Pursehouse15a9f532014-06-23 10:55:20 +090058 check_call(['zip', '-9qr', args.o, '.'], cwd=war)
David Pursehouse7a16e852013-10-18 15:08:44 +090059except KeyboardInterrupt:
60 print('Interrupted by user', file=sys.stderr)
Shawn Pearce55583ac2013-10-17 20:29:05 -070061 exit(1)