blob: 5049f03e93a2955192e9293985544468cdecb234 [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
Martin v. Löwis98858c92005-03-21 21:00:59 +000010import os
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
Georg Brandl24420152008-05-26 16:32:26 +000014import http.client
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
116 auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip()
117
118 # Build up the MIME payload for the POST data
119 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
120 sep_boundary = '\n--' + boundary
121 end_boundary = sep_boundary + '--'
Guido van Rossum34d19282007-08-09 01:03:29 +0000122 body = io.StringIO()
Martin v. Löwis98858c92005-03-21 21:00:59 +0000123 for key, value in data.items():
124 # handle multiple entries for the same name
125 if type(value) != type([]):
126 value = [value]
127 for value in value:
128 if type(value) is tuple:
129 fn = ';filename="%s"' % value[0]
130 value = value[1]
131 else:
132 fn = ""
133 value = str(value)
134 body.write(sep_boundary)
135 body.write('\nContent-Disposition: form-data; name="%s"'%key)
136 body.write(fn)
137 body.write("\n\n")
138 body.write(value)
139 if value and value[-1] == '\r':
140 body.write('\n') # write an extra newline (lurve Macs)
141 body.write(end_boundary)
142 body.write("\n")
143 body = body.getvalue()
144
145 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
146
147 # build the Request
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000148 # We can't use urllib since we need to send the Basic
Martin v. Löwis98858c92005-03-21 21:00:59 +0000149 # auth right with the first request
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000150 # TODO(jhylton): Can we fix urllib?
Martin v. Löwis98858c92005-03-21 21:00:59 +0000151 schema, netloc, url, params, query, fragments = \
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000152 urllib.parse.urlparse(self.repository)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000153 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000154 if schema == 'http':
Georg Brandl24420152008-05-26 16:32:26 +0000155 http = http.client.HTTPConnection(netloc)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000156 elif schema == 'https':
Georg Brandl24420152008-05-26 16:32:26 +0000157 http = http.client.HTTPSConnection(netloc)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000158 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000159 raise AssertionError("unsupported schema "+schema)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000160
161 data = ''
162 loglevel = log.INFO
163 try:
164 http.connect()
165 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000166 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000167 'multipart/form-data; boundary=%s'%boundary)
168 http.putheader('Content-length', str(len(body)))
169 http.putheader('Authorization', auth)
170 http.endheaders()
171 http.send(body)
Guido van Rossumb940e112007-01-10 16:19:56 +0000172 except socket.error as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000173 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000174 return
175
176 r = http.getresponse()
177 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000178 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000179 log.INFO)
180 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000181 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000182 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000183 if self.show_response:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000184 print('-'*75, r.read(), '-'*75)