Daniele Moro | 8a8b572 | 2021-03-04 11:57:55 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Copyright 2021-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 | from subprocess import call |
| 19 | import requests, os |
| 20 | import argparse |
| 21 | import sys |
| 22 | |
| 23 | |
| 24 | if __name__ == '__main__': |
| 25 | parser = argparse.ArgumentParser(description='Upload artifacts with Maven.') |
| 26 | parser.add_argument('catalog_file_name', type=str, help="Catalog File Name") |
| 27 | parser.add_argument('destination_repo_url', type=str, nargs="?", default="", |
| 28 | help="Nexus complete URL") |
| 29 | parser.add_argument('--local_settings', type=str, required=False, default=None, |
| 30 | help="Use a local settings.xml instead of the default one") |
| 31 | parser.add_argument('--repo_id', type=str, required=False, default="snapshots", |
| 32 | help="ID to map on the <id> under <server> section of settings.xml") |
| 33 | parser.add_argument('--group_id', type=str, required=False, default="org.onosproject", |
| 34 | help="GroupId of the artifacts to be deployed") |
| 35 | parser.add_argument('--dry_run', action="store_true", required=False, default=False) |
| 36 | args = parser.parse_args() |
| 37 | |
| 38 | input_list_file = args.catalog_file_name |
| 39 | destination_repo_url = args.destination_repo_url |
| 40 | local_settings_file = args.local_settings |
| 41 | repo_id = args.repo_id |
| 42 | group_id = args.group_id |
| 43 | dry_run = args.dry_run |
| 44 | |
| 45 | with open(input_list_file, "r") as list_file: |
| 46 | lines = list_file.readlines() |
| 47 | |
| 48 | artifacts_to_upload = {} |
| 49 | for line in lines: |
| 50 | s = line.split() |
| 51 | src = s[0] |
| 52 | dest = s[1] |
| 53 | artifactId = dest.split("/")[2] |
| 54 | info = artifacts_to_upload.get(artifactId, {}) |
| 55 | version = dest.split("/")[3] |
| 56 | if info.get("version", None) is not None: |
| 57 | if info["version"] != version: |
| 58 | print("ERROR: Version mismatch: " + artifactId + ", Version: " + |
| 59 | info["version"], ", New Version: " + version) |
| 60 | sys.exit(1) |
| 61 | else: |
| 62 | info['version'] = version |
| 63 | artifacts = info.get("artifacts", {}) |
| 64 | src_and_extension = [src, os.path.splitext(src)[1].replace(".", '')] |
| 65 | if "-sources" in src: |
| 66 | artifacts["sources"] = src_and_extension |
| 67 | elif "-javadoc" in src: |
| 68 | artifacts["javadoc"] = src_and_extension |
| 69 | elif "-oar" in src: |
| 70 | artifacts["oar"] = src_and_extension |
| 71 | elif "-tests" in src: |
| 72 | artifacts["tests"] = src_and_extension |
| 73 | elif "-pom" in src: |
| 74 | artifacts["pom"] = src_and_extension |
| 75 | elif src.endswith(".jar"): |
| 76 | artifacts["jar"] = src_and_extension |
| 77 | # Exclude archives for now |
| 78 | # elif src.endswith(".tar.gz"): |
| 79 | # artifacts["tar"] = [src, "targz"] |
| 80 | else: |
| 81 | print("WARNING: artifact " + dest + " not supported, skipping") |
| 82 | info["artifacts"] = artifacts |
| 83 | artifacts_to_upload[artifactId] = info |
| 84 | |
| 85 | if dry_run: |
| 86 | print("------- Artifacts dictionary ---------") |
| 87 | print(artifacts_to_upload) |
| 88 | print("--------------------------------------") |
| 89 | print() |
| 90 | |
| 91 | # Upload the artifacts to the remote Nexus repository |
| 92 | for artifact_id, info in artifacts_to_upload.items(): |
| 93 | artifacts = info["artifacts"] |
| 94 | call_funct = ["mvn", "deploy:deploy-file", |
| 95 | "-Durl=" + destination_repo_url, |
| 96 | "-DrepositoryId=" + repo_id, |
| 97 | "-DgroupId=" + group_id, |
| 98 | "-DartifactId=" + artifact_id, |
| 99 | "-Dversion=" + info["version"]] |
| 100 | if local_settings_file is not None: |
| 101 | call_funct.insert(1, "-s") |
| 102 | call_funct.insert(2, local_settings_file) |
| 103 | |
| 104 | # If we have a pom, use it, otherwise do not let Maven generate one |
| 105 | if "pom" in artifacts.keys(): |
| 106 | call_funct.append("-DpomFile=" + artifacts["pom"][0]) |
| 107 | del artifacts["pom"] |
| 108 | else: |
| 109 | call_funct.append("-DgeneratePom=false") |
| 110 | |
| 111 | # Find the main artifact, it can be a JAR or a OAR |
| 112 | if "jar" in artifacts.keys(): |
| 113 | call_funct.append("-Dfile=" + artifacts["jar"][0]) |
| 114 | del artifacts["jar"] |
| 115 | elif "oar" in artifacts.keys(): |
| 116 | call_funct.append("-Dfile=" + artifacts["oar"][0]) |
| 117 | del artifacts["oar"] |
| 118 | else: |
| 119 | print("WARNING: Skipping, no main artifact for artifact ID: " + artifact_id) |
| 120 | continue |
| 121 | |
| 122 | if "javadoc" in artifacts.keys(): |
| 123 | call_funct.append("-Djavadoc=" + artifacts["javadoc"][0]) |
| 124 | del artifacts["javadoc"] |
| 125 | if "sources" in artifacts.keys(): |
| 126 | call_funct.append("-Dsources=" + artifacts["sources"][0]) |
| 127 | del artifacts["sources"] |
| 128 | |
| 129 | # Build the list of the other files to upload together with the main artifact |
| 130 | other_files = [] |
| 131 | other_files_types = [] |
| 132 | other_files_classifier = [] |
| 133 | for key, val in artifacts.items(): |
| 134 | other_files.append(val[0]) |
| 135 | other_files_types.append(val[1]) |
| 136 | other_files_classifier.append(key) |
| 137 | if len(other_files) > 0: |
| 138 | call_funct.append("-Dfiles=" + ",".join(other_files)) |
| 139 | call_funct.append("-Dtypes=" + ",".join(other_files_types)) |
| 140 | call_funct.append("-Dclassifiers=" + ",".join(other_files_classifier)) |
| 141 | if dry_run: |
| 142 | print(artifact_id + "\n" + " ".join(call_funct)) |
| 143 | print() |
| 144 | else: |
| 145 | call(call_funct) |