blob: defdda642b1b33eb08e151592abcff3fc24dc967 [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
Tarek Ziadé25bd2062009-06-28 21:26:27 +00008from urllib.request import urlopen, Request, HTTPError
Martin v. Löwis98858c92005-03-21 21:00:59 +00009import base64
Tarek Ziadé25bd2062009-06-28 21:26:27 +000010from urllib.parse import urlparse
Tarek Ziadé6f6f9462009-06-28 21:10:49 +000011from hashlib import md5
Martin v. Löwis98858c92005-03-21 21:00:59 +000012
Tarek Ziadé6f6f9462009-06-28 21:10:49 +000013from distutils.errors import *
14from distutils.core import PyPIRCCommand
15from distutils.spawn import spawn
16from distutils import log
Tarek Ziadé38e3d512009-02-27 12:58:56 +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):
Tarek Ziadé25bd2062009-06-28 21:26:27 +000063 # Makes sure the repository URL is compliant
64 schema, netloc, url, params, query, fragments = \
65 urlparse(self.repository)
66 if params or query or fragments:
67 raise AssertionError("Incompatible url %s" % self.repository)
68
69 if schema not in ('http', 'https'):
70 raise AssertionError("unsupported schema " + schema)
71
Martin v. Löwisf74b9232005-03-22 15:51:14 +000072 # Sign if requested
73 if self.sign:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000074 gpg_args = ["gpg", "--detach-sign", "-a", filename]
75 if self.identity:
76 gpg_args[2:2] = ["--local-user", self.identity]
77 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000078 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000079
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000080 # Fill in the data - send all the meta-data in case we need to
81 # register a new release
Phillip J. Eby5cb78462005-07-07 15:36:20 +000082 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000083 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000084 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000085 # action
86 ':action': 'file_upload',
87 'protcol_version': '1',
88
89 # identify release
90 'name': meta.get_name(),
91 'version': meta.get_version(),
92
93 # file content
94 'content': (os.path.basename(filename),content),
95 'filetype': command,
96 'pyversion': pyversion,
97 'md5_digest': md5(content).hexdigest(),
98
99 # additional meta-data
100 'metadata_version' : '1.0',
101 'summary': meta.get_description(),
102 'home_page': meta.get_url(),
103 'author': meta.get_contact(),
104 'author_email': meta.get_contact_email(),
105 'license': meta.get_licence(),
106 'description': meta.get_long_description(),
107 'keywords': meta.get_keywords(),
108 'platform': meta.get_platforms(),
109 'classifiers': meta.get_classifiers(),
110 'download_url': meta.get_download_url(),
111 # PEP 314
112 'provides': meta.get_provides(),
113 'requires': meta.get_requires(),
114 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000115 }
116 comment = ''
117 if command == 'bdist_rpm':
118 dist, version, id = platform.dist()
119 if dist:
120 comment = 'built for %s %s' % (dist, version)
121 elif command == 'bdist_dumb':
122 comment = 'built for %s' % platform.platform(terse=1)
123 data['comment'] = comment
124
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000125 if self.sign:
126 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
127 open(filename+".asc").read())
128
Martin v. Löwis98858c92005-03-21 21:00:59 +0000129 # set up the authentication
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000130 user_pass = (self.username + ":" + self.password).encode('ascii')
131 # The exact encoding of the authentication string is debated.
132 # Anyway PyPI only accepts ascii for both username or password.
Georg Brandl706824f2009-06-04 09:42:55 +0000133 auth = "Basic " + base64.encodebytes(user_pass).strip().decode('ascii')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000134
135 # Build up the MIME payload for the POST data
136 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000137 sep_boundary = b'\n--' + boundary.encode('ascii')
138 end_boundary = sep_boundary + b'--'
139 body = io.BytesIO()
Martin v. Löwis98858c92005-03-21 21:00:59 +0000140 for key, value in data.items():
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000141 title = '\nContent-Disposition: form-data; name="%s"' % key
Martin v. Löwis98858c92005-03-21 21:00:59 +0000142 # handle multiple entries for the same name
Tarek Ziadé6f6f9462009-06-28 21:10:49 +0000143 if not isinstance(value, list):
Martin v. Löwis98858c92005-03-21 21:00:59 +0000144 value = [value]
145 for value in value:
Tarek Ziadé6f6f9462009-06-28 21:10:49 +0000146 if isinstance(value, tuple):
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000147 title += '; filename="%s"' % value[0]
Martin v. Löwis98858c92005-03-21 21:00:59 +0000148 value = value[1]
149 else:
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000150 value = str(value).encode('utf-8')
Martin v. Löwis98858c92005-03-21 21:00:59 +0000151 body.write(sep_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000152 body.write(title.encode('utf-8'))
153 body.write(b"\n\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000154 body.write(value)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000155 if value and value[-1:] == b'\r':
156 body.write(b'\n') # write an extra newline (lurve Macs)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000157 body.write(end_boundary)
Amaury Forgeot d'Arc836b6702008-11-20 23:53:46 +0000158 body.write(b"\n")
Martin v. Löwis98858c92005-03-21 21:00:59 +0000159 body = body.getvalue()
160
161 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
162
163 # build the Request
Tarek Ziadé25bd2062009-06-28 21:26:27 +0000164 headers = {'Content-type':
165 'multipart/form-data; boundary=%s' % boundary,
166 'Content-length': str(len(body)),
167 'Authorization': auth}
Martin v. Löwis98858c92005-03-21 21:00:59 +0000168
Tarek Ziadé25bd2062009-06-28 21:26:27 +0000169 request = Request(self.repository, data=body,
170 headers=headers)
171 # send the data
Martin v. Löwis98858c92005-03-21 21:00:59 +0000172 try:
Tarek Ziadé25bd2062009-06-28 21:26:27 +0000173 result = urlopen(request)
174 status = result.getcode()
175 reason = result.msg
Guido van Rossumb940e112007-01-10 16:19:56 +0000176 except socket.error as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000177 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000178 return
Tarek Ziadé25bd2062009-06-28 21:26:27 +0000179 except HTTPError as e:
180 status = e.code
181 reason = e.msg
Martin v. Löwis98858c92005-03-21 21:00:59 +0000182
Tarek Ziadé25bd2062009-06-28 21:26:27 +0000183 if status == 200:
184 self.announce('Server response (%s): %s' % (status, reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000185 log.INFO)
186 else:
Tarek Ziadé25bd2062009-06-28 21:26:27 +0000187 self.announce('Upload failed (%s): %s' % (status, reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000188 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000189 if self.show_response:
Tarek Ziadé25bd2062009-06-28 21:26:27 +0000190 self.announce('-'*75, result.read(), '-'*75)