Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 1 | #!/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 Pursehouse | 7a16e85 | 2013-10-18 15:08:44 +0900 | [diff] [blame] | 16 | from __future__ import print_function |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 17 | from optparse import OptionParser |
David Ostrovsky | a52eca3 | 2014-03-23 19:42:20 -0700 | [diff] [blame] | 18 | from os import getcwd, chdir, makedirs, path, symlink |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 19 | from subprocess import check_call, check_output |
David Pursehouse | 7a16e85 | 2013-10-18 15:08:44 +0900 | [diff] [blame] | 20 | import sys |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 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') |
Shawn Pearce | 55583ac | 2013-10-17 20:29:05 -0700 | [diff] [blame] | 26 | opts.add_option('--tmp', help='temporary directory') |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 27 | args, ctx = opts.parse_args() |
| 28 | |
Shawn Pearce | 55583ac | 2013-10-17 20:29:05 -0700 | [diff] [blame] | 29 | war = args.tmp |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 30 | root = war[:war.index('buck-out')] |
| 31 | jars = set() |
| 32 | |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 33 | |
David Pursehouse | 6c25ea8 | 2013-09-26 16:48:27 +0900 | [diff] [blame] | 34 | def link_jars(libs, directory): |
| 35 | makedirs(directory) |
David Ostrovsky | a52eca3 | 2014-03-23 19:42:20 -0700 | [diff] [blame] | 36 | while not path.isfile('.buckconfig'): |
| 37 | chdir('..') |
David Pursehouse | 67c263f | 2014-09-01 11:21:29 +0900 | [diff] [blame] | 38 | 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 Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 43 | 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 Pursehouse | 6c25ea8 | 2013-09-26 16:48:27 +0900 | [diff] [blame] | 49 | symlink(path.join(root, j), path.join(directory, n)) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 50 | |
| 51 | if args.lib: |
| 52 | link_jars(args.lib, path.join(war, 'WEB-INF', 'lib')) |
| 53 | if args.pgmlib: |
| 54 | link_jars(args.pgmlib, path.join(war, 'WEB-INF', 'pgm-lib')) |
David Pursehouse | 7a16e85 | 2013-10-18 15:08:44 +0900 | [diff] [blame] | 55 | try: |
| 56 | for s in ctx: |
| 57 | check_call(['unzip', '-q', '-d', war, s]) |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 58 | check_call(['zip', '-9qr', args.o, '.'], cwd=war) |
David Pursehouse | 7a16e85 | 2013-10-18 15:08:44 +0900 | [diff] [blame] | 59 | except KeyboardInterrupt: |
| 60 | print('Interrupted by user', file=sys.stderr) |
Shawn Pearce | 55583ac | 2013-10-17 20:29:05 -0700 | [diff] [blame] | 61 | exit(1) |