blob: 88990b2c6bd44bb67b07a428a90254f9ef153e1b [file] [log] [blame]
Martin v. Löwis55f1bb82005-03-21 20:56:35 +00001"""distutils.command.upload
2
3Implements the Distutils 'upload' subcommand (upload package to PyPI)."""
Martin v. Löwis98858c92005-03-21 21:00:59 +00004
Tarek Ziadé36797272010-07-22 12:50:05 +00005from distutils.errors import *
Tarek Ziadé6f6f9462009-06-28 21:10:49 +00006from distutils.core import PyPIRCCommand
7from distutils.spawn import spawn
8from distutils import log
Tarek Ziadé36797272010-07-22 12:50:05 +00009import sys
10import os, io
11import socket
12import platform
13import configparser
14import http.client as httpclient
15from base64 import standard_b64encode
16import urllib.parse
17
18# this keeps compatibility for 2.3 and 2.4
19if sys.version < "2.5":
20 from md5 import md5
21else:
22 from hashlib import md5
Tarek Ziadé38e3d512009-02-27 12:58:56 +000023
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000024class upload(PyPIRCCommand):
Martin v. Löwis98858c92005-03-21 21:00:59 +000025
26 description = "upload binary package to PyPI"
27
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000028 user_options = PyPIRCCommand.user_options + [
Martin v. Löwisf74b9232005-03-22 15:51:14 +000029 ('sign', 's',
30 'sign files to upload using gpg'),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000031 ('identity=', 'i', 'GPG identity used to sign files'),
Martin v. Löwis98858c92005-03-21 21:00:59 +000032 ]
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000033
34 boolean_options = PyPIRCCommand.boolean_options + ['sign']
Martin v. Löwis98858c92005-03-21 21:00:59 +000035
36 def initialize_options(self):
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000037 PyPIRCCommand.initialize_options(self)
Martin v. Löwis98858c92005-03-21 21:00:59 +000038 self.username = ''
39 self.password = ''
Martin v. Löwis98858c92005-03-21 21:00:59 +000040 self.show_response = 0
Martin v. Löwisf74b9232005-03-22 15:51:14 +000041 self.sign = False
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042 self.identity = None
Martin v. Löwis98858c92005-03-21 21:00:59 +000043
44 def finalize_options(self):
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000045 PyPIRCCommand.finalize_options(self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000046 if self.identity and not self.sign:
47 raise DistutilsOptionError(
48 "Must use --sign for --identity to have meaning"
49 )
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000050 config = self._read_pypirc()
51 if config != {}:
52 self.username = config['username']
53 self.password = config['password']
54 self.repository = config['repository']
55 self.realm = config['realm']
Martin v. Löwis98858c92005-03-21 21:00:59 +000056
Tarek Ziadé13f7c3b2009-01-09 00:15:45 +000057 # getting the password from the distribution
58 # if previously set by the register command
59 if not self.password and self.distribution.password:
60 self.password = self.distribution.password
61
Martin v. Löwis98858c92005-03-21 21:00:59 +000062 def run(self):
63 if not self.distribution.dist_files:
64 raise DistutilsOptionError("No dist file created in earlier command")
Martin v. Löwis98da5622005-03-23 18:54:36 +000065 for command, pyversion, filename in self.distribution.dist_files:
66 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000067
Martin v. Löwis98da5622005-03-23 18:54:36 +000068 def upload_file(self, command, pyversion, filename):
Martin v. Löwisf74b9232005-03-22 15:51:14 +000069 # Sign if requested
70 if self.sign:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071 gpg_args = ["gpg", "--detach-sign", "-a", filename]
72 if self.identity:
73 gpg_args[2:2] = ["--local-user", self.identity]
74 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000075 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000076
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000077 # Fill in the data - send all the meta-data in case we need to
78 # register a new release
Éric Araujobee5cef2010-11-05 23:51:56 +000079 f = open(filename,'rb')
80 try:
81 content = f.read()
82 finally:
83 f.close()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000084 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000085 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000086 # action
87 ':action': 'file_upload',
88 'protcol_version': '1',
89
90 # identify release
91 'name': meta.get_name(),
92 'version': meta.get_version(),
93
94 # file content
95 'content': (os.path.basename(filename),content),
96 'filetype': command,
97 'pyversion': pyversion,
98 'md5_digest': md5(content).hexdigest(),
99
100 # additional meta-data
101 'metadata_version' : '1.0',
102 'summary': meta.get_description(),
103 'home_page': meta.get_url(),
104 'author': meta.get_contact(),
105 'author_email': meta.get_contact_email(),
106 'license': meta.get_licence(),
107 'description': meta.get_long_description(),
108 'keywords': meta.get_keywords(),
109 'platform': meta.get_platforms(),
110 'classifiers': meta.get_classifiers(),
111 'download_url': meta.get_download_url(),
112 # PEP 314
113 'provides': meta.get_provides(),
114 'requires': meta.get_requires(),
115 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000116 }
117 comment = ''
118 if command == 'bdist_rpm':
119 dist, version, id = platform.dist()
120 if dist:
121 comment = 'built for %s %s' % (dist, version)
122 elif command == 'bdist_dumb':
123 comment = 'built for %s' % platform.platform(terse=1)
124 data['comment'] = comment
125
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000126 if self.sign:
127 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
Antoine Pitrou24319ac2012-06-29 01:05:26 +0200128 open(filename+".asc", "rb").read())
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000129
Martin v. Löwis98858c92005-03-21 21:00:59 +0000130 # set up the authentication
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000131 user_pass = (self.username + ":" + self.password).encode('ascii')
132 # The exact encoding of the authentication string is debated.
133 # Anyway PyPI only accepts ascii for both username or password.
Tarek Ziadé8b9361a2009-12-21 00:02:20 +0000134 auth = "Basic " + standard_b64encode(user_pass).decode('ascii')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000135
136 # Build up the MIME payload for the POST data
137 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000138 sep_boundary = b'\n--' + boundary.encode('ascii')
139 end_boundary = sep_boundary + b'--'
140 body = io.BytesIO()
Martin v. Löwis98858c92005-03-21 21:00:59 +0000141 for key, value in data.items():
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000142 title = '\nContent-Disposition: form-data; name="%s"' % key
Martin v. Löwis98858c92005-03-21 21:00:59 +0000143 # handle multiple entries for the same name
Tarek Ziadé36797272010-07-22 12:50:05 +0000144 if type(value) != type([]):
Martin v. Löwis98858c92005-03-21 21:00:59 +0000145 value = [value]
146 for value in value:
Tarek Ziadé36797272010-07-22 12:50:05 +0000147 if type(value) is tuple:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000148 title += '; filename="%s"' % value[0]
Martin v. Löwis98858c92005-03-21 21:00:59 +0000149 value = value[1]
150 else:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000151 value = str(value).encode('utf-8')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000152 body.write(sep_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000153 body.write(title.encode('utf-8'))
154 body.write(b"\n\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000155 body.write(value)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000156 if value and value[-1:] == b'\r':
157 body.write(b'\n') # write an extra newline (lurve Macs)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000158 body.write(end_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000159 body.write(b"\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000160 body = body.getvalue()
161
162 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
163
164 # build the Request
Tarek Ziadé36797272010-07-22 12:50:05 +0000165 # We can't use urllib since we need to send the Basic
166 # auth right with the first request
167 # TODO(jhylton): Can we fix urllib?
168 schema, netloc, url, params, query, fragments = \
169 urllib.parse.urlparse(self.repository)
170 assert not params and not query and not fragments
171 if schema == 'http':
172 http = httpclient.HTTPConnection(netloc)
173 elif schema == 'https':
174 http = httpclient.HTTPSConnection(netloc)
175 else:
176 raise AssertionError("unsupported schema "+schema)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000177
Tarek Ziadé36797272010-07-22 12:50:05 +0000178 data = ''
179 loglevel = log.INFO
Martin v. Löwis98858c92005-03-21 21:00:59 +0000180 try:
Tarek Ziadé36797272010-07-22 12:50:05 +0000181 http.connect()
182 http.putrequest("POST", url)
183 http.putheader('Content-type',
184 'multipart/form-data; boundary=%s'%boundary)
185 http.putheader('Content-length', str(len(body)))
186 http.putheader('Authorization', auth)
187 http.endheaders()
188 http.send(body)
Andrew Svetlov0832af62012-12-18 23:10:48 +0200189 except OSError as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000190 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000191 return
192
Tarek Ziadé36797272010-07-22 12:50:05 +0000193 r = http.getresponse()
194 if r.status == 200:
195 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000196 log.INFO)
197 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000198 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000199 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000200 if self.show_response:
Éric Araujo51e01a62010-09-08 00:00:45 +0000201 msg = '\n'.join(('-' * 75, r.read(), '-' * 75))
Éric Araujo480504b2010-09-07 23:08:57 +0000202 self.announce(msg, log.INFO)