blob: e11a3faae29235baf0b0e69e17dab2296cfc697f [file] [log] [blame]
Ray Milkeyad83ef92018-06-05 11:05:51 -07001#!/usr/bin/env python
Thomas Vachuska8e022a92018-07-10 14:47:38 -07002"""
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 Milkeyad83ef92018-06-05 11:05:51 -070017
18import re
19import os
20from zipfile import ZipFile
21from tarfile import TarFile, TarInfo
22import tarfile
23import time
Laszlo Papp48844102018-09-18 09:33:42 +000024from io import BytesIO
Ray Milkeyad83ef92018-06-05 11:05:51 -070025import subprocess
26
27
28written_files = set()
29now = time.time()
Daniele Morocf425012020-01-31 16:25:57 -080030karaf_version = "4.2.8"
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031karaf_system = "apache-karaf-" + karaf_version + "/system/"
Ray Milkeyad83ef92018-06-05 11:05:51 -070032
33def 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 Papp48844102018-09-18 09:33:42 +000038 info.mode = 0o777
Ray Milkeyad83ef92018-06-05 11:05:51 -070039 tar.addfile(info, fileobj=file)
40 written_files.add(dest)
41
Laszlo Papp48844102018-09-18 09:33:42 +000042def addBytes(tar, dest, bytes):
Ray Milkeyad83ef92018-06-05 11:05:51 -070043 if dest not in written_files:
Thomas Vachuska41c652c2018-06-27 16:01:36 -070044 # print dest, string
Ray Milkeyad83ef92018-06-05 11:05:51 -070045 info = TarInfo(dest)
Laszlo Papp48844102018-09-18 09:33:42 +000046 info.size = len(bytes)
Ray Milkeyad83ef92018-06-05 11:05:51 -070047 info.mtime = now
Laszlo Papp48844102018-09-18 09:33:42 +000048 info.mode = 0o777
49 file = BytesIO(bytes)
Ray Milkeyad83ef92018-06-05 11:05:51 -070050 tar.addfile(info, fileobj=file)
51 file.close()
52 written_files.add(dest)
53
54def 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
59def 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 Milkeyd84f89b2018-08-17 14:54:17 -070074 dest = base + karaf_system + f.filename
Ray Milkeyad83ef92018-06-05 11:05:51 -070075 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 Papp48844102018-09-18 09:33:42 +000079 app_name = re.search(b'name="([^"]+)"', app_xml).group(1).decode('utf-8')
Ray Milkeyad83ef92018-06-05 11:05:51 -070080 dest = base + 'apps/%(name)s/%(name)s.oar' % { 'name': app_name}
Laszlo Papp48844102018-09-18 09:33:42 +000081 addFile(output, dest, open(file, 'rb'), os.stat(file).st_size)
Ray Milkeyad83ef92018-06-05 11:05:51 -070082 dest = base + 'apps/%s/app.xml' % app_name
Laszlo Papp48844102018-09-18 09:33:42 +000083 addBytes(output, dest, app_xml)
Ray Milkeyad83ef92018-06-05 11:05:51 -070084 for f in oar.infolist():
85 filename = f.filename
86 if 'm2' in filename:
Ray Milkeyd84f89b2018-08-17 14:54:17 -070087 dest = base + karaf_system + filename[3:]
Ray Milkeyad83ef92018-06-05 11:05:51 -070088 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 Milkeyd84f89b2018-08-17 14:54:17 -070092 dest = base + karaf_system + 'org/onosproject/onos-features/%s/' % version
Ray Milkeyad83ef92018-06-05 11:05:51 -070093 dest += 'onos-features-%s-features.xml' % version
Laszlo Papp48844102018-09-18 09:33:42 +000094 with open(file, 'rb') as f:
Ray Milkeyad83ef92018-06-05 11:05:51 -070095 addFile(output, dest, f, os.stat(file).st_size)
Laszlo Papp48844102018-09-18 09:33:42 +000096 addBytes(output, base + 'apps/org.onosproject.drivers/active', b'')
97 addBytes(output, base + 'VERSION', runtimeVersion.encode('utf-8'))
Ray Milkeyad83ef92018-06-05 11:05:51 -070098
99if __name__ == '__main__':
100 import sys
101
102 if len(sys.argv) < 3:
Laszlo Papp48844102018-09-18 09:33:42 +0000103 print('USAGE') #FIXME
Ray Milkeyad83ef92018-06-05 11:05:51 -0700104 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)