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