blob: 9a5494144aaecdc81e9ba79709b9e5df8db70e7a [file] [log] [blame]
Daniele Moro42dce382021-03-04 11:57:55 +01001#!/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
18from subprocess import call
19import requests, os
20import argparse
21import sys
22
23
24if __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 Moro7b121a22021-05-20 13:05:10 +020031 parser.add_argument('--repo_id', type=str, required=False, default="ossrh",
Daniele Moro42dce382021-03-04 11:57:55 +010032 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
Daniele Morob784d982021-05-21 17:08:29 +0200104 if artifact_id == "onos-dependencies":
105 # Special case for onos-dependencies, the JAR is a fake empty jar.
106 # For this case, upload only the pom.
107 if "pom" in artifacts.keys():
108 call_funct.append("-Dfile=" + artifacts["pom"][0])
109 call_funct.append("-DgeneratePom=false")
110 del artifacts["pom"]
111 if "jar" in artifacts.keys():
112 del artifacts["jar"]
Daniele Moro42dce382021-03-04 11:57:55 +0100113 else:
Daniele Morob784d982021-05-21 17:08:29 +0200114 # If we have a pom, use it, otherwise do not let Maven generate one
115 if "pom" in artifacts.keys():
116 call_funct.append("-DpomFile=" + artifacts["pom"][0])
117 del artifacts["pom"]
118 else:
119 call_funct.append("-DgeneratePom=false")
Daniele Moro42dce382021-03-04 11:57:55 +0100120
Daniele Morob784d982021-05-21 17:08:29 +0200121 # Find the main artifact, it can be a JAR or a OAR
122 if "jar" in artifacts.keys():
123 call_funct.append("-Dfile=" + artifacts["jar"][0])
124 del artifacts["jar"]
125 elif "oar" in artifacts.keys():
126 call_funct.append("-Dfile=" + artifacts["oar"][0])
127 del artifacts["oar"]
128 else:
129 print("WARNING: Skipping, no main artifact for artifact ID: " + artifact_id)
130 continue
Daniele Moro42dce382021-03-04 11:57:55 +0100131
132 if "javadoc" in artifacts.keys():
133 call_funct.append("-Djavadoc=" + artifacts["javadoc"][0])
134 del artifacts["javadoc"]
135 if "sources" in artifacts.keys():
136 call_funct.append("-Dsources=" + artifacts["sources"][0])
137 del artifacts["sources"]
138
139 # Build the list of the other files to upload together with the main artifact
140 other_files = []
141 other_files_types = []
142 other_files_classifier = []
143 for key, val in artifacts.items():
144 other_files.append(val[0])
145 other_files_types.append(val[1])
146 other_files_classifier.append(key)
147 if len(other_files) > 0:
148 call_funct.append("-Dfiles=" + ",".join(other_files))
149 call_funct.append("-Dtypes=" + ",".join(other_files_types))
150 call_funct.append("-Dclassifiers=" + ",".join(other_files_classifier))
151 if dry_run:
152 print(artifact_id + "\n" + " ".join(call_funct))
153 print()
154 else:
155 call(call_funct)