blob: 020e860a6d32b7099ae0d61f32f06defe80faec3 [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)."""
4
Martin v. Löwis98858c92005-03-21 21:00:59 +00005from distutils.errors import *
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00006from distutils.core import PyPIRCCommand
Martin v. Löwisf74b9232005-03-22 15:51:14 +00007from distutils.spawn import spawn
Martin v. Löwis98858c92005-03-21 21:00:59 +00008from distutils import log
Thomas Wouters477c8d52006-05-27 19:21:47 +00009from hashlib import md5
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +000010import os, io
Martin v. Löwisca5d8fe2005-03-24 19:40:57 +000011import socket
Martin v. Löwis98858c92005-03-21 21:00:59 +000012import platform
Alexandre Vassalotti1d1eaa42008-05-14 22:59:42 +000013import configparser
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +000014import http.client as httpclient
Martin v. Löwis98858c92005-03-21 21:00:59 +000015import base64
Jeremy Hylton1afc1692008-06-18 20:49:58 +000016import urllib.parse
Martin v. Löwis98858c92005-03-21 21:00:59 +000017
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000018class upload(PyPIRCCommand):
Martin v. Löwis98858c92005-03-21 21:00:59 +000019
20 description = "upload binary package to PyPI"
21
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000022 user_options = PyPIRCCommand.user_options + [
Martin v. Löwisf74b9232005-03-22 15:51:14 +000023 ('sign', 's',
24 'sign files to upload using gpg'),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000025 ('identity=', 'i', 'GPG identity used to sign files'),
Martin v. Löwis98858c92005-03-21 21:00:59 +000026 ]
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000027
28 boolean_options = PyPIRCCommand.boolean_options + ['sign']
Martin v. Löwis98858c92005-03-21 21:00:59 +000029
30 def initialize_options(self):
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000031 PyPIRCCommand.initialize_options(self)
Martin v. Löwis98858c92005-03-21 21:00:59 +000032 self.username = ''
33 self.password = ''
Martin v. Löwis98858c92005-03-21 21:00:59 +000034 self.show_response = 0
Martin v. Löwisf74b9232005-03-22 15:51:14 +000035 self.sign = False
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036 self.identity = None
Martin v. Löwis98858c92005-03-21 21:00:59 +000037
38 def finalize_options(self):
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000039 PyPIRCCommand.finalize_options(self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040 if self.identity and not self.sign:
41 raise DistutilsOptionError(
42 "Must use --sign for --identity to have meaning"
43 )
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000044 config = self._read_pypirc()
45 if config != {}:
46 self.username = config['username']
47 self.password = config['password']
48 self.repository = config['repository']
49 self.realm = config['realm']
Martin v. Löwis98858c92005-03-21 21:00:59 +000050
Tarek Ziadé13f7c3b2009-01-09 00:15:45 +000051 # getting the password from the distribution
52 # if previously set by the register command
53 if not self.password and self.distribution.password:
54 self.password = self.distribution.password
55
Martin v. Löwis98858c92005-03-21 21:00:59 +000056 def run(self):
57 if not self.distribution.dist_files:
58 raise DistutilsOptionError("No dist file created in earlier command")
Martin v. Löwis98da5622005-03-23 18:54:36 +000059 for command, pyversion, filename in self.distribution.dist_files:
60 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000061
Martin v. Löwis98da5622005-03-23 18:54:36 +000062 def upload_file(self, command, pyversion, filename):
Martin v. Löwisf74b9232005-03-22 15:51:14 +000063 # Sign if requested
64 if self.sign:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000065 gpg_args = ["gpg", "--detach-sign", "-a", filename]
66 if self.identity:
67 gpg_args[2:2] = ["--local-user", self.identity]
68 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000069 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000070
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000071 # Fill in the data - send all the meta-data in case we need to
72 # register a new release
Phillip J. Eby5cb78462005-07-07 15:36:20 +000073 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000074 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000075 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000076 # action
77 ':action': 'file_upload',
78 'protcol_version': '1',
79
80 # identify release
81 'name': meta.get_name(),
82 'version': meta.get_version(),
83
84 # file content
85 'content': (os.path.basename(filename),content),
86 'filetype': command,
87 'pyversion': pyversion,
88 'md5_digest': md5(content).hexdigest(),
89
90 # additional meta-data
91 'metadata_version' : '1.0',
92 'summary': meta.get_description(),
93 'home_page': meta.get_url(),
94 'author': meta.get_contact(),
95 'author_email': meta.get_contact_email(),
96 'license': meta.get_licence(),
97 'description': meta.get_long_description(),
98 'keywords': meta.get_keywords(),
99 'platform': meta.get_platforms(),
100 'classifiers': meta.get_classifiers(),
101 'download_url': meta.get_download_url(),
102 # PEP 314
103 'provides': meta.get_provides(),
104 'requires': meta.get_requires(),
105 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000106 }
107 comment = ''
108 if command == 'bdist_rpm':
109 dist, version, id = platform.dist()
110 if dist:
111 comment = 'built for %s %s' % (dist, version)
112 elif command == 'bdist_dumb':
113 comment = 'built for %s' % platform.platform(terse=1)
114 data['comment'] = comment
115
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000116 if self.sign:
117 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
118 open(filename+".asc").read())
119
Martin v. Löwis98858c92005-03-21 21:00:59 +0000120 # set up the authentication
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000121 user_pass = (self.username + ":" + self.password).encode('ascii')
122 # The exact encoding of the authentication string is debated.
123 # Anyway PyPI only accepts ascii for both username or password.
124 auth = "Basic " + base64.encodestring(user_pass).strip().decode('ascii')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000125
126 # Build up the MIME payload for the POST data
127 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000128 sep_boundary = b'\n--' + boundary.encode('ascii')
129 end_boundary = sep_boundary + b'--'
130 body = io.BytesIO()
Martin v. Löwis98858c92005-03-21 21:00:59 +0000131 for key, value in data.items():
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000132 title = '\nContent-Disposition: form-data; name="%s"' % key
Martin v. Löwis98858c92005-03-21 21:00:59 +0000133 # handle multiple entries for the same name
134 if type(value) != type([]):
135 value = [value]
136 for value in value:
137 if type(value) is tuple:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000138 title += '; filename="%s"' % value[0]
Martin v. Löwis98858c92005-03-21 21:00:59 +0000139 value = value[1]
140 else:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000141 value = str(value).encode('utf-8')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000142 body.write(sep_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000143 body.write(title.encode('utf-8'))
144 body.write(b"\n\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000145 body.write(value)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000146 if value and value[-1:] == b'\r':
147 body.write(b'\n') # write an extra newline (lurve Macs)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000148 body.write(end_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000149 body.write(b"\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000150 body = body.getvalue()
151
152 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
153
154 # build the Request
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000155 # We can't use urllib since we need to send the Basic
Martin v. Löwis98858c92005-03-21 21:00:59 +0000156 # auth right with the first request
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000157 # TODO(jhylton): Can we fix urllib?
Martin v. Löwis98858c92005-03-21 21:00:59 +0000158 schema, netloc, url, params, query, fragments = \
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000159 urllib.parse.urlparse(self.repository)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000160 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000161 if schema == 'http':
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000162 http = httpclient.HTTPConnection(netloc)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000163 elif schema == 'https':
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000164 http = httpclient.HTTPSConnection(netloc)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000165 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000166 raise AssertionError("unsupported schema "+schema)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000167
168 data = ''
169 loglevel = log.INFO
170 try:
171 http.connect()
172 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000173 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000174 'multipart/form-data; boundary=%s'%boundary)
175 http.putheader('Content-length', str(len(body)))
176 http.putheader('Authorization', auth)
177 http.endheaders()
178 http.send(body)
Guido van Rossumb940e112007-01-10 16:19:56 +0000179 except socket.error as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000180 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000181 return
182
183 r = http.getresponse()
184 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000185 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000186 log.INFO)
187 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000188 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000189 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000190 if self.show_response:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000191 print('-'*75, r.read(), '-'*75)