blob: 7ba7f5888a7613776b942b2d160f8796a2f0ae6c [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
51 def run(self):
52 if not self.distribution.dist_files:
53 raise DistutilsOptionError("No dist file created in earlier command")
Martin v. Löwis98da5622005-03-23 18:54:36 +000054 for command, pyversion, filename in self.distribution.dist_files:
55 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000056
Martin v. Löwis98da5622005-03-23 18:54:36 +000057 def upload_file(self, command, pyversion, filename):
Martin v. Löwisf74b9232005-03-22 15:51:14 +000058 # Sign if requested
59 if self.sign:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060 gpg_args = ["gpg", "--detach-sign", "-a", filename]
61 if self.identity:
62 gpg_args[2:2] = ["--local-user", self.identity]
63 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000064 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000065
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000066 # Fill in the data - send all the meta-data in case we need to
67 # register a new release
Phillip J. Eby5cb78462005-07-07 15:36:20 +000068 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000069 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000070 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000071 # action
72 ':action': 'file_upload',
73 'protcol_version': '1',
74
75 # identify release
76 'name': meta.get_name(),
77 'version': meta.get_version(),
78
79 # file content
80 'content': (os.path.basename(filename),content),
81 'filetype': command,
82 'pyversion': pyversion,
83 'md5_digest': md5(content).hexdigest(),
84
85 # additional meta-data
86 'metadata_version' : '1.0',
87 'summary': meta.get_description(),
88 'home_page': meta.get_url(),
89 'author': meta.get_contact(),
90 'author_email': meta.get_contact_email(),
91 'license': meta.get_licence(),
92 'description': meta.get_long_description(),
93 'keywords': meta.get_keywords(),
94 'platform': meta.get_platforms(),
95 'classifiers': meta.get_classifiers(),
96 'download_url': meta.get_download_url(),
97 # PEP 314
98 'provides': meta.get_provides(),
99 'requires': meta.get_requires(),
100 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000101 }
102 comment = ''
103 if command == 'bdist_rpm':
104 dist, version, id = platform.dist()
105 if dist:
106 comment = 'built for %s %s' % (dist, version)
107 elif command == 'bdist_dumb':
108 comment = 'built for %s' % platform.platform(terse=1)
109 data['comment'] = comment
110
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000111 if self.sign:
112 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
113 open(filename+".asc").read())
114
Martin v. Löwis98858c92005-03-21 21:00:59 +0000115 # set up the authentication
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000116 user_pass = (self.username + ":" + self.password).encode('ascii')
117 # The exact encoding of the authentication string is debated.
118 # Anyway PyPI only accepts ascii for both username or password.
119 auth = "Basic " + base64.encodestring(user_pass).strip().decode('ascii')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000120
121 # Build up the MIME payload for the POST data
122 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000123 sep_boundary = b'\n--' + boundary.encode('ascii')
124 end_boundary = sep_boundary + b'--'
125 body = io.BytesIO()
Martin v. Löwis98858c92005-03-21 21:00:59 +0000126 for key, value in data.items():
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000127 title = '\nContent-Disposition: form-data; name="%s"' % key
Martin v. Löwis98858c92005-03-21 21:00:59 +0000128 # handle multiple entries for the same name
129 if type(value) != type([]):
130 value = [value]
131 for value in value:
132 if type(value) is tuple:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000133 title += '; filename="%s"' % value[0]
Martin v. Löwis98858c92005-03-21 21:00:59 +0000134 value = value[1]
135 else:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000136 value = str(value).encode('utf-8')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000137 body.write(sep_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000138 body.write(title.encode('utf-8'))
139 body.write(b"\n\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000140 body.write(value)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000141 if value and value[-1:] == b'\r':
142 body.write(b'\n') # write an extra newline (lurve Macs)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000143 body.write(end_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000144 body.write(b"\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000145 body = body.getvalue()
146
147 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
148
149 # build the Request
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000150 # We can't use urllib since we need to send the Basic
Martin v. Löwis98858c92005-03-21 21:00:59 +0000151 # auth right with the first request
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000152 # TODO(jhylton): Can we fix urllib?
Martin v. Löwis98858c92005-03-21 21:00:59 +0000153 schema, netloc, url, params, query, fragments = \
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000154 urllib.parse.urlparse(self.repository)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000155 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000156 if schema == 'http':
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000157 http = httpclient.HTTPConnection(netloc)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000158 elif schema == 'https':
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000159 http = httpclient.HTTPSConnection(netloc)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000160 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000161 raise AssertionError("unsupported schema "+schema)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000162
163 data = ''
164 loglevel = log.INFO
165 try:
166 http.connect()
167 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000168 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000169 'multipart/form-data; boundary=%s'%boundary)
170 http.putheader('Content-length', str(len(body)))
171 http.putheader('Authorization', auth)
172 http.endheaders()
173 http.send(body)
Guido van Rossumb940e112007-01-10 16:19:56 +0000174 except socket.error as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000175 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000176 return
177
178 r = http.getresponse()
179 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000180 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000181 log.INFO)
182 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000183 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000184 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000185 if self.show_response:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000186 print('-'*75, r.read(), '-'*75)