pierventre | cf38ba3 | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 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 | from subprocess import call |
| 19 | import tempfile |
| 20 | import hashlib |
| 21 | import requests, os |
| 22 | import xml.etree.ElementTree, shutil |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 23 | import time |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 24 | |
| 25 | SONATYPE_USER=os.environ.get("SONATYPE_USER") |
| 26 | SONATYPE_PASSWORD=os.environ.get("SONATYPE_PASSWORD") |
| 27 | SONATYPE_PROFILE=os.environ.get("SONATYPE_PROFILE") |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 28 | |
| 29 | CREATE_REPO_REQUEST_TEMPLATE = '''\ |
| 30 | <promoteRequest> |
| 31 | <data> |
| 32 | <description>%(description)</description> |
| 33 | </data> |
| 34 | </promoteRequest> |
| 35 | ''' |
| 36 | |
| 37 | CLOSE_REPO_REQUEST_TEMPLATE = '''\ |
| 38 | <promoteRequest> |
| 39 | <data> |
| 40 | <description>%(description)</description> |
| 41 | <stagedRepositoryId>%(repo_id)</stagedRepositoryId> |
| 42 | </data> |
| 43 | </promoteRequest> |
| 44 | ''' |
| 45 | |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 46 | CLOSE_RETRY_ATTEMPTS = 12 * 2 |
| 47 | |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 48 | def hashlib_compute(hash, input_file, output_file): |
| 49 | with open(input_file, 'rb') as f: |
| 50 | for block in iter(lambda: f.read(100000), b''): |
| 51 | hash.update(block) |
| 52 | md5_string = hash.hexdigest() |
| 53 | output = open(output_file, "w") |
| 54 | output.write(md5_string + "\n") |
| 55 | f.close() |
| 56 | output.close() |
| 57 | |
| 58 | |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 59 | def generate_metadata_files(input_file, dest): |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 60 | # create a temporary directory to hold the metadata files |
| 61 | global tempdir |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 62 | base_metadata_filename = tempdir + "/" + os.path.basename(dest) |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 63 | |
| 64 | files = [] |
| 65 | |
Ray Milkey | c161f82 | 2018-11-14 14:21:17 -0800 | [diff] [blame] | 66 | if destination_repo_url is not None: |
| 67 | # generate maven metadata files: signature, MD5, and SHA |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 68 | |
Ray Milkey | c161f82 | 2018-11-14 14:21:17 -0800 | [diff] [blame] | 69 | # generate the signature file |
| 70 | signature_filename = base_metadata_filename + ".asc" |
| 71 | call(["gpg", "--armor", "--detach-sig", "--output", signature_filename, input_file]) |
| 72 | files.append(signature_filename) |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 73 | |
Ray Milkey | c161f82 | 2018-11-14 14:21:17 -0800 | [diff] [blame] | 74 | # generate the md5 checksum file |
| 75 | md5_filename = base_metadata_filename + ".md5" |
| 76 | md5 = hashlib.md5() |
| 77 | hashlib_compute(md5, input_file, md5_filename) |
| 78 | files.append(md5_filename) |
| 79 | |
| 80 | # generate the SHA checksum file |
| 81 | sha1_filename = base_metadata_filename + ".sha1" |
| 82 | sha1 = hashlib.sha1() |
| 83 | hashlib_compute(sha1, input_file, sha1_filename) |
| 84 | files.append(sha1_filename) |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 85 | |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 86 | # generate the base artifact |
| 87 | base_artifact_filename = base_metadata_filename |
| 88 | shutil.copyfile(input_file, base_artifact_filename) |
| 89 | files.append(base_artifact_filename) |
| 90 | |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 91 | return files |
| 92 | |
| 93 | |
| 94 | def create_staging_repo(description): |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 95 | if destination_repo_url is None: |
| 96 | return None |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 97 | create_request = CREATE_REPO_REQUEST_TEMPLATE.replace("%(description)", description) |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 98 | url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE + "/start" |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 99 | headers = {'Content-Type': 'application/xml'} |
| 100 | r = requests.post(url, create_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD)) |
| 101 | root = xml.etree.ElementTree.fromstring(r.text) |
| 102 | repo_id = root.find("data").find("stagedRepositoryId").text |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 103 | return repo_id |
| 104 | |
| 105 | |
| 106 | def close_staging_repo(description, repo_id): |
Ray Milkey | 245d119 | 2018-08-14 17:49:33 -0700 | [diff] [blame] | 107 | if destination_repo_url is None: |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 108 | return |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 109 | close_request = CLOSE_REPO_REQUEST_TEMPLATE.replace("%(description)", description).replace("%(repo_id)", repo_id) |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 110 | url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE + "/finish" |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 111 | headers = {'Content-Type': 'application/xml'} |
| 112 | r = requests.post(url, close_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD)) |
| 113 | |
| 114 | |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 115 | def wait_for_staging_repo(description, repo_id): |
Ray Milkey | cb8ea06 | 2018-08-14 19:31:47 -0700 | [diff] [blame] | 116 | if destination_repo_url is None: |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 117 | return |
Ray Milkey | cb8ea06 | 2018-08-14 19:31:47 -0700 | [diff] [blame] | 118 | base_url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 119 | close_request = CLOSE_REPO_REQUEST_TEMPLATE.replace("%(description)", description).replace("%(repo_id)", repo_id) |
| 120 | url = base_url + "/finish" |
| 121 | headers = {'Content-Type': 'application/xml'} |
| 122 | repo_query_url = "https://oss.sonatype.org/service/local/staging/repository/" + repo_id |
| 123 | |
| 124 | attempt = 1 |
| 125 | print ("waiting for repo to close...") |
| 126 | while True: |
| 127 | r = requests.get(repo_query_url, close_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD)) |
| 128 | root = xml.etree.ElementTree.fromstring(r.text) |
| 129 | transitioning = root.find("transitioning").text |
| 130 | if transitioning != "true": |
| 131 | break |
| 132 | if attempt == CLOSE_RETRY_ATTEMPTS: |
| 133 | print ("Unable to close repo") |
| 134 | sys.exit(1) |
| 135 | attempt = attempt + 1 |
| 136 | time.sleep(5) |
| 137 | print ("Repo closed successfully") |
| 138 | |
| 139 | |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 140 | def stage_file(file, repo_id, dest): |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 141 | filename_in_repo = os.path.dirname(dest) + "/" + os.path.basename(file) |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 142 | if destination_repo_url is not None: |
| 143 | # deploy to Nexus repo |
| 144 | upload_base = "https://" + destination_repo_url + "/service/local/staging/deployByRepositoryId" |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 145 | url = upload_base + "/" + repo_id + "/" + filename_in_repo |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 146 | headers = {'Content-Type': 'application/xml'} |
| 147 | with open(file, 'rb') as f: |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 148 | r = requests.post(url, data=f.read(), headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD)) |
| 149 | if r.status_code != 201: |
pierventre | cf38ba3 | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 150 | print((r.status_code)) |
| 151 | print((r.text)) |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 152 | sys.exit(1) |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 153 | else: |
| 154 | # deploy to local repo |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 155 | file_in_local_repo = os.path.expanduser(local_maven_repo + "/" + filename_in_repo) |
| 156 | dir_in_local_repo = os.path.dirname(file_in_local_repo) |
| 157 | if not os.path.isdir(dir_in_local_repo): |
| 158 | os.makedirs(dir_in_local_repo) |
| 159 | shutil.copyfile(src, file_in_local_repo) |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 160 | |
| 161 | |
| 162 | def stage_files(files, dest): |
| 163 | for file in files: |
| 164 | stage_file(file=file, repo_id=repo_id, dest=dest) |
| 165 | |
| 166 | |
| 167 | def upload_file(src, dest): |
pierventre | cf38ba3 | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 168 | print(("publishing: " + dest.replace("org/onosproject", ""))) |
Ray Milkey | 7f46b1f | 2018-07-24 19:01:58 -0700 | [diff] [blame] | 169 | files = generate_metadata_files(src, dest) |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 170 | stage_files(files, dest) |
| 171 | |
| 172 | |
| 173 | if __name__ == '__main__': |
| 174 | import sys |
| 175 | |
| 176 | if len(sys.argv) < 2: |
pierventre | cf38ba3 | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 177 | print('USAGE: upload-maven-artifacts catalog-file-name [nexus root url]') |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 178 | sys.exit(1) |
| 179 | |
| 180 | input_list_file = sys.argv[1] |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 181 | |
| 182 | local_maven_repo = None |
| 183 | destination_repo_url = None |
| 184 | |
| 185 | if len(sys.argv) == 3: |
| 186 | destination_repo_url = sys.argv[2] |
| 187 | else: |
| 188 | local_maven_repo = os.environ.get("MAVEN_REPO") |
| 189 | if local_maven_repo is None: |
| 190 | local_maven_repo = "~/.m2/repository" |
| 191 | |
| 192 | if destination_repo_url is not None: |
| 193 | if SONATYPE_USER is None: |
pierventre | cf38ba3 | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 194 | print("Environment variable SONATYPE_USER must be set") |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 195 | sys.exit(1) |
| 196 | |
| 197 | if SONATYPE_PASSWORD is None: |
pierventre | cf38ba3 | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 198 | print("Environment variable SONATYPE_PASSWORD must be set") |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 199 | sys.exit(1) |
| 200 | |
| 201 | if SONATYPE_PROFILE is None: |
pierventre | cf38ba3 | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 202 | print("Environment variable SONATYPE_PROFILE must be set") |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 203 | sys.exit(1) |
| 204 | |
pierventre | cf38ba3 | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 205 | print(("Uploading to remote repo: " + destination_repo_url)) |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 206 | else: |
pierventre | cf38ba3 | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 207 | print(("Installing in local repo: " + local_maven_repo)) |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 208 | |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 209 | list_file = open(input_list_file, "r") |
| 210 | lines = list_file.readlines() |
| 211 | list_file.close() |
| 212 | |
Ray Milkey | d111751b | 2018-07-24 10:14:02 -0700 | [diff] [blame] | 213 | tempdir = tempfile.mkdtemp(prefix="upload-maven-artifacts-") |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 214 | description = "test repo" |
| 215 | repo_id = create_staging_repo(description) |
| 216 | for line in lines: |
| 217 | s = line.split() |
| 218 | src = s[0] |
| 219 | dest = s[1] |
| 220 | upload_file(src, dest) |
| 221 | close_staging_repo(repo_id=repo_id, description=description) |
Ray Milkey | f77ea41 | 2018-08-10 17:37:28 -0700 | [diff] [blame] | 222 | wait_for_staging_repo(repo_id=repo_id, description=description) |
Ray Milkey | ebc673f | 2018-07-20 16:51:55 -0700 | [diff] [blame] | 223 | shutil.rmtree(tempdir) |