blob: 67ba080427ca301a47e62a00c1c47700d52af7c3 [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 *
6from distutils.core import Command
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
13import ConfigParser
14import httplib
15import base64
16import urlparse
17import cStringIO as StringIO
18
19class upload(Command):
20
21 description = "upload binary package to PyPI"
22
23 DEFAULT_REPOSITORY = 'http://www.python.org/pypi'
24
25 user_options = [
26 ('repository=', 'r',
27 "url of repository [default: %s]" % DEFAULT_REPOSITORY),
28 ('show-response', None,
29 'display full response text from server'),
Martin v. Löwisf74b9232005-03-22 15:51:14 +000030 ('sign', 's',
31 'sign files to upload using gpg'),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000032 ('identity=', 'i', 'GPG identity used to sign files'),
Martin v. Löwis98858c92005-03-21 21:00:59 +000033 ]
Martin v. Löwisf74b9232005-03-22 15:51:14 +000034 boolean_options = ['show-response', 'sign']
Martin v. Löwis98858c92005-03-21 21:00:59 +000035
36 def initialize_options(self):
37 self.username = ''
38 self.password = ''
39 self.repository = ''
40 self.show_response = 0
Martin v. Löwisf74b9232005-03-22 15:51:14 +000041 self.sign = False
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042 self.identity = None
Martin v. Löwis98858c92005-03-21 21:00:59 +000043
44 def finalize_options(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000045 if self.identity and not self.sign:
46 raise DistutilsOptionError(
47 "Must use --sign for --identity to have meaning"
48 )
Martin v. Löwis98858c92005-03-21 21:00:59 +000049 if os.environ.has_key('HOME'):
50 rc = os.path.join(os.environ['HOME'], '.pypirc')
51 if os.path.exists(rc):
52 self.announce('Using PyPI login from %s' % rc)
53 config = ConfigParser.ConfigParser({
54 'username':'',
55 'password':'',
56 'repository':''})
57 config.read(rc)
58 if not self.repository:
59 self.repository = config.get('server-login', 'repository')
60 if not self.username:
61 self.username = config.get('server-login', 'username')
62 if not self.password:
63 self.password = config.get('server-login', 'password')
64 if not self.repository:
65 self.repository = self.DEFAULT_REPOSITORY
66
67 def run(self):
68 if not self.distribution.dist_files:
69 raise DistutilsOptionError("No dist file created in earlier command")
Martin v. Löwis98da5622005-03-23 18:54:36 +000070 for command, pyversion, filename in self.distribution.dist_files:
71 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000072
Martin v. Löwis98da5622005-03-23 18:54:36 +000073 def upload_file(self, command, pyversion, filename):
Martin v. Löwisf74b9232005-03-22 15:51:14 +000074 # Sign if requested
75 if self.sign:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000076 gpg_args = ["gpg", "--detach-sign", "-a", filename]
77 if self.identity:
78 gpg_args[2:2] = ["--local-user", self.identity]
79 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000080 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000081
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000082 # Fill in the data - send all the meta-data in case we need to
83 # register a new release
Phillip J. Eby5cb78462005-07-07 15:36:20 +000084 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000085 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000086 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000087 # action
88 ':action': 'file_upload',
89 'protcol_version': '1',
90
91 # identify release
92 'name': meta.get_name(),
93 'version': meta.get_version(),
94
95 # file content
96 'content': (os.path.basename(filename),content),
97 'filetype': command,
98 'pyversion': pyversion,
99 'md5_digest': md5(content).hexdigest(),
100
101 # additional meta-data
102 'metadata_version' : '1.0',
103 'summary': meta.get_description(),
104 'home_page': meta.get_url(),
105 'author': meta.get_contact(),
106 'author_email': meta.get_contact_email(),
107 'license': meta.get_licence(),
108 'description': meta.get_long_description(),
109 'keywords': meta.get_keywords(),
110 'platform': meta.get_platforms(),
111 'classifiers': meta.get_classifiers(),
112 'download_url': meta.get_download_url(),
113 # PEP 314
114 'provides': meta.get_provides(),
115 'requires': meta.get_requires(),
116 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000117 }
118 comment = ''
119 if command == 'bdist_rpm':
120 dist, version, id = platform.dist()
121 if dist:
122 comment = 'built for %s %s' % (dist, version)
123 elif command == 'bdist_dumb':
124 comment = 'built for %s' % platform.platform(terse=1)
125 data['comment'] = comment
126
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000127 if self.sign:
128 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
129 open(filename+".asc").read())
130
Martin v. Löwis98858c92005-03-21 21:00:59 +0000131 # set up the authentication
132 auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip()
133
134 # Build up the MIME payload for the POST data
135 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
136 sep_boundary = '\n--' + boundary
137 end_boundary = sep_boundary + '--'
138 body = StringIO.StringIO()
139 for key, value in data.items():
140 # handle multiple entries for the same name
141 if type(value) != type([]):
142 value = [value]
143 for value in value:
144 if type(value) is tuple:
145 fn = ';filename="%s"' % value[0]
146 value = value[1]
147 else:
148 fn = ""
149 value = str(value)
150 body.write(sep_boundary)
151 body.write('\nContent-Disposition: form-data; name="%s"'%key)
152 body.write(fn)
153 body.write("\n\n")
154 body.write(value)
155 if value and value[-1] == '\r':
156 body.write('\n') # write an extra newline (lurve Macs)
157 body.write(end_boundary)
158 body.write("\n")
159 body = body.getvalue()
160
161 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
162
163 # build the Request
164 # We can't use urllib2 since we need to send the Basic
165 # auth right with the first request
166 schema, netloc, url, params, query, fragments = \
167 urlparse.urlparse(self.repository)
168 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000169 if schema == 'http':
Martin v. Löwis98858c92005-03-21 21:00:59 +0000170 http = httplib.HTTPConnection(netloc)
171 elif schema == 'https':
172 http = httplib.HTTPSConnection(netloc)
173 else:
174 raise AssertionError, "unsupported schema "+schema
175
176 data = ''
177 loglevel = log.INFO
178 try:
179 http.connect()
180 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000181 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000182 'multipart/form-data; boundary=%s'%boundary)
183 http.putheader('Content-length', str(len(body)))
184 http.putheader('Authorization', auth)
185 http.endheaders()
186 http.send(body)
187 except socket.error, e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000188 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000189 return
190
191 r = http.getresponse()
192 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000193 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000194 log.INFO)
195 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000196 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000197 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000198 if self.show_response:
199 print '-'*75, r.read(), '-'*75