Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [diff] [blame] | 1 | #!/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 | |
| 16 | from __future__ import print_function |
| 17 | from optparse import OptionParser |
| 18 | from os import makedirs, path, symlink |
| 19 | from subprocess import check_call |
| 20 | import sys |
| 21 | |
| 22 | opts = OptionParser() |
| 23 | opts.add_option('-o', help='path to write WAR to') |
| 24 | opts.add_option('--lib', action='append', help='target for WEB-INF/lib') |
| 25 | opts.add_option('--pgmlib', action='append', help='target for WEB-INF/pgm-lib') |
| 26 | opts.add_option('--tmp', help='temporary directory') |
| 27 | args, ctx = opts.parse_args() |
| 28 | |
| 29 | war = args.tmp |
| 30 | jars = set() |
| 31 | basenames = set() |
| 32 | |
| 33 | def prune(l): |
| 34 | return [j for e in l for j in e.split(':')] |
| 35 | |
| 36 | def 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 | |
| 55 | if args.lib: |
| 56 | link_jars(prune(args.lib), path.join(war, 'WEB-INF', 'lib')) |
| 57 | if args.pgmlib: |
| 58 | link_jars(prune(args.pgmlib), path.join(war, 'WEB-INF', 'pgm-lib')) |
| 59 | try: |
| 60 | for s in ctx: |
| 61 | check_call(['unzip', '-q', '-d', war, s]) |
| 62 | check_call(['zip', '-9qr', args.o, '.'], cwd=war) |
| 63 | except KeyboardInterrupt: |
| 64 | print('Interrupted by user', file=sys.stderr) |
| 65 | exit(1) |