blob: 0cd6c3e348a54bcfdad35af9880cdf483128f127 [file] [log] [blame]
Ray Milkeyebc673f2018-07-20 16:51:55 -07001#!/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
18from subprocess import call
19import tempfile
20import hashlib
21import requests, os
22import xml.etree.ElementTree, shutil
23
24SONATYPE_USER=os.environ.get("SONATYPE_USER")
25SONATYPE_PASSWORD=os.environ.get("SONATYPE_PASSWORD")
26SONATYPE_PROFILE=os.environ.get("SONATYPE_PROFILE")
Ray Milkeyebc673f2018-07-20 16:51:55 -070027
28CREATE_REPO_REQUEST_TEMPLATE = '''\
29<promoteRequest>
30 <data>
31 <description>%(description)</description>
32 </data>
33</promoteRequest>
34'''
35
36CLOSE_REPO_REQUEST_TEMPLATE = '''\
37<promoteRequest>
38 <data>
39 <description>%(description)</description>
40 <stagedRepositoryId>%(repo_id)</stagedRepositoryId>
41 </data>
42</promoteRequest>
43'''
44
45def 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 Milkey7f46b1f2018-07-24 19:01:58 -070056def generate_metadata_files(input_file, dest):
Ray Milkeyebc673f2018-07-20 16:51:55 -070057 # create a temporary directory to hold the metadata files
58 global tempdir
Ray Milkey7f46b1f2018-07-24 19:01:58 -070059 base_metadata_filename = tempdir + "/" + os.path.basename(dest)
Ray Milkeyebc673f2018-07-20 16:51:55 -070060
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 Milkey7f46b1f2018-07-24 19:01:58 -070080 # 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 Milkeyebc673f2018-07-20 16:51:55 -070085 return files
86
87
88def create_staging_repo(description):
Ray Milkeyd111751b2018-07-24 10:14:02 -070089 if destination_repo_url is None:
90 return None
Ray Milkeyebc673f2018-07-20 16:51:55 -070091 create_request = CREATE_REPO_REQUEST_TEMPLATE.replace("%(description)", description)
Ray Milkeyd111751b2018-07-24 10:14:02 -070092 url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE + "/start"
Ray Milkeyebc673f2018-07-20 16:51:55 -070093 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 Milkeyebc673f2018-07-20 16:51:55 -070097 return repo_id
98
99
100def close_staging_repo(description, repo_id):
Ray Milkeyd111751b2018-07-24 10:14:02 -0700101 if repo_id is None:
102 return
Ray Milkeyebc673f2018-07-20 16:51:55 -0700103 close_request = CLOSE_REPO_REQUEST_TEMPLATE.replace("%(description)", description).replace("%(repo_id)", repo_id)
Ray Milkeyd111751b2018-07-24 10:14:02 -0700104 url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE + "/finish"
Ray Milkeyebc673f2018-07-20 16:51:55 -0700105 headers = {'Content-Type': 'application/xml'}
106 r = requests.post(url, close_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD))
107
108
109def stage_file(file, repo_id, dest):
Ray Milkey7f46b1f2018-07-24 19:01:58 -0700110 filename_in_repo = os.path.dirname(dest) + "/" + os.path.basename(file)
Ray Milkeyd111751b2018-07-24 10:14:02 -0700111 if destination_repo_url is not None:
112 # deploy to Nexus repo
113 upload_base = "https://" + destination_repo_url + "/service/local/staging/deployByRepositoryId"
Ray Milkey7f46b1f2018-07-24 19:01:58 -0700114 url = upload_base + "/" + repo_id + "/" + filename_in_repo
Ray Milkeyd111751b2018-07-24 10:14:02 -0700115 headers = {'Content-Type': 'application/xml'}
116 with open(file, 'rb') as f:
Ray Milkey7f46b1f2018-07-24 19:01:58 -0700117 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 Milkeyd111751b2018-07-24 10:14:02 -0700122 else:
123 # deploy to local repo
Ray Milkey7f46b1f2018-07-24 19:01:58 -0700124 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 Milkeyebc673f2018-07-20 16:51:55 -0700129
130
131def stage_files(files, dest):
132 for file in files:
133 stage_file(file=file, repo_id=repo_id, dest=dest)
134
135
136def upload_file(src, dest):
Ray Milkey7f46b1f2018-07-24 19:01:58 -0700137 print ("publishing: " + dest.replace("org/onosproject", ""))
138 files = generate_metadata_files(src, dest)
Ray Milkeyebc673f2018-07-20 16:51:55 -0700139 stage_files(files, dest)
140
141
142if __name__ == '__main__':
143 import sys
144
145 if len(sys.argv) < 2:
Ray Milkeyd111751b2018-07-24 10:14:02 -0700146 print 'USAGE: upload-maven-artifacts catalog-file-name [nexus root url]'
Ray Milkeyebc673f2018-07-20 16:51:55 -0700147 sys.exit(1)
148
149 input_list_file = sys.argv[1]
Ray Milkeyd111751b2018-07-24 10:14:02 -0700150
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 Milkeyebc673f2018-07-20 16:51:55 -0700178 list_file = open(input_list_file, "r")
179 lines = list_file.readlines()
180 list_file.close()
181
Ray Milkeyd111751b2018-07-24 10:14:02 -0700182 tempdir = tempfile.mkdtemp(prefix="upload-maven-artifacts-")
Ray Milkeyebc673f2018-07-20 16:51:55 -0700183 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)