blob: f602fbeb68697018b60d65ccaf1641c6334bcaef [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
Phillip J. Eby5cb78462005-07-07 15:36:20 +000079 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000080 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000081 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000082 # action
83 ':action': 'file_upload',
84 'protcol_version': '1',
85
86 # identify release
87 'name': meta.get_name(),
88 'version': meta.get_version(),
89
90 # file content
91 'content': (os.path.basename(filename),content),
92 'filetype': command,
93 'pyversion': pyversion,
94 'md5_digest': md5(content).hexdigest(),
95
96 # additional meta-data
97 'metadata_version' : '1.0',
98 'summary': meta.get_description(),
99 'home_page': meta.get_url(),
100 'author': meta.get_contact(),
101 'author_email': meta.get_contact_email(),
102 'license': meta.get_licence(),
103 'description': meta.get_long_description(),
104 'keywords': meta.get_keywords(),
105 'platform': meta.get_platforms(),
106 'classifiers': meta.get_classifiers(),
107 'download_url': meta.get_download_url(),
108 # PEP 314
109 'provides': meta.get_provides(),
110 'requires': meta.get_requires(),
111 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000112 }
113 comment = ''
114 if command == 'bdist_rpm':
115 dist, version, id = platform.dist()
116 if dist:
117 comment = 'built for %s %s' % (dist, version)
118 elif command == 'bdist_dumb':
119 comment = 'built for %s' % platform.platform(terse=1)
120 data['comment'] = comment
121
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000122 if self.sign:
123 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
124 open(filename+".asc").read())
125
Martin v. Löwis98858c92005-03-21 21:00:59 +0000126 # set up the authentication
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000127 user_pass = (self.username + ":" + self.password).encode('ascii')
128 # The exact encoding of the authentication string is debated.
129 # Anyway PyPI only accepts ascii for both username or password.
Tarek Ziadé8b9361a2009-12-21 00:02:20 +0000130 auth = "Basic " + standard_b64encode(user_pass).decode('ascii')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000131
132 # Build up the MIME payload for the POST data
133 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000134 sep_boundary = b'\n--' + boundary.encode('ascii')
135 end_boundary = sep_boundary + b'--'
136 body = io.BytesIO()
Martin v. Löwis98858c92005-03-21 21:00:59 +0000137 for key, value in data.items():
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000138 title = '\nContent-Disposition: form-data; name="%s"' % key
Martin v. Löwis98858c92005-03-21 21:00:59 +0000139 # handle multiple entries for the same name
Tarek Ziadé36797272010-07-22 12:50:05 +0000140 if type(value) != type([]):
Martin v. Löwis98858c92005-03-21 21:00:59 +0000141 value = [value]
142 for value in value:
Tarek Ziadé36797272010-07-22 12:50:05 +0000143 if type(value) is tuple:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000144 title += '; filename="%s"' % value[0]
Martin v. Löwis98858c92005-03-21 21:00:59 +0000145 value = value[1]
146 else:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000147 value = str(value).encode('utf-8')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000148 body.write(sep_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000149 body.write(title.encode('utf-8'))
150 body.write(b"\n\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000151 body.write(value)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000152 if value and value[-1:] == b'\r':
153 body.write(b'\n') # write an extra newline (lurve Macs)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000154 body.write(end_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000155 body.write(b"\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000156 body = body.getvalue()
157
158 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
159
160 # build the Request
Tarek Ziadé36797272010-07-22 12:50:05 +0000161 # We can't use urllib since we need to send the Basic
162 # auth right with the first request
163 # TODO(jhylton): Can we fix urllib?
164 schema, netloc, url, params, query, fragments = \
165 urllib.parse.urlparse(self.repository)
166 assert not params and not query and not fragments
167 if schema == 'http':
168 http = httpclient.HTTPConnection(netloc)
169 elif schema == 'https':
170 http = httpclient.HTTPSConnection(netloc)
171 else:
172 raise AssertionError("unsupported schema "+schema)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000173
Tarek Ziadé36797272010-07-22 12:50:05 +0000174 data = ''
175 loglevel = log.INFO
Martin v. Löwis98858c92005-03-21 21:00:59 +0000176 try:
Tarek Ziadé36797272010-07-22 12:50:05 +0000177 http.connect()
178 http.putrequest("POST", url)
179 http.putheader('Content-type',
180 'multipart/form-data; boundary=%s'%boundary)
181 http.putheader('Content-length', str(len(body)))
182 http.putheader('Authorization', auth)
183 http.endheaders()
184 http.send(body)
Guido van Rossumb940e112007-01-10 16:19:56 +0000185 except socket.error as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000186 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000187 return
188
Tarek Ziadé36797272010-07-22 12:50:05 +0000189 r = http.getresponse()
190 if r.status == 200:
191 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000192 log.INFO)
193 else:
Tarek Ziadé36797272010-07-22 12:50:05 +0000194 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000195 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000196 if self.show_response:
Tarek Ziadé36797272010-07-22 12:50:05 +0000197 self.announce('-'*75, r.read(), '-'*75)