blob: d822ba01338af98ae19bc03a58a8ebdfefa3a4a2 [file] [log] [blame]
Jason R. Coombs7ae0fde2014-05-10 13:20:28 -04001"""
2distutils.command.upload
Martin v. Löwis55f1bb82005-03-21 20:56:35 +00003
Jason R. Coombs7ae0fde2014-05-10 13:20:28 -04004Implements the Distutils 'upload' subcommand (upload package to a package
5index).
6"""
Martin v. Löwis98858c92005-03-21 21:00:59 +00007
Jason R. Coombs7ae0fde2014-05-10 13:20:28 -04008import os
9import io
Jason R. Coombsa3846522014-05-10 13:22:43 -040010import hashlib
Tarek Ziadé36797272010-07-22 12:50:05 +000011from base64 import standard_b64encode
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -050012from urllib.request import urlopen, Request, HTTPError
13from urllib.parse import urlparse
Antoine Pitrou2e4d3b12014-06-18 23:07:46 -040014from distutils.errors import DistutilsError, DistutilsOptionError
Jason R. Coombs7ae0fde2014-05-10 13:20:28 -040015from distutils.core import PyPIRCCommand
16from distutils.spawn import spawn
17from distutils import log
Tarek Ziadé36797272010-07-22 12:50:05 +000018
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000019class upload(PyPIRCCommand):
Martin v. Löwis98858c92005-03-21 21:00:59 +000020
21 description = "upload binary package to PyPI"
22
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000023 user_options = PyPIRCCommand.user_options + [
Martin v. Löwisf74b9232005-03-22 15:51:14 +000024 ('sign', 's',
25 'sign files to upload using gpg'),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000026 ('identity=', 'i', 'GPG identity used to sign files'),
Martin v. Löwis98858c92005-03-21 21:00:59 +000027 ]
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000028
29 boolean_options = PyPIRCCommand.boolean_options + ['sign']
Martin v. Löwis98858c92005-03-21 21:00:59 +000030
31 def initialize_options(self):
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000032 PyPIRCCommand.initialize_options(self)
Martin v. Löwis98858c92005-03-21 21:00:59 +000033 self.username = ''
34 self.password = ''
Martin v. Löwis98858c92005-03-21 21:00:59 +000035 self.show_response = 0
Martin v. Löwisf74b9232005-03-22 15:51:14 +000036 self.sign = False
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037 self.identity = None
Martin v. Löwis98858c92005-03-21 21:00:59 +000038
39 def finalize_options(self):
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000040 PyPIRCCommand.finalize_options(self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041 if self.identity and not self.sign:
42 raise DistutilsOptionError(
43 "Must use --sign for --identity to have meaning"
44 )
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000045 config = self._read_pypirc()
46 if config != {}:
47 self.username = config['username']
48 self.password = config['password']
49 self.repository = config['repository']
50 self.realm = config['realm']
Martin v. Löwis98858c92005-03-21 21:00:59 +000051
Tarek Ziadé13f7c3b2009-01-09 00:15:45 +000052 # getting the password from the distribution
53 # if previously set by the register command
54 if not self.password and self.distribution.password:
55 self.password = self.distribution.password
56
Martin v. Löwis98858c92005-03-21 21:00:59 +000057 def run(self):
58 if not self.distribution.dist_files:
Éric Araujo08a69262018-02-18 18:14:54 -050059 msg = ("Must create and upload files in one command "
60 "(e.g. setup.py sdist upload)")
Jason R. Coombs09122f82014-05-10 13:24:58 -040061 raise DistutilsOptionError(msg)
Martin v. Löwis98da5622005-03-23 18:54:36 +000062 for command, pyversion, filename in self.distribution.dist_files:
63 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000064
Martin v. Löwis98da5622005-03-23 18:54:36 +000065 def upload_file(self, command, pyversion, filename):
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -050066 # Makes sure the repository URL is compliant
67 schema, netloc, url, params, query, fragments = \
68 urlparse(self.repository)
69 if params or query or fragments:
70 raise AssertionError("Incompatible url %s" % self.repository)
71
72 if schema not in ('http', 'https'):
73 raise AssertionError("unsupported schema " + schema)
74
Martin v. Löwisf74b9232005-03-22 15:51:14 +000075 # Sign if requested
76 if self.sign:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077 gpg_args = ["gpg", "--detach-sign", "-a", filename]
78 if self.identity:
79 gpg_args[2:2] = ["--local-user", self.identity]
80 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000081 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000082
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000083 # Fill in the data - send all the meta-data in case we need to
84 # register a new release
Éric Araujobee5cef2010-11-05 23:51:56 +000085 f = open(filename,'rb')
86 try:
87 content = f.read()
88 finally:
89 f.close()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000090 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000091 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000092 # action
93 ':action': 'file_upload',
Berker Peksag56fe4742016-06-18 21:42:37 +030094 'protocol_version': '1',
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000095
96 # identify release
97 'name': meta.get_name(),
98 'version': meta.get_version(),
99
100 # file content
101 'content': (os.path.basename(filename),content),
102 'filetype': command,
103 'pyversion': pyversion,
Jason R. Coombsa3846522014-05-10 13:22:43 -0400104 'md5_digest': hashlib.md5(content).hexdigest(),
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +0000105
106 # additional meta-data
Jason R. Coombs7ae0fde2014-05-10 13:20:28 -0400107 'metadata_version': '1.0',
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +0000108 'summary': meta.get_description(),
109 'home_page': meta.get_url(),
110 'author': meta.get_contact(),
111 'author_email': meta.get_contact_email(),
112 'license': meta.get_licence(),
113 'description': meta.get_long_description(),
114 'keywords': meta.get_keywords(),
115 'platform': meta.get_platforms(),
116 'classifiers': meta.get_classifiers(),
117 'download_url': meta.get_download_url(),
118 # PEP 314
119 'provides': meta.get_provides(),
120 'requires': meta.get_requires(),
121 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000122 }
Paul Ganssle4e80f5c2018-12-17 02:59:02 -0500123
124 data['comment'] = ''
Martin v. Löwis98858c92005-03-21 21:00:59 +0000125
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000126 if self.sign:
Mickaël Schoentgen58721a92019-04-08 13:08:48 +0000127 with open(filename + ".asc", "rb") as f:
128 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
129 f.read())
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000130
Martin v. Löwis98858c92005-03-21 21:00:59 +0000131 # set up the authentication
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000132 user_pass = (self.username + ":" + self.password).encode('ascii')
133 # The exact encoding of the authentication string is debated.
134 # Anyway PyPI only accepts ascii for both username or password.
Tarek Ziadé8b9361a2009-12-21 00:02:20 +0000135 auth = "Basic " + standard_b64encode(user_pass).decode('ascii')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000136
137 # Build up the MIME payload for the POST data
138 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
R David Murray9ce69672014-09-27 16:56:15 -0400139 sep_boundary = b'\r\n--' + boundary.encode('ascii')
140 end_boundary = sep_boundary + b'--\r\n'
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000141 body = io.BytesIO()
Martin v. Löwis98858c92005-03-21 21:00:59 +0000142 for key, value in data.items():
R David Murray9ce69672014-09-27 16:56:15 -0400143 title = '\r\nContent-Disposition: form-data; name="%s"' % key
Martin v. Löwis98858c92005-03-21 21:00:59 +0000144 # handle multiple entries for the same name
Jason R. Coombs03756532014-05-10 13:24:18 -0400145 if not isinstance(value, list):
Martin v. Löwis98858c92005-03-21 21:00:59 +0000146 value = [value]
147 for value in value:
Tarek Ziadé36797272010-07-22 12:50:05 +0000148 if type(value) is tuple:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000149 title += '; filename="%s"' % value[0]
Martin v. Löwis98858c92005-03-21 21:00:59 +0000150 value = value[1]
151 else:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000152 value = str(value).encode('utf-8')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000153 body.write(sep_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000154 body.write(title.encode('utf-8'))
R David Murray9ce69672014-09-27 16:56:15 -0400155 body.write(b"\r\n\r\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000156 body.write(value)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000157 body.write(end_boundary)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000158 body = body.getvalue()
159
Jason R. Coombs7ae0fde2014-05-10 13:20:28 -0400160 msg = "Submitting %s to %s" % (filename, self.repository)
161 self.announce(msg, log.INFO)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000162
163 # build the Request
Jason R. Coombs7ae0fde2014-05-10 13:20:28 -0400164 headers = {
165 'Content-type': 'multipart/form-data; boundary=%s' % boundary,
166 'Content-length': str(len(body)),
167 'Authorization': auth,
168 }
Martin v. Löwis98858c92005-03-21 21:00:59 +0000169
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -0500170 request = Request(self.repository, data=body,
171 headers=headers)
172 # send the data
Martin v. Löwis98858c92005-03-21 21:00:59 +0000173 try:
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -0500174 result = urlopen(request)
175 status = result.getcode()
176 reason = result.msg
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -0500177 except HTTPError as e:
178 status = e.code
179 reason = e.msg
Berker Peksag6a8e6262016-06-02 13:45:53 -0700180 except OSError as e:
181 self.announce(str(e), log.ERROR)
182 raise
Martin v. Löwis98858c92005-03-21 21:00:59 +0000183
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -0500184 if status == 200:
185 self.announce('Server response (%s): %s' % (status, reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000186 log.INFO)
Berker Peksag6a8e6262016-06-02 13:45:53 -0700187 if self.show_response:
188 text = self._read_pypi_response(result)
189 msg = '\n'.join(('-' * 75, text, '-' * 75))
190 self.announce(msg, log.INFO)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000191 else:
Antoine Pitrou2e4d3b12014-06-18 23:07:46 -0400192 msg = 'Upload failed (%s): %s' % (status, reason)
193 self.announce(msg, log.ERROR)
194 raise DistutilsError(msg)