Brian O'Connor | 0167bd4 | 2016-04-29 17:09:10 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | #FIXME Add license |
| 3 | |
| 4 | import re |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 5 | import os |
Brian O'Connor | 0167bd4 | 2016-04-29 17:09:10 -0700 | [diff] [blame] | 6 | from zipfile import ZipFile |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 7 | from tarfile import TarFile, TarInfo |
Brian O'Connor | 65c0bdf | 2016-05-03 18:40:17 -0700 | [diff] [blame] | 8 | import tarfile |
| 9 | import time |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 10 | from cStringIO import StringIO |
| 11 | |
Brian O'Connor | e642f7c | 2016-05-23 18:33:04 -0700 | [diff] [blame] | 12 | VERSION = '1.7.0' #FIXME version, and maybe git commit hash |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 13 | BASE = 'onos-%s/' % VERSION |
| 14 | |
| 15 | |
| 16 | written_files = set() |
Brian O'Connor | 65c0bdf | 2016-05-03 18:40:17 -0700 | [diff] [blame] | 17 | now = time.time() |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 18 | |
| 19 | def addFile(tar, dest, file, file_size): |
| 20 | if dest not in written_files: |
| 21 | info = TarInfo(dest) |
| 22 | info.size = file_size |
Brian O'Connor | 65c0bdf | 2016-05-03 18:40:17 -0700 | [diff] [blame] | 23 | info.mtime = now |
| 24 | info.mode = 0777 |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 25 | tar.addfile(info, fileobj=file) |
| 26 | written_files.add(dest) |
| 27 | |
| 28 | def addString(tar, dest, string): |
| 29 | if dest not in written_files: |
| 30 | print dest, string |
| 31 | info = TarInfo(dest) |
| 32 | info.size = len(string) |
Brian O'Connor | 65c0bdf | 2016-05-03 18:40:17 -0700 | [diff] [blame] | 33 | info.mtime = now |
| 34 | info.mode = 0777 |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 35 | file = StringIO(string) |
| 36 | tar.addfile(info, fileobj=file) |
| 37 | file.close() |
| 38 | written_files.add(dest) |
Brian O'Connor | 0167bd4 | 2016-04-29 17:09:10 -0700 | [diff] [blame] | 39 | |
| 40 | def stageOnos(output, files=[]): |
| 41 | # Note this is not a compressed zip |
Brian O'Connor | 65c0bdf | 2016-05-03 18:40:17 -0700 | [diff] [blame] | 42 | with tarfile.open(output, 'w:gz') as output: |
Brian O'Connor | 0167bd4 | 2016-04-29 17:09:10 -0700 | [diff] [blame] | 43 | for file in files: |
| 44 | if '.zip' in file: |
| 45 | with ZipFile(file, 'r') as zip_part: |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 46 | for f in zip_part.infolist(): |
Brian O'Connor | 65c0bdf | 2016-05-03 18:40:17 -0700 | [diff] [blame] | 47 | dest = f.filename |
| 48 | if BASE not in dest: |
| 49 | dest = BASE + 'apache-karaf-3.0.5/system/' + f.filename |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 50 | addFile(output, dest, zip_part.open(f), f.file_size) |
Brian O'Connor | 0167bd4 | 2016-04-29 17:09:10 -0700 | [diff] [blame] | 51 | elif '.oar' in file: |
| 52 | with ZipFile(file, 'r') as oar: |
| 53 | app_xml = oar.open('app.xml').read() |
| 54 | app_name = re.search('name="([^"]+)"', app_xml).group(1) |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 55 | dest = BASE + 'apps/%(name)s/%(name)s.oar' % { 'name': app_name} |
| 56 | addFile(output, dest, open(file), os.stat(file).st_size) |
| 57 | dest = BASE + 'apps/%s/app.xml' % app_name |
| 58 | addString(output, dest, app_xml) |
| 59 | for f in oar.infolist(): |
| 60 | filename = f.filename |
| 61 | if 'm2' in filename: |
| 62 | dest = BASE + 'apache-karaf-3.0.5/system/' + filename[3:] |
Brian O'Connor | 0167bd4 | 2016-04-29 17:09:10 -0700 | [diff] [blame] | 63 | if dest not in written_files: |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 64 | addFile(output, dest, oar.open(f), f.file_size) |
Brian O'Connor | 0167bd4 | 2016-04-29 17:09:10 -0700 | [diff] [blame] | 65 | written_files.add(dest) |
| 66 | elif 'features.xml' in file: |
Brian O'Connor | e642f7c | 2016-05-23 18:33:04 -0700 | [diff] [blame] | 67 | dest = BASE + 'apache-karaf-3.0.5/system/org/onosproject/onos-features/1.7.0-SNAPSHOT/' |
| 68 | dest += 'onos-features-1.7.0-SNAPSHOT-features.xml' |
Brian O'Connor | 0167bd4 | 2016-04-29 17:09:10 -0700 | [diff] [blame] | 69 | with open(file) as f: |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 70 | addFile(output, dest, f, os.stat(file).st_size) |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 71 | addString(output, BASE + 'apps/org.onosproject.drivers/active', '') |
Brian O'Connor | 92ec213 | 2016-05-03 17:30:25 -0700 | [diff] [blame] | 72 | addString(output, BASE + 'VERSION', VERSION) |
Brian O'Connor | 0167bd4 | 2016-04-29 17:09:10 -0700 | [diff] [blame] | 73 | |
| 74 | if __name__ == '__main__': |
| 75 | import sys |
| 76 | |
| 77 | if len(sys.argv) < 2: |
| 78 | print 'USAGE' |
| 79 | sys.exit(1) |
| 80 | |
| 81 | output = sys.argv[1] |
| 82 | args = sys.argv[2:] |
| 83 | |
Brian O'Connor | 0167bd4 | 2016-04-29 17:09:10 -0700 | [diff] [blame] | 84 | stageOnos(output, args) |