Carmelo Cascone | 87b893e | 2019-11-12 10:34:05 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2019-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | import argparse |
| 16 | from datetime import datetime |
| 17 | from xml.dom import minidom |
| 18 | |
| 19 | |
| 20 | def resolve(mvn_coord): |
| 21 | mvn_pieces = mvn_coord.split(":") |
| 22 | if mvn_pieces[0] != "mvn": |
| 23 | raise ("Invalid Maven coordinate: %s" % mvn_coord) |
| 24 | return dict( |
| 25 | groupId=mvn_pieces[1], |
| 26 | artifactId=mvn_pieces[2], |
| 27 | version=mvn_pieces[-1], |
| 28 | name=mvn_coord, |
| 29 | ) |
| 30 | |
| 31 | |
| 32 | def xml_beautify(data): |
| 33 | beautified = '\n'.join([ |
| 34 | l for l in |
| 35 | minidom.parseString(data).toprettyxml(indent=' ' * 4).split('\n') |
| 36 | if l.strip()]) |
| 37 | return beautified |
| 38 | |
| 39 | |
| 40 | def generate_pom(out_file, template_file, provided_deps, test_deps, deps, var_dict): |
| 41 | deps = {d: resolve(d) for d in deps} |
| 42 | |
| 43 | dep_mgmt_template = """ |
| 44 | <dependency> |
| 45 | <!-- {name} --> |
| 46 | <groupId>{groupId}</groupId> |
| 47 | <artifactId>{artifactId}</artifactId> |
| 48 | <version>{version}</version> |
| 49 | </dependency>""" |
| 50 | |
| 51 | dep_template = """ |
| 52 | <dependency> |
| 53 | <!-- {name} --> |
| 54 | <groupId>{groupId}</groupId> |
| 55 | <artifactId>{artifactId}</artifactId> |
| 56 | <scope>{scope}</scope> |
| 57 | </dependency>""" |
| 58 | |
| 59 | mgmt_deps = sorted(deps.keys()) |
| 60 | provided_deps.sort() |
| 61 | test_deps.sort() |
| 62 | |
| 63 | with open(template_file, "r") as f: |
| 64 | lines = f.readlines() |
| 65 | |
| 66 | new_lines = [ |
| 67 | "<!-- Automatically generated on %s -->" |
| 68 | % datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 69 | ] |
| 70 | for line in lines: |
| 71 | if "<!-- DEPS_MGMT -->" in line: |
| 72 | new_lines.extend([ |
| 73 | dep_mgmt_template.format(**deps[x]) for x in mgmt_deps]) |
| 74 | elif "<!-- DEPS -->" in line: |
| 75 | new_lines.extend([ |
| 76 | dep_template.format(scope='provided', **deps[x]) |
| 77 | for x in provided_deps]) |
| 78 | new_lines.extend([ |
| 79 | dep_template.format(scope='test', **deps[x]) |
| 80 | for x in test_deps]) |
| 81 | else: |
| 82 | for old, new in var_dict.items(): |
| 83 | line = line.replace(old, new) |
| 84 | new_lines.append(line) |
| 85 | |
| 86 | with open(out_file, 'w') as f: |
| 87 | f.write(xml_beautify("\n".join(new_lines))) |
| 88 | |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | parser = argparse.ArgumentParser() |
| 92 | parser.add_argument('-o', dest='out_file', type=str, action="store", |
| 93 | required=True, help="Path to output file") |
| 94 | parser.add_argument('-p', dest='template_file', type=str, action="store", |
| 95 | required=True, help="Path to pom template file") |
| 96 | parser.add_argument('-c', dest='provided_deps', metavar='PROVIDED_DEP', |
| 97 | type=str, nargs='+', default=[], |
| 98 | help='Maven coordinates to list with scope provided') |
| 99 | parser.add_argument('-t', dest='test_deps', metavar='TEST_DEP', type=str, |
| 100 | nargs='+', default=[], |
| 101 | help='Maven coordinates to list with scope test') |
| 102 | parser.add_argument('-d', dest='deps', metavar='DEP', type=str, |
| 103 | nargs='+', default=[], |
| 104 | help='Maven coordinates to list under <dependencyManagement>') |
| 105 | parser.add_argument('-v', dest='vars', metavar='VAR=value', type=str, |
| 106 | nargs='+', default=[], |
| 107 | help='Replace all instances of <!-- VAR --> with the given value') |
| 108 | args = parser.parse_args() |
| 109 | |
| 110 | processed_vars = {} |
| 111 | for var in args.vars: |
| 112 | pieces = var.split('=') |
| 113 | if len(pieces) != 2: |
| 114 | raise ("Invalid var '%s'" % var) |
| 115 | processed_vars["<!-- %s -->" % pieces[0]] = pieces[1] |
| 116 | |
| 117 | generate_pom( |
| 118 | out_file=args.out_file, |
| 119 | template_file=args.template_file, |
| 120 | provided_deps=args.provided_deps, |
| 121 | test_deps=args.test_deps, |
| 122 | deps=args.deps, |
| 123 | var_dict=processed_vars |
| 124 | ) |