blob: 40e944adb8e8c474a00f727450f8a1a54816df88 [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 2018-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
Thomas Vachuskae8f06892018-06-12 15:54:49 -070018from zipfile import ZipFile, ZipInfo
Ray Milkeyad83ef92018-06-05 11:05:51 -070019
Thomas Vachuska8e022a92018-07-10 14:47:38 -070020# Utility to write out the ZIP bundle containing the artifacts required by an
21# OSGI feature.
22
23def writeFeatureBundle(output, files=[]):
Ray Milkeyad83ef92018-06-05 11:05:51 -070024 with ZipFile(output, 'w') as zip:
25 for file, mvnCoords in files:
26 filename = file.split('/')[-1]
27 if mvnCoords == 'NONE':
28 dest = filename
29 else:
30 parts = mvnCoords.split(':')
Thomas Vachuskae8f06892018-06-12 15:54:49 -070031 if len(parts) > 4:
32 parts.insert(3, parts.pop()) # move version to the 3rd position
33 groupId, artifactId, version = parts[1:4]
Ray Milkeyad83ef92018-06-05 11:05:51 -070034 groupId = groupId.replace('.', '/')
35 extension = filename.split('.')[-1]
36 if extension == 'jar':
37 filename = '%s-%s.jar' % ( artifactId, version )
38 elif 'features.xml' in filename:
39 filename = '%s-%s-features.xml' % ( artifactId, version )
40 dest = '%s/%s/%s/%s' % ( groupId, artifactId, version, filename )
Thomas Vachuskae8f06892018-06-12 15:54:49 -070041 f = open(file, 'rb')
42 zip.writestr(ZipInfo(dest, date_time=(1980, 1, 1, 0, 0, 0)), f.read())
43 f.close()
Ray Milkeyad83ef92018-06-05 11:05:51 -070044
45if __name__ == '__main__':
46 import sys
47
48 if len(sys.argv) < 2:
49 print 'USAGE'
50 sys.exit(1)
51
52 output = sys.argv[1]
53 args = sys.argv[2:]
54
55 if len(args) % 2 != 0:
56 print 'There must be an even number of args: file mvn_coords'
57 sys.exit(2)
58
59 files = zip(*[iter(args)]*2)
Thomas Vachuska8e022a92018-07-10 14:47:38 -070060 writeFeatureBundle(output, files)