Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | #FIXME Add license |
| 3 | |
| 4 | import re |
| 5 | import os |
| 6 | from zipfile import ZipFile |
| 7 | from tarfile import TarFile, TarInfo |
| 8 | import tarfile |
| 9 | import time |
| 10 | from cStringIO import StringIO |
| 11 | import subprocess |
| 12 | |
| 13 | |
| 14 | written_files = set() |
| 15 | now = time.time() |
| 16 | |
| 17 | def addFile(tar, dest, file, file_size): |
| 18 | if dest not in written_files: |
| 19 | info = TarInfo(dest) |
| 20 | info.size = file_size |
| 21 | info.mtime = now |
| 22 | info.mode = 0777 |
| 23 | tar.addfile(info, fileobj=file) |
| 24 | written_files.add(dest) |
| 25 | |
| 26 | def addString(tar, dest, string): |
| 27 | if dest not in written_files: |
| 28 | print dest, string |
| 29 | info = TarInfo(dest) |
| 30 | info.size = len(string) |
| 31 | info.mtime = now |
| 32 | info.mode = 0777 |
| 33 | file = StringIO(string) |
| 34 | tar.addfile(info, fileobj=file) |
| 35 | file.close() |
| 36 | written_files.add(dest) |
| 37 | |
| 38 | def getHash(): |
| 39 | p = subprocess.Popen('git rev-parse --verify HEAD --short', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 40 | (output, err) = p.communicate() |
| 41 | return output if p.wait() == 0 else '0000000000' |
| 42 | |
| 43 | def stageOnos(output, version, files=[]): |
| 44 | base = 'onos-%s/' % version |
| 45 | |
| 46 | runtimeVersion = version |
| 47 | if version.endswith('-SNAPSHOT'): |
| 48 | runtimeVersion = version.replace('-SNAPSHOT', '.%s' % getHash()) |
| 49 | |
| 50 | # Note this is not a compressed zip |
| 51 | with tarfile.open(output, 'w:gz') as output: |
| 52 | for file in files: |
| 53 | if '.zip' in file: |
| 54 | with ZipFile(file, 'r') as zip_part: |
| 55 | for f in zip_part.infolist(): |
| 56 | dest = f.filename |
| 57 | if base not in dest: |
| 58 | dest = base + 'apache-karaf-3.0.8/system/' + f.filename |
| 59 | addFile(output, dest, zip_part.open(f), f.file_size) |
| 60 | elif '.oar' in file: |
| 61 | with ZipFile(file, 'r') as oar: |
| 62 | app_xml = oar.open('app.xml').read() |
| 63 | app_name = re.search('name="([^"]+)"', app_xml).group(1) |
| 64 | dest = base + 'apps/%(name)s/%(name)s.oar' % { 'name': app_name} |
| 65 | addFile(output, dest, open(file), os.stat(file).st_size) |
| 66 | dest = base + 'apps/%s/app.xml' % app_name |
| 67 | addString(output, dest, app_xml) |
| 68 | for f in oar.infolist(): |
| 69 | filename = f.filename |
| 70 | if 'm2' in filename: |
| 71 | dest = base + 'apache-karaf-3.0.8/system/' + filename[3:] |
| 72 | if dest not in written_files: |
| 73 | addFile(output, dest, oar.open(f), f.file_size) |
| 74 | written_files.add(dest) |
| 75 | elif 'features.xml' in file: |
| 76 | dest = base + 'apache-karaf-3.0.8/system/org/onosproject/onos-features/%s/' % version |
| 77 | dest += 'onos-features-%s-features.xml' % version |
| 78 | with open(file) as f: |
| 79 | addFile(output, dest, f, os.stat(file).st_size) |
| 80 | addString(output, base + 'apps/org.onosproject.drivers/active', '') |
| 81 | addString(output, base + 'VERSION', runtimeVersion) |
| 82 | |
| 83 | if __name__ == '__main__': |
| 84 | import sys |
| 85 | |
| 86 | if len(sys.argv) < 3: |
| 87 | print 'USAGE' #FIXME |
| 88 | sys.exit(1) |
| 89 | |
| 90 | output = sys.argv[1] |
| 91 | version = sys.argv[2] |
| 92 | args = sys.argv[3:] |
| 93 | |
| 94 | stageOnos(output, version, args) |