Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 2 | """ |
| 3 | Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | """ |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 17 | |
| 18 | import re |
| 19 | import os |
| 20 | from zipfile import ZipFile |
| 21 | from tarfile import TarFile, TarInfo |
| 22 | import tarfile |
| 23 | import time |
| 24 | from cStringIO import StringIO |
| 25 | import subprocess |
| 26 | |
| 27 | |
| 28 | written_files = set() |
| 29 | now = time.time() |
| 30 | |
| 31 | def addFile(tar, dest, file, file_size): |
| 32 | if dest not in written_files: |
| 33 | info = TarInfo(dest) |
| 34 | info.size = file_size |
| 35 | info.mtime = now |
| 36 | info.mode = 0777 |
| 37 | tar.addfile(info, fileobj=file) |
| 38 | written_files.add(dest) |
| 39 | |
| 40 | def addString(tar, dest, string): |
| 41 | if dest not in written_files: |
Thomas Vachuska | 41c652c | 2018-06-27 16:01:36 -0700 | [diff] [blame] | 42 | # print dest, string |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 43 | info = TarInfo(dest) |
| 44 | info.size = len(string) |
| 45 | info.mtime = now |
| 46 | info.mode = 0777 |
| 47 | file = StringIO(string) |
| 48 | tar.addfile(info, fileobj=file) |
| 49 | file.close() |
| 50 | written_files.add(dest) |
| 51 | |
| 52 | def getHash(): |
| 53 | p = subprocess.Popen('git rev-parse --verify HEAD --short', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 54 | (output, err) = p.communicate() |
| 55 | return output if p.wait() == 0 else '0000000000' |
| 56 | |
| 57 | def stageOnos(output, version, files=[]): |
| 58 | base = 'onos-%s/' % version |
| 59 | |
| 60 | runtimeVersion = version |
| 61 | if version.endswith('-SNAPSHOT'): |
| 62 | runtimeVersion = version.replace('-SNAPSHOT', '.%s' % getHash()) |
| 63 | |
| 64 | # Note this is not a compressed zip |
| 65 | with tarfile.open(output, 'w:gz') as output: |
| 66 | for file in files: |
| 67 | if '.zip' in file: |
| 68 | with ZipFile(file, 'r') as zip_part: |
| 69 | for f in zip_part.infolist(): |
| 70 | dest = f.filename |
| 71 | if base not in dest: |
| 72 | dest = base + 'apache-karaf-3.0.8/system/' + f.filename |
| 73 | addFile(output, dest, zip_part.open(f), f.file_size) |
| 74 | elif '.oar' in file: |
| 75 | with ZipFile(file, 'r') as oar: |
| 76 | app_xml = oar.open('app.xml').read() |
| 77 | app_name = re.search('name="([^"]+)"', app_xml).group(1) |
| 78 | dest = base + 'apps/%(name)s/%(name)s.oar' % { 'name': app_name} |
| 79 | addFile(output, dest, open(file), os.stat(file).st_size) |
| 80 | dest = base + 'apps/%s/app.xml' % app_name |
| 81 | addString(output, dest, app_xml) |
| 82 | for f in oar.infolist(): |
| 83 | filename = f.filename |
| 84 | if 'm2' in filename: |
| 85 | dest = base + 'apache-karaf-3.0.8/system/' + filename[3:] |
| 86 | if dest not in written_files: |
| 87 | addFile(output, dest, oar.open(f), f.file_size) |
| 88 | written_files.add(dest) |
| 89 | elif 'features.xml' in file: |
| 90 | dest = base + 'apache-karaf-3.0.8/system/org/onosproject/onos-features/%s/' % version |
| 91 | dest += 'onos-features-%s-features.xml' % version |
| 92 | with open(file) as f: |
| 93 | addFile(output, dest, f, os.stat(file).st_size) |
| 94 | addString(output, base + 'apps/org.onosproject.drivers/active', '') |
| 95 | addString(output, base + 'VERSION', runtimeVersion) |
| 96 | |
| 97 | if __name__ == '__main__': |
| 98 | import sys |
| 99 | |
| 100 | if len(sys.argv) < 3: |
| 101 | print 'USAGE' #FIXME |
| 102 | sys.exit(1) |
| 103 | |
| 104 | output = sys.argv[1] |
| 105 | version = sys.argv[2] |
| 106 | args = sys.argv[3:] |
| 107 | |
| 108 | stageOnos(output, version, args) |