blob: 2692377558154e0a8ce6d1ab7b2b5eba9e8aa756 [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)."""
Tarek Ziadé38e3d512009-02-27 12:58:56 +00004import sys
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +00005import os, io
Martin v. Löwisca5d8fe2005-03-24 19:40:57 +00006import socket
Martin v. Löwis98858c92005-03-21 21:00:59 +00007import platform
Alexandre Vassalotti1d1eaa42008-05-14 22:59:42 +00008import configparser
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +00009import http.client as httpclient
Martin v. Löwis98858c92005-03-21 21:00:59 +000010import base64
Jeremy Hylton1afc1692008-06-18 20:49:58 +000011import urllib.parse
Tarek Ziadé6f6f9462009-06-28 21:10:49 +000012from hashlib import md5
Martin v. Löwis98858c92005-03-21 21:00:59 +000013
Tarek Ziadé6f6f9462009-06-28 21:10:49 +000014from distutils.errors import *
15from distutils.core import PyPIRCCommand
16from distutils.spawn import spawn
17from distutils import log
Tarek Ziadé38e3d512009-02-27 12:58:56 +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:
59 raise DistutilsOptionError("No dist file created in earlier command")
Martin v. Löwis98da5622005-03-23 18:54:36 +000060 for command, pyversion, filename in self.distribution.dist_files:
61 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000062
Martin v. Löwis98da5622005-03-23 18:54:36 +000063 def upload_file(self, command, pyversion, filename):
Martin v. Löwisf74b9232005-03-22 15:51:14 +000064 # Sign if requested
65 if self.sign:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000066 gpg_args = ["gpg", "--detach-sign", "-a", filename]
67 if self.identity:
68 gpg_args[2:2] = ["--local-user", self.identity]
69 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000070 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000071
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000072 # Fill in the data - send all the meta-data in case we need to
73 # register a new release
Phillip J. Eby5cb78462005-07-07 15:36:20 +000074 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000075 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000076 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000077 # action
78 ':action': 'file_upload',
79 'protcol_version': '1',
80
81 # identify release
82 'name': meta.get_name(),
83 'version': meta.get_version(),
84
85 # file content
86 'content': (os.path.basename(filename),content),
87 'filetype': command,
88 'pyversion': pyversion,
89 'md5_digest': md5(content).hexdigest(),
90
91 # additional meta-data
92 'metadata_version' : '1.0',
93 'summary': meta.get_description(),
94 'home_page': meta.get_url(),
95 'author': meta.get_contact(),
96 'author_email': meta.get_contact_email(),
97 'license': meta.get_licence(),
98 'description': meta.get_long_description(),
99 'keywords': meta.get_keywords(),
100 'platform': meta.get_platforms(),
101 'classifiers': meta.get_classifiers(),
102 'download_url': meta.get_download_url(),
103 # PEP 314
104 'provides': meta.get_provides(),
105 'requires': meta.get_requires(),
106 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000107 }
108 comment = ''
109 if command == 'bdist_rpm':
110 dist, version, id = platform.dist()
111 if dist:
112 comment = 'built for %s %s' % (dist, version)
113 elif command == 'bdist_dumb':
114 comment = 'built for %s' % platform.platform(terse=1)
115 data['comment'] = comment
116
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000117 if self.sign:
118 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
119 open(filename+".asc").read())
120
Martin v. Löwis98858c92005-03-21 21:00:59 +0000121 # set up the authentication
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000122 user_pass = (self.username + ":" + self.password).encode('ascii')
123 # The exact encoding of the authentication string is debated.
124 # Anyway PyPI only accepts ascii for both username or password.
Georg Brandl706824f2009-06-04 09:42:55 +0000125 auth = "Basic " + base64.encodebytes(user_pass).strip().decode('ascii')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000126
127 # Build up the MIME payload for the POST data
128 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000129 sep_boundary = b'\n--' + boundary.encode('ascii')
130 end_boundary = sep_boundary + b'--'
131 body = io.BytesIO()
Martin v. Löwis98858c92005-03-21 21:00:59 +0000132 for key, value in data.items():
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000133 title = '\nContent-Disposition: form-data; name="%s"' % key
Martin v. Löwis98858c92005-03-21 21:00:59 +0000134 # handle multiple entries for the same name
Tarek Ziadé6f6f9462009-06-28 21:10:49 +0000135 if not isinstance(value, list):
Martin v. Löwis98858c92005-03-21 21:00:59 +0000136 value = [value]
137 for value in value:
Tarek Ziadé6f6f9462009-06-28 21:10:49 +0000138 if isinstance(value, tuple):
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000139 title += '; filename="%s"' % value[0]
Martin v. Löwis98858c92005-03-21 21:00:59 +0000140 value = value[1]
141 else:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000142 value = str(value).encode('utf-8')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000143 body.write(sep_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000144 body.write(title.encode('utf-8'))
145 body.write(b"\n\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000146 body.write(value)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000147 if value and value[-1:] == b'\r':
148 body.write(b'\n') # write an extra newline (lurve Macs)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000149 body.write(end_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000150 body.write(b"\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000151 body = body.getvalue()
152
153 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
154
155 # build the Request
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000156 # We can't use urllib since we need to send the Basic
Martin v. Löwis98858c92005-03-21 21:00:59 +0000157 # auth right with the first request
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000158 # TODO(jhylton): Can we fix urllib?
Martin v. Löwis98858c92005-03-21 21:00:59 +0000159 schema, netloc, url, params, query, fragments = \
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000160 urllib.parse.urlparse(self.repository)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000161 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000162 if schema == 'http':
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000163 http = httpclient.HTTPConnection(netloc)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000164 elif schema == 'https':
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000165 http = httpclient.HTTPSConnection(netloc)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000166 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000167 raise AssertionError("unsupported schema "+schema)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000168
169 data = ''
170 loglevel = log.INFO
171 try:
172 http.connect()
173 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000174 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000175 'multipart/form-data; boundary=%s'%boundary)
176 http.putheader('Content-length', str(len(body)))
177 http.putheader('Authorization', auth)
178 http.endheaders()
179 http.send(body)
Guido van Rossumb940e112007-01-10 16:19:56 +0000180 except socket.error as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000181 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000182 return
183
184 r = http.getresponse()
185 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000186 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000187 log.INFO)
188 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000189 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000190 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000191 if self.show_response:
Tarek Ziadébaf51802009-03-31 21:37:16 +0000192 self.announce('-'*75, r.read(), '-'*75)