blob: cd836a81a34556c04fee084dda7f793a725c5421 [file] [log] [blame]
Ray Milkeyf80bbb22016-03-11 10:16:22 -08001#!/usr/bin/env 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
16from __future__ import print_function
17from optparse import OptionParser
18from os import makedirs, path, symlink
19from subprocess import check_call
20import sys
21
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')
26opts.add_option('--tmp', help='temporary directory')
27args, ctx = opts.parse_args()
28
29war = args.tmp
30jars = set()
31basenames = set()
32
33def prune(l):
34 return [j for e in l for j in e.split(':')]
35
36def link_jars(libs, directory):
37 makedirs(directory)
38 for j in libs:
39 if j not in jars:
40 # When jgit is consumed from its own cell,
41 # potential duplicates should be filtered.
42 # e.g. jsch.jar will be reached through:
43 # 1. /home/username/projects/gerrit/buck-out/gen/lib/jsch.jar
44 # 2. /home/username/projects/jgit/buck-out/gen/lib/jsch.jar
45 if (j.find('jgit/buck-out/gen/lib') > 0
46 and path.basename(j) in basenames):
47 continue
48 jars.add(j)
49 n = path.basename(j)
50 if j.find('buck-out/gen/gerrit-') > 0:
51 n = j[j.find('buck-out'):].split('/')[2] + '-' + n
52 basenames.add(n)
53 symlink(j, path.join(directory, n))
54
55if args.lib:
56 link_jars(prune(args.lib), path.join(war, 'WEB-INF', 'lib'))
57if args.pgmlib:
58 link_jars(prune(args.pgmlib), path.join(war, 'WEB-INF', 'pgm-lib'))
59try:
60 for s in ctx:
61 check_call(['unzip', '-q', '-d', war, s])
62 check_call(['zip', '-9qr', args.o, '.'], cwd=war)
63except KeyboardInterrupt:
64 print('Interrupted by user', file=sys.stderr)
65 exit(1)