blob: d20e41dec0fa1163d693b982311990e61a26f30f [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
56def generate_metadata_files(input_file):
57 # create a temporary directory to hold the metadata files
58 global tempdir
59 base_metadata_filename = tempdir + "/" + os.path.basename(input_file)
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
80 return files
81
82
83def create_staging_repo(description):
Ray Milkeyd111751b2018-07-24 10:14:02 -070084 if destination_repo_url is None:
85 return None
Ray Milkeyebc673f2018-07-20 16:51:55 -070086 create_request = CREATE_REPO_REQUEST_TEMPLATE.replace("%(description)", description)
Ray Milkeyd111751b2018-07-24 10:14:02 -070087 url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE + "/start"
Ray Milkeyebc673f2018-07-20 16:51:55 -070088 headers = {'Content-Type': 'application/xml'}
89 r = requests.post(url, create_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD))
90 root = xml.etree.ElementTree.fromstring(r.text)
91 repo_id = root.find("data").find("stagedRepositoryId").text
Ray Milkeyebc673f2018-07-20 16:51:55 -070092 return repo_id
93
94
95def close_staging_repo(description, repo_id):
Ray Milkeyd111751b2018-07-24 10:14:02 -070096 if repo_id is None:
97 return
Ray Milkeyebc673f2018-07-20 16:51:55 -070098 close_request = CLOSE_REPO_REQUEST_TEMPLATE.replace("%(description)", description).replace("%(repo_id)", repo_id)
Ray Milkeyd111751b2018-07-24 10:14:02 -070099 url = "https://" + destination_repo_url + "/service/local/staging/profiles" + "/" + SONATYPE_PROFILE + "/finish"
Ray Milkeyebc673f2018-07-20 16:51:55 -0700100 headers = {'Content-Type': 'application/xml'}
101 r = requests.post(url, close_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD))
102
103
104def stage_file(file, repo_id, dest):
Ray Milkeyd111751b2018-07-24 10:14:02 -0700105 if destination_repo_url is not None:
106 # deploy to Nexus repo
107 upload_base = "https://" + destination_repo_url + "/service/local/staging/deployByRepositoryId"
108 url = upload_base + "/" + repo_id + "/" + os.path.dirname(dest) + "/" + os.path.basename(file)
109 headers = {'Content-Type': 'application/xml'}
110 with open(file, 'rb') as f:
111 r = requests.post(url, files={file: f}, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD))
112 else:
113 # deploy to local repo
114 dest_local_repo = os.path.expanduser(local_maven_repo + "/" + dest)
115 dest_local_repo_dir = os.path.dirname(dest_local_repo)
116 if not os.path.isdir(dest_local_repo_dir):
117 os.makedirs(dest_local_repo_dir)
118 shutil.copyfile(src, dest_local_repo)
Ray Milkeyebc673f2018-07-20 16:51:55 -0700119
120
121def stage_files(files, dest):
122 for file in files:
123 stage_file(file=file, repo_id=repo_id, dest=dest)
124
125
126def upload_file(src, dest):
127 files = generate_metadata_files(src)
128 files.append(src)
129 stage_files(files, dest)
130
131
132if __name__ == '__main__':
133 import sys
134
135 if len(sys.argv) < 2:
Ray Milkeyd111751b2018-07-24 10:14:02 -0700136 print 'USAGE: upload-maven-artifacts catalog-file-name [nexus root url]'
Ray Milkeyebc673f2018-07-20 16:51:55 -0700137 sys.exit(1)
138
139 input_list_file = sys.argv[1]
Ray Milkeyd111751b2018-07-24 10:14:02 -0700140
141 local_maven_repo = None
142 destination_repo_url = None
143
144 if len(sys.argv) == 3:
145 destination_repo_url = sys.argv[2]
146 else:
147 local_maven_repo = os.environ.get("MAVEN_REPO")
148 if local_maven_repo is None:
149 local_maven_repo = "~/.m2/repository"
150
151 if destination_repo_url is not None:
152 if SONATYPE_USER is None:
153 print "Environment variable SONATYPE_USER must be set"
154 sys.exit(1)
155
156 if SONATYPE_PASSWORD is None:
157 print "Environment variable SONATYPE_PASSWORD must be set"
158 sys.exit(1)
159
160 if SONATYPE_PROFILE is None:
161 print "Environment variable SONATYPE_PROFILE must be set"
162 sys.exit(1)
163
164 print ("Uploading to remote repo: " + destination_repo_url)
165 else:
166 print ("Installing in local repo: " + local_maven_repo)
167
Ray Milkeyebc673f2018-07-20 16:51:55 -0700168 list_file = open(input_list_file, "r")
169 lines = list_file.readlines()
170 list_file.close()
171
Ray Milkeyd111751b2018-07-24 10:14:02 -0700172 tempdir = tempfile.mkdtemp(prefix="upload-maven-artifacts-")
Ray Milkeyebc673f2018-07-20 16:51:55 -0700173 description = "test repo"
174 repo_id = create_staging_repo(description)
175 for line in lines:
176 s = line.split()
177 src = s[0]
178 dest = s[1]
179 upload_file(src, dest)
180 close_staging_repo(repo_id=repo_id, description=description)
181 shutil.rmtree(tempdir)