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 |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 24 | from io import BytesIO |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 25 | import subprocess |
| 26 | |
| 27 | |
| 28 | written_files = set() |
| 29 | now = time.time() |
Ray Milkey | 5f11a07 | 2019-06-28 14:10:19 -0700 | [diff] [blame] | 30 | karaf_version = "4.2.6" |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | karaf_system = "apache-karaf-" + karaf_version + "/system/" |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 32 | |
| 33 | def addFile(tar, dest, file, file_size): |
| 34 | if dest not in written_files: |
| 35 | info = TarInfo(dest) |
| 36 | info.size = file_size |
| 37 | info.mtime = now |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 38 | info.mode = 0o777 |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 39 | tar.addfile(info, fileobj=file) |
| 40 | written_files.add(dest) |
| 41 | |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 42 | def addBytes(tar, dest, bytes): |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 43 | if dest not in written_files: |
Thomas Vachuska | 41c652c | 2018-06-27 16:01:36 -0700 | [diff] [blame] | 44 | # print dest, string |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 45 | info = TarInfo(dest) |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 46 | info.size = len(bytes) |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 47 | info.mtime = now |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 48 | info.mode = 0o777 |
| 49 | file = BytesIO(bytes) |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 50 | tar.addfile(info, fileobj=file) |
| 51 | file.close() |
| 52 | written_files.add(dest) |
| 53 | |
| 54 | def getHash(): |
| 55 | p = subprocess.Popen('git rev-parse --verify HEAD --short', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 56 | (output, err) = p.communicate() |
| 57 | return output if p.wait() == 0 else '0000000000' |
| 58 | |
| 59 | def stageOnos(output, version, files=[]): |
| 60 | base = 'onos-%s/' % version |
| 61 | |
| 62 | runtimeVersion = version |
| 63 | if version.endswith('-SNAPSHOT'): |
| 64 | runtimeVersion = version.replace('-SNAPSHOT', '.%s' % getHash()) |
| 65 | |
| 66 | # Note this is not a compressed zip |
| 67 | with tarfile.open(output, 'w:gz') as output: |
| 68 | for file in files: |
| 69 | if '.zip' in file: |
| 70 | with ZipFile(file, 'r') as zip_part: |
| 71 | for f in zip_part.infolist(): |
| 72 | dest = f.filename |
| 73 | if base not in dest: |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 74 | dest = base + karaf_system + f.filename |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 75 | addFile(output, dest, zip_part.open(f), f.file_size) |
| 76 | elif '.oar' in file: |
| 77 | with ZipFile(file, 'r') as oar: |
| 78 | app_xml = oar.open('app.xml').read() |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 79 | app_name = re.search(b'name="([^"]+)"', app_xml).group(1).decode('utf-8') |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 80 | dest = base + 'apps/%(name)s/%(name)s.oar' % { 'name': app_name} |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 81 | addFile(output, dest, open(file, 'rb'), os.stat(file).st_size) |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 82 | dest = base + 'apps/%s/app.xml' % app_name |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 83 | addBytes(output, dest, app_xml) |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 84 | for f in oar.infolist(): |
| 85 | filename = f.filename |
| 86 | if 'm2' in filename: |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 87 | dest = base + karaf_system + filename[3:] |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 88 | if dest not in written_files: |
| 89 | addFile(output, dest, oar.open(f), f.file_size) |
| 90 | written_files.add(dest) |
| 91 | elif 'features.xml' in file: |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 92 | dest = base + karaf_system + 'org/onosproject/onos-features/%s/' % version |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 93 | dest += 'onos-features-%s-features.xml' % version |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 94 | with open(file, 'rb') as f: |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 95 | addFile(output, dest, f, os.stat(file).st_size) |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 96 | addBytes(output, base + 'apps/org.onosproject.drivers/active', b'') |
| 97 | addBytes(output, base + 'VERSION', runtimeVersion.encode('utf-8')) |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 98 | |
| 99 | if __name__ == '__main__': |
| 100 | import sys |
| 101 | |
| 102 | if len(sys.argv) < 3: |
Laszlo Papp | 4884410 | 2018-09-18 09:33:42 +0000 | [diff] [blame] | 103 | print('USAGE') #FIXME |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 104 | sys.exit(1) |
| 105 | |
| 106 | output = sys.argv[1] |
| 107 | version = sys.argv[2] |
| 108 | args = sys.argv[3:] |
| 109 | |
| 110 | stageOnos(output, version, args) |