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 | |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 18 | from zipfile import ZipFile, ZipInfo |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 19 | |
| 20 | # Utility to write out the ONOS OAR file bundle containing the artifacts |
| 21 | # required to install and activate an ONOS application. |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 22 | |
| 23 | def generateOar(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: |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 26 | mvnCoords = mvnCoords.replace("mvn:", "") |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 27 | filename = file.split('/')[-1] |
| 28 | if mvnCoords == 'NONE': |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 29 | if 'app-xml.xml' in filename: |
| 30 | dest = 'app.xml' |
| 31 | else: |
| 32 | dest = filename |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 33 | else: |
| 34 | parts = mvnCoords.split(':') |
| 35 | if len(parts) > 3: |
| 36 | parts.insert(2, parts.pop()) # move version to the 3rd position |
| 37 | groupId, artifactId, version = parts[0:3] |
| 38 | groupId = groupId.replace('.', '/') |
| 39 | extension = filename.split('.')[-1] |
| 40 | if extension == 'jar': |
| 41 | filename = '%s-%s.jar' % ( artifactId, version ) |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 42 | elif 'feature-xml' in filename: |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 43 | filename = '%s-%s-features.xml' % ( artifactId, version ) |
| 44 | dest = 'm2/%s/%s/%s/%s' % ( groupId, artifactId, version, filename ) |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 45 | f = open(file, 'rb') |
| 46 | zip.writestr(ZipInfo(dest, date_time=(1980, 1, 1, 0, 0, 0)), f.read()) |
| 47 | f.close() |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 48 | |
| 49 | if __name__ == '__main__': |
| 50 | import sys |
| 51 | |
| 52 | if len(sys.argv) < 2: |
Laszlo Papp | 066fe4a | 2018-09-05 13:21:27 +0100 | [diff] [blame] | 53 | print('USAGE') |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 54 | sys.exit(1) |
| 55 | |
| 56 | output = sys.argv[1] |
| 57 | args = sys.argv[2:] |
| 58 | |
| 59 | if len(args) % 2 != 0: |
Laszlo Papp | 066fe4a | 2018-09-05 13:21:27 +0100 | [diff] [blame] | 60 | print('There must be an even number of args: file mvn_coords') |
Ray Milkey | ad83ef9 | 2018-06-05 11:05:51 -0700 | [diff] [blame] | 61 | sys.exit(2) |
| 62 | |
| 63 | files = zip(*[iter(args)]*2) |
| 64 | generateOar(output, files) |