blob: ef76d0313c70f5465471bfde85fda22b2f538888 [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")
27UPLOAD_BASE="https://oss.sonatype.org/service/local/staging/deployByRepositoryId"
28
29CREATE_REPO_REQUEST_TEMPLATE = '''\
30<promoteRequest>
31 <data>
32 <description>%(description)</description>
33 </data>
34</promoteRequest>
35'''
36
37CLOSE_REPO_REQUEST_TEMPLATE = '''\
38<promoteRequest>
39 <data>
40 <description>%(description)</description>
41 <stagedRepositoryId>%(repo_id)</stagedRepositoryId>
42 </data>
43</promoteRequest>
44'''
45
46def hashlib_compute(hash, input_file, output_file):
47 with open(input_file, 'rb') as f:
48 for block in iter(lambda: f.read(100000), b''):
49 hash.update(block)
50 md5_string = hash.hexdigest()
51 output = open(output_file, "w")
52 output.write(md5_string + "\n")
53 f.close()
54 output.close()
55
56
57def generate_metadata_files(input_file):
58 # create a temporary directory to hold the metadata files
59 global tempdir
60 base_metadata_filename = tempdir + "/" + os.path.basename(input_file)
61
62 files = []
63
64 # generate the signature file
65 signature_filename = base_metadata_filename + ".asc"
66 call(["gpg", "--armor", "--detach-sig", "--output", signature_filename, input_file])
67 files.append(signature_filename)
68
69 # generate the md5 checksum file
70 md5_filename = base_metadata_filename + ".md5"
71 md5 = hashlib.md5()
72 hashlib_compute(md5, input_file, md5_filename)
73 files.append(md5_filename)
74
75 # generate the SHA checksum file
76 sha1_filename = base_metadata_filename + ".sha1"
77 sha1 = hashlib.sha1()
78 hashlib_compute(sha1, input_file, sha1_filename)
79 files.append(sha1_filename)
80
81 return files
82
83
84def create_staging_repo(description):
85 create_request = CREATE_REPO_REQUEST_TEMPLATE.replace("%(description)", description)
86 url = "https://oss.sonatype.org/service/local/staging/profiles/87c0ccf277f362/start"
87 headers = {'Content-Type': 'application/xml'}
88 r = requests.post(url, create_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD))
89 root = xml.etree.ElementTree.fromstring(r.text)
90 repo_id = root.find("data").find("stagedRepositoryId").text
91 print repo_id
92 return repo_id
93
94
95def close_staging_repo(description, repo_id):
96 close_request = CLOSE_REPO_REQUEST_TEMPLATE.replace("%(description)", description).replace("%(repo_id)", repo_id)
97 url = "https://oss.sonatype.org/service/local/staging/profiles/87c0ccf277f362/finish"
98 headers = {'Content-Type': 'application/xml'}
99 r = requests.post(url, close_request, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD))
100
101
102def stage_file(file, repo_id, dest):
103 url = UPLOAD_BASE + "/" + repo_id + "/" + os.path.dirname(dest) + "/" + os.path.basename(file)
104 headers = {'Content-Type': 'application/xml'}
105 print("Uploading " + file)
106 with open(file, 'rb') as f:
107 r = requests.post(url, files={file: f}, headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD))
108
109
110def stage_files(files, dest):
111 for file in files:
112 stage_file(file=file, repo_id=repo_id, dest=dest)
113
114
115def upload_file(src, dest):
116 files = generate_metadata_files(src)
117 files.append(src)
118 stage_files(files, dest)
119
120
121if __name__ == '__main__':
122 import sys
123
124 if len(sys.argv) < 2:
125 print 'USAGE'
126 sys.exit(1)
127
128 if SONATYPE_USER == None:
129 print "Environment variable SONATYPE_USER must be set"
130 sys.exit(1)
131
132 if SONATYPE_PASSWORD == None:
133 print "Environment variable SONATYPE_PASSWORD must be set"
134 sys.exit(1)
135
136 if SONATYPE_PROFILE == None:
137 print "Environment variable SONATYPE_PROFILE must be set"
138 sys.exit(1)
139
140 input_list_file = sys.argv[1]
141 list_file = open(input_list_file, "r")
142 lines = list_file.readlines()
143 list_file.close()
144
145 tempdir = tempfile.mkdtemp(prefix="upload-sonatype-")
146 description = "test repo"
147 repo_id = create_staging_repo(description)
148 for line in lines:
149 s = line.split()
150 src = s[0]
151 dest = s[1]
152 upload_file(src, dest)
153 close_staging_repo(repo_id=repo_id, description=description)
154 shutil.rmtree(tempdir)