blob: 1c4fc48a12935993f747e08cfef583a14085fcb7 [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
Tarek Ziadé36797272010-07-22 12:50:05 +000010import platform
Jason R. Coombsa3846522014-05-10 13:22:43 -040011import hashlib
Tarek Ziadé36797272010-07-22 12:50:05 +000012from base64 import standard_b64encode
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -050013from urllib.request import urlopen, Request, HTTPError
14from urllib.parse import urlparse
Antoine Pitrou2e4d3b12014-06-18 23:07:46 -040015from distutils.errors import DistutilsError, DistutilsOptionError
Jason R. Coombs7ae0fde2014-05-10 13:20:28 -040016from distutils.core import PyPIRCCommand
17from distutils.spawn import spawn
18from distutils import log
Tarek Ziadé36797272010-07-22 12:50:05 +000019
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000020class upload(PyPIRCCommand):
Martin v. Löwis98858c92005-03-21 21:00:59 +000021
22 description = "upload binary package to PyPI"
23
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000024 user_options = PyPIRCCommand.user_options + [
Martin v. Löwisf74b9232005-03-22 15:51:14 +000025 ('sign', 's',
26 'sign files to upload using gpg'),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027 ('identity=', 'i', 'GPG identity used to sign files'),
Martin v. Löwis98858c92005-03-21 21:00:59 +000028 ]
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000029
30 boolean_options = PyPIRCCommand.boolean_options + ['sign']
Martin v. Löwis98858c92005-03-21 21:00:59 +000031
32 def initialize_options(self):
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000033 PyPIRCCommand.initialize_options(self)
Martin v. Löwis98858c92005-03-21 21:00:59 +000034 self.username = ''
35 self.password = ''
Martin v. Löwis98858c92005-03-21 21:00:59 +000036 self.show_response = 0
Martin v. Löwisf74b9232005-03-22 15:51:14 +000037 self.sign = False
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038 self.identity = None
Martin v. Löwis98858c92005-03-21 21:00:59 +000039
40 def finalize_options(self):
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000041 PyPIRCCommand.finalize_options(self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042 if self.identity and not self.sign:
43 raise DistutilsOptionError(
44 "Must use --sign for --identity to have meaning"
45 )
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000046 config = self._read_pypirc()
47 if config != {}:
48 self.username = config['username']
49 self.password = config['password']
50 self.repository = config['repository']
51 self.realm = config['realm']
Martin v. Löwis98858c92005-03-21 21:00:59 +000052
Tarek Ziadé13f7c3b2009-01-09 00:15:45 +000053 # getting the password from the distribution
54 # if previously set by the register command
55 if not self.password and self.distribution.password:
56 self.password = self.distribution.password
57
Martin v. Löwis98858c92005-03-21 21:00:59 +000058 def run(self):
59 if not self.distribution.dist_files:
Jason R. Coombs09122f82014-05-10 13:24:58 -040060 msg = "No dist file created in earlier command"
61 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',
94 'protcol_version': '1',
95
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 }
123 comment = ''
124 if command == 'bdist_rpm':
125 dist, version, id = platform.dist()
126 if dist:
127 comment = 'built for %s %s' % (dist, version)
128 elif command == 'bdist_dumb':
129 comment = 'built for %s' % platform.platform(terse=1)
130 data['comment'] = comment
131
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000132 if self.sign:
133 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
Antoine Pitrou24319ac2012-06-29 01:05:26 +0200134 open(filename+".asc", "rb").read())
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000135
Martin v. Löwis98858c92005-03-21 21:00:59 +0000136 # set up the authentication
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000137 user_pass = (self.username + ":" + self.password).encode('ascii')
138 # The exact encoding of the authentication string is debated.
139 # Anyway PyPI only accepts ascii for both username or password.
Tarek Ziadé8b9361a2009-12-21 00:02:20 +0000140 auth = "Basic " + standard_b64encode(user_pass).decode('ascii')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000141
142 # Build up the MIME payload for the POST data
143 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
R David Murray9ce69672014-09-27 16:56:15 -0400144 sep_boundary = b'\r\n--' + boundary.encode('ascii')
145 end_boundary = sep_boundary + b'--\r\n'
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000146 body = io.BytesIO()
Martin v. Löwis98858c92005-03-21 21:00:59 +0000147 for key, value in data.items():
R David Murray9ce69672014-09-27 16:56:15 -0400148 title = '\r\nContent-Disposition: form-data; name="%s"' % key
Martin v. Löwis98858c92005-03-21 21:00:59 +0000149 # handle multiple entries for the same name
Jason R. Coombs03756532014-05-10 13:24:18 -0400150 if not isinstance(value, list):
Martin v. Löwis98858c92005-03-21 21:00:59 +0000151 value = [value]
152 for value in value:
Tarek Ziadé36797272010-07-22 12:50:05 +0000153 if type(value) is tuple:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000154 title += '; filename="%s"' % value[0]
Martin v. Löwis98858c92005-03-21 21:00:59 +0000155 value = value[1]
156 else:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000157 value = str(value).encode('utf-8')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000158 body.write(sep_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000159 body.write(title.encode('utf-8'))
R David Murray9ce69672014-09-27 16:56:15 -0400160 body.write(b"\r\n\r\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000161 body.write(value)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000162 if value and value[-1:] == b'\r':
163 body.write(b'\n') # write an extra newline (lurve Macs)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000164 body.write(end_boundary)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000165 body = body.getvalue()
166
Jason R. Coombs7ae0fde2014-05-10 13:20:28 -0400167 msg = "Submitting %s to %s" % (filename, self.repository)
168 self.announce(msg, log.INFO)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000169
170 # build the Request
Jason R. Coombs7ae0fde2014-05-10 13:20:28 -0400171 headers = {
172 'Content-type': 'multipart/form-data; boundary=%s' % boundary,
173 'Content-length': str(len(body)),
174 'Authorization': auth,
175 }
Martin v. Löwis98858c92005-03-21 21:00:59 +0000176
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -0500177 request = Request(self.repository, data=body,
178 headers=headers)
179 # send the data
Martin v. Löwis98858c92005-03-21 21:00:59 +0000180 try:
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -0500181 result = urlopen(request)
182 status = result.getcode()
183 reason = result.msg
Andrew Svetlov0832af62012-12-18 23:10:48 +0200184 except OSError as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000185 self.announce(str(e), log.ERROR)
Antoine Pitrou2e4d3b12014-06-18 23:07:46 -0400186 raise
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -0500187 except HTTPError as e:
188 status = e.code
189 reason = e.msg
Martin v. Löwis98858c92005-03-21 21:00:59 +0000190
Jason R. Coombsa2ebfd02013-11-10 18:50:10 -0500191 if status == 200:
192 self.announce('Server response (%s): %s' % (status, reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000193 log.INFO)
194 else:
Antoine Pitrou2e4d3b12014-06-18 23:07:46 -0400195 msg = 'Upload failed (%s): %s' % (status, reason)
196 self.announce(msg, log.ERROR)
197 raise DistutilsError(msg)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000198 if self.show_response:
Antoine Pitrou335a5122013-12-22 18:13:51 +0100199 text = self._read_pypi_response(result)
200 msg = '\n'.join(('-' * 75, text, '-' * 75))
Éric Araujo480504b2010-09-07 23:08:57 +0000201 self.announce(msg, log.INFO)