blob: 603336c954417e1fc767a858e892988155f4f6a7 [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
Martin v. Löwis98858c92005-03-21 21:00:59 +000014import httplib
15import base64
16import urlparse
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
148 # We can't use urllib2 since we need to send the Basic
149 # auth right with the first request
150 schema, netloc, url, params, query, fragments = \
151 urlparse.urlparse(self.repository)
152 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000153 if schema == 'http':
Martin v. Löwis98858c92005-03-21 21:00:59 +0000154 http = httplib.HTTPConnection(netloc)
155 elif schema == 'https':
156 http = httplib.HTTPSConnection(netloc)
157 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000158 raise AssertionError("unsupported schema "+schema)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000159
160 data = ''
161 loglevel = log.INFO
162 try:
163 http.connect()
164 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000165 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000166 'multipart/form-data; boundary=%s'%boundary)
167 http.putheader('Content-length', str(len(body)))
168 http.putheader('Authorization', auth)
169 http.endheaders()
170 http.send(body)
Guido van Rossumb940e112007-01-10 16:19:56 +0000171 except socket.error as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000172 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000173 return
174
175 r = http.getresponse()
176 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000177 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000178 log.INFO)
179 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000180 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000181 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000182 if self.show_response:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000183 print('-'*75, r.read(), '-'*75)