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 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 Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 18 | from zipfile import ZipFile, ZipInfo |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 19 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 20 | # Utility to write out the ZIP bundle containing the artifacts required by an |
| 21 | # OSGI feature. |
| 22 | |
| 23 | def writeFeatureBundle(output, files=[]): |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 24 | 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 Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 31 | if len(parts) > 4: |
| 32 | parts.insert(3, parts.pop()) # move version to the 3rd position |
| 33 | groupId, artifactId, version = parts[1:4] |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 34 | 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 Vachuska | e8f0689 | 2018-06-12 15:54:49 -0700 | [diff] [blame] | 41 | f = open(file, 'rb') |
| 42 | zip.writestr(ZipInfo(dest, date_time=(1980, 1, 1, 0, 0, 0)), f.read()) |
| 43 | f.close() |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 44 | |
| 45 | if __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 Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 60 | writeFeatureBundle(output, files) |