blob: 2d9462fd97571170ccab91f6d8dad2f1d0d818b9 [file] [log] [blame]
Brian O'Connor0167bd42016-04-29 17:09:10 -07001#!/usr/bin/env python
2#FIXME Add license
3
4import re
Brian O'Connor92ec2132016-05-03 17:30:25 -07005import os
Brian O'Connor0167bd42016-04-29 17:09:10 -07006from zipfile import ZipFile
Brian O'Connor92ec2132016-05-03 17:30:25 -07007from tarfile import TarFile, TarInfo
Brian O'Connor65c0bdf2016-05-03 18:40:17 -07008import tarfile
9import time
Brian O'Connor92ec2132016-05-03 17:30:25 -070010from cStringIO import StringIO
11
Brian O'Connore642f7c2016-05-23 18:33:04 -070012VERSION = '1.7.0' #FIXME version, and maybe git commit hash
Brian O'Connor92ec2132016-05-03 17:30:25 -070013BASE = 'onos-%s/' % VERSION
14
15
16written_files = set()
Brian O'Connor65c0bdf2016-05-03 18:40:17 -070017now = time.time()
Brian O'Connor92ec2132016-05-03 17:30:25 -070018
19def addFile(tar, dest, file, file_size):
20 if dest not in written_files:
21 info = TarInfo(dest)
22 info.size = file_size
Brian O'Connor65c0bdf2016-05-03 18:40:17 -070023 info.mtime = now
24 info.mode = 0777
Brian O'Connor92ec2132016-05-03 17:30:25 -070025 tar.addfile(info, fileobj=file)
26 written_files.add(dest)
27
28def addString(tar, dest, string):
29 if dest not in written_files:
30 print dest, string
31 info = TarInfo(dest)
32 info.size = len(string)
Brian O'Connor65c0bdf2016-05-03 18:40:17 -070033 info.mtime = now
34 info.mode = 0777
Brian O'Connor92ec2132016-05-03 17:30:25 -070035 file = StringIO(string)
36 tar.addfile(info, fileobj=file)
37 file.close()
38 written_files.add(dest)
Brian O'Connor0167bd42016-04-29 17:09:10 -070039
40def stageOnos(output, files=[]):
41 # Note this is not a compressed zip
Brian O'Connor65c0bdf2016-05-03 18:40:17 -070042 with tarfile.open(output, 'w:gz') as output:
Brian O'Connor0167bd42016-04-29 17:09:10 -070043 for file in files:
44 if '.zip' in file:
45 with ZipFile(file, 'r') as zip_part:
Brian O'Connor92ec2132016-05-03 17:30:25 -070046 for f in zip_part.infolist():
Brian O'Connor65c0bdf2016-05-03 18:40:17 -070047 dest = f.filename
48 if BASE not in dest:
49 dest = BASE + 'apache-karaf-3.0.5/system/' + f.filename
Brian O'Connor92ec2132016-05-03 17:30:25 -070050 addFile(output, dest, zip_part.open(f), f.file_size)
Brian O'Connor0167bd42016-04-29 17:09:10 -070051 elif '.oar' in file:
52 with ZipFile(file, 'r') as oar:
53 app_xml = oar.open('app.xml').read()
54 app_name = re.search('name="([^"]+)"', app_xml).group(1)
Brian O'Connor92ec2132016-05-03 17:30:25 -070055 dest = BASE + 'apps/%(name)s/%(name)s.oar' % { 'name': app_name}
56 addFile(output, dest, open(file), os.stat(file).st_size)
57 dest = BASE + 'apps/%s/app.xml' % app_name
58 addString(output, dest, app_xml)
59 for f in oar.infolist():
60 filename = f.filename
61 if 'm2' in filename:
62 dest = BASE + 'apache-karaf-3.0.5/system/' + filename[3:]
Brian O'Connor0167bd42016-04-29 17:09:10 -070063 if dest not in written_files:
Brian O'Connor92ec2132016-05-03 17:30:25 -070064 addFile(output, dest, oar.open(f), f.file_size)
Brian O'Connor0167bd42016-04-29 17:09:10 -070065 written_files.add(dest)
66 elif 'features.xml' in file:
Brian O'Connore642f7c2016-05-23 18:33:04 -070067 dest = BASE + 'apache-karaf-3.0.5/system/org/onosproject/onos-features/1.7.0-SNAPSHOT/'
68 dest += 'onos-features-1.7.0-SNAPSHOT-features.xml'
Brian O'Connor0167bd42016-04-29 17:09:10 -070069 with open(file) as f:
Brian O'Connor92ec2132016-05-03 17:30:25 -070070 addFile(output, dest, f, os.stat(file).st_size)
Brian O'Connor92ec2132016-05-03 17:30:25 -070071 addString(output, BASE + 'apps/org.onosproject.drivers/active', '')
Brian O'Connor92ec2132016-05-03 17:30:25 -070072 addString(output, BASE + 'VERSION', VERSION)
Brian O'Connor0167bd42016-04-29 17:09:10 -070073
74if __name__ == '__main__':
75 import sys
76
77 if len(sys.argv) < 2:
78 print 'USAGE'
79 sys.exit(1)
80
81 output = sys.argv[1]
82 args = sys.argv[2:]
83
Brian O'Connor0167bd42016-04-29 17:09:10 -070084 stageOnos(output, args)