Thomas Vachuska | ac9e524 | 2018-07-19 16:15:39 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | """ |
| 17 | |
| 18 | POM_HEADER = '''\ |
| 19 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 20 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 21 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 22 | <modelVersion>4.0.0</modelVersion> |
| 23 | <parent> |
| 24 | <groupId>org.onosproject</groupId> |
| 25 | <artifactId>onos-base</artifactId> |
| 26 | <version>1</version> |
| 27 | </parent>''' |
| 28 | POM_FOOTER = '</project>\n' |
| 29 | |
| 30 | ID_BLOCK = ''' |
| 31 | <groupId>%s</groupId> |
| 32 | <artifactId>%s</artifactId> |
| 33 | <version>%s</version> |
| 34 | <name>%s</name> |
| 35 | ''' |
| 36 | |
| 37 | DEPENDENCIES_HEADER = ' <dependencies>\n' |
| 38 | DEPENDENCIES_FOOTER = ' </dependencies>\n' |
| 39 | |
| 40 | DEPENDENCY_BLOCK = ''' <dependency> |
| 41 | <groupId>%s</groupId> |
| 42 | <artifactId>%s</artifactId> |
| 43 | <version>%s</version> |
| 44 | </dependency> |
| 45 | ''' |
| 46 | |
| 47 | def write(name, msg): |
| 48 | if name is not None: |
| 49 | with open(name, "w") as file: |
| 50 | file.write(msg) |
| 51 | else: |
| 52 | print msg |
| 53 | |
| 54 | |
| 55 | def write_pom(output, coords, deps): |
| 56 | mvn = coords.split(':') |
| 57 | lines = POM_HEADER |
| 58 | lines += ID_BLOCK % (mvn[1], mvn[2], mvn[3], mvn[2]) |
| 59 | lines += DEPENDENCIES_HEADER |
| 60 | |
| 61 | for dep in deps: |
| 62 | mvn = dep.split(':') |
| 63 | lines += DEPENDENCY_BLOCK % (mvn[1], mvn[2], mvn[len(mvn)-1]) |
| 64 | |
| 65 | lines += DEPENDENCIES_FOOTER |
| 66 | lines += POM_FOOTER |
| 67 | write(output, lines) |
| 68 | |
| 69 | if __name__ == '__main__': |
| 70 | import sys |
| 71 | |
| 72 | if len(sys.argv) < 3: |
| 73 | print 'usage: pom_generator pom.xml maven_coords dep_coords1 dep_coords2 ...' |
| 74 | sys.exit(1) |
| 75 | |
| 76 | output = sys.argv[1] |
| 77 | coords = sys.argv[2] |
| 78 | deps = sys.argv[3:] |
| 79 | |
| 80 | write_pom(output, coords, deps) |