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") |
Daniele Moro | 7286e89 | 2021-05-20 13:05:10 +0200 | [diff] [blame] | 31 | parser.add_argument('--repo_id', type=str, required=False, default="ossrh", |
Daniele Moro | 8a8b572 | 2021-03-04 11:57:55 +0100 | [diff] [blame] | 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"] |
Daniele Moro | ed04b54 | 2022-03-08 11:09:01 +0100 | [diff] [blame] | 94 | call_funct = ["mvn", |
| 95 | "-B", # Run in batch mode |
| 96 | "-q", # Run in quiet mode, will report only warning or errors |
| 97 | "deploy:deploy-file", |
Daniele Moro | 8a8b572 | 2021-03-04 11:57:55 +0100 | [diff] [blame] | 98 | "-Durl=" + destination_repo_url, |
| 99 | "-DrepositoryId=" + repo_id, |
| 100 | "-DgroupId=" + group_id, |
| 101 | "-DartifactId=" + artifact_id, |
| 102 | "-Dversion=" + info["version"]] |
| 103 | if local_settings_file is not None: |
| 104 | call_funct.insert(1, "-s") |
| 105 | call_funct.insert(2, local_settings_file) |
| 106 | |
Daniele Moro | 385cdd7 | 2021-05-21 17:08:29 +0200 | [diff] [blame] | 107 | if artifact_id == "onos-dependencies": |
| 108 | # Special case for onos-dependencies, the JAR is a fake empty jar. |
| 109 | # For this case, upload only the pom. |
| 110 | if "pom" in artifacts.keys(): |
| 111 | call_funct.append("-Dfile=" + artifacts["pom"][0]) |
| 112 | call_funct.append("-DgeneratePom=false") |
| 113 | del artifacts["pom"] |
| 114 | if "jar" in artifacts.keys(): |
| 115 | del artifacts["jar"] |
Daniele Moro | 8a8b572 | 2021-03-04 11:57:55 +0100 | [diff] [blame] | 116 | else: |
Daniele Moro | 385cdd7 | 2021-05-21 17:08:29 +0200 | [diff] [blame] | 117 | # If we have a pom, use it, otherwise do not let Maven generate one |
| 118 | if "pom" in artifacts.keys(): |
| 119 | call_funct.append("-DpomFile=" + artifacts["pom"][0]) |
| 120 | del artifacts["pom"] |
| 121 | else: |
| 122 | call_funct.append("-DgeneratePom=false") |
Daniele Moro | 8a8b572 | 2021-03-04 11:57:55 +0100 | [diff] [blame] | 123 | |
Daniele Moro | 385cdd7 | 2021-05-21 17:08:29 +0200 | [diff] [blame] | 124 | # Find the main artifact, it can be a JAR or a OAR |
| 125 | if "jar" in artifacts.keys(): |
| 126 | call_funct.append("-Dfile=" + artifacts["jar"][0]) |
| 127 | del artifacts["jar"] |
| 128 | elif "oar" in artifacts.keys(): |
| 129 | call_funct.append("-Dfile=" + artifacts["oar"][0]) |
| 130 | del artifacts["oar"] |
| 131 | else: |
| 132 | print("WARNING: Skipping, no main artifact for artifact ID: " + artifact_id) |
| 133 | continue |
Daniele Moro | 8a8b572 | 2021-03-04 11:57:55 +0100 | [diff] [blame] | 134 | |
| 135 | if "javadoc" in artifacts.keys(): |
| 136 | call_funct.append("-Djavadoc=" + artifacts["javadoc"][0]) |
| 137 | del artifacts["javadoc"] |
| 138 | if "sources" in artifacts.keys(): |
| 139 | call_funct.append("-Dsources=" + artifacts["sources"][0]) |
| 140 | del artifacts["sources"] |
| 141 | |
| 142 | # Build the list of the other files to upload together with the main artifact |
| 143 | other_files = [] |
| 144 | other_files_types = [] |
| 145 | other_files_classifier = [] |
| 146 | for key, val in artifacts.items(): |
| 147 | other_files.append(val[0]) |
| 148 | other_files_types.append(val[1]) |
| 149 | other_files_classifier.append(key) |
| 150 | if len(other_files) > 0: |
| 151 | call_funct.append("-Dfiles=" + ",".join(other_files)) |
| 152 | call_funct.append("-Dtypes=" + ",".join(other_files_types)) |
| 153 | call_funct.append("-Dclassifiers=" + ",".join(other_files_classifier)) |
| 154 | if dry_run: |
| 155 | print(artifact_id + "\n" + " ".join(call_funct)) |
| 156 | print() |
| 157 | else: |
| 158 | call(call_funct) |