blob: a9f519fedd340ad7b4a6fd65d6811a9ea398158c [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()
30
31def 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
Laszlo Papp48844102018-09-18 09:33:42 +000036 info.mode = 0o777
Ray Milkeyad83ef92018-06-05 11:05:51 -070037 tar.addfile(info, fileobj=file)
38 written_files.add(dest)
39
Laszlo Papp48844102018-09-18 09:33:42 +000040def addBytes(tar, dest, bytes):
Ray Milkeyad83ef92018-06-05 11:05:51 -070041 if dest not in written_files:
Thomas Vachuska41c652c2018-06-27 16:01:36 -070042 # print dest, string
Ray Milkeyad83ef92018-06-05 11:05:51 -070043 info = TarInfo(dest)
Laszlo Papp48844102018-09-18 09:33:42 +000044 info.size = len(bytes)
Ray Milkeyad83ef92018-06-05 11:05:51 -070045 info.mtime = now
Laszlo Papp48844102018-09-18 09:33:42 +000046 info.mode = 0o777
47 file = BytesIO(bytes)
Ray Milkeyad83ef92018-06-05 11:05:51 -070048 tar.addfile(info, fileobj=file)
49 file.close()
50 written_files.add(dest)
51
52def 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
57def 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()
Laszlo Papp48844102018-09-18 09:33:42 +000077 app_name = re.search(b'name="([^"]+)"', app_xml).group(1).decode('utf-8')
Ray Milkeyad83ef92018-06-05 11:05:51 -070078 dest = base + 'apps/%(name)s/%(name)s.oar' % { 'name': app_name}
Laszlo Papp48844102018-09-18 09:33:42 +000079 addFile(output, dest, open(file, 'rb'), os.stat(file).st_size)
Ray Milkeyad83ef92018-06-05 11:05:51 -070080 dest = base + 'apps/%s/app.xml' % app_name
Laszlo Papp48844102018-09-18 09:33:42 +000081 addBytes(output, dest, app_xml)
Ray Milkeyad83ef92018-06-05 11:05:51 -070082 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
Laszlo Papp48844102018-09-18 09:33:42 +000092 with open(file, 'rb') as f:
Ray Milkeyad83ef92018-06-05 11:05:51 -070093 addFile(output, dest, f, os.stat(file).st_size)
Laszlo Papp48844102018-09-18 09:33:42 +000094 addBytes(output, base + 'apps/org.onosproject.drivers/active', b'')
95 addBytes(output, base + 'VERSION', runtimeVersion.encode('utf-8'))
Ray Milkeyad83ef92018-06-05 11:05:51 -070096
97if __name__ == '__main__':
98 import sys
99
100 if len(sys.argv) < 3:
Laszlo Papp48844102018-09-18 09:33:42 +0000101 print('USAGE') #FIXME
Ray Milkeyad83ef92018-06-05 11:05:51 -0700102 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)