blob: 62767a348eafed48e973bc75a78025d38207d6e7 [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
Martin v. Löwisf74b9232005-03-22 15:51:14 +00009from md5 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'),
Martin v. Löwis98858c92005-03-21 21:00:59 +000032 ]
Martin v. Löwisf74b9232005-03-22 15:51:14 +000033 boolean_options = ['show-response', 'sign']
Martin v. Löwis98858c92005-03-21 21:00:59 +000034
35 def initialize_options(self):
36 self.username = ''
37 self.password = ''
38 self.repository = ''
39 self.show_response = 0
Martin v. Löwisf74b9232005-03-22 15:51:14 +000040 self.sign = False
Martin v. Löwis98858c92005-03-21 21:00:59 +000041
42 def finalize_options(self):
43 if os.environ.has_key('HOME'):
44 rc = os.path.join(os.environ['HOME'], '.pypirc')
45 if os.path.exists(rc):
46 self.announce('Using PyPI login from %s' % rc)
47 config = ConfigParser.ConfigParser({
48 'username':'',
49 'password':'',
50 'repository':''})
51 config.read(rc)
52 if not self.repository:
53 self.repository = config.get('server-login', 'repository')
54 if not self.username:
55 self.username = config.get('server-login', 'username')
56 if not self.password:
57 self.password = config.get('server-login', 'password')
58 if not self.repository:
59 self.repository = self.DEFAULT_REPOSITORY
60
61 def run(self):
62 if not self.distribution.dist_files:
63 raise DistutilsOptionError("No dist file created in earlier command")
Martin v. Löwis98da5622005-03-23 18:54:36 +000064 for command, pyversion, filename in self.distribution.dist_files:
65 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000066
Martin v. Löwis98da5622005-03-23 18:54:36 +000067 def upload_file(self, command, pyversion, filename):
Martin v. Löwisf74b9232005-03-22 15:51:14 +000068 # Sign if requested
69 if self.sign:
Martin v. Löwis8d121582005-03-22 23:02:54 +000070 spawn(("gpg", "--detach-sign", "-a", filename),
Martin v. Löwisf74b9232005-03-22 15:51:14 +000071 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000072
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000073 # Fill in the data - send all the meta-data in case we need to
74 # register a new release
Phillip J. Eby5cb78462005-07-07 15:36:20 +000075 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000076 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000077 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000078 # action
79 ':action': 'file_upload',
80 'protcol_version': '1',
81
82 # identify release
83 'name': meta.get_name(),
84 'version': meta.get_version(),
85
86 # file content
87 'content': (os.path.basename(filename),content),
88 'filetype': command,
89 'pyversion': pyversion,
90 'md5_digest': md5(content).hexdigest(),
91
92 # additional meta-data
93 'metadata_version' : '1.0',
94 'summary': meta.get_description(),
95 'home_page': meta.get_url(),
96 'author': meta.get_contact(),
97 'author_email': meta.get_contact_email(),
98 'license': meta.get_licence(),
99 'description': meta.get_long_description(),
100 'keywords': meta.get_keywords(),
101 'platform': meta.get_platforms(),
102 'classifiers': meta.get_classifiers(),
103 'download_url': meta.get_download_url(),
104 # PEP 314
105 'provides': meta.get_provides(),
106 'requires': meta.get_requires(),
107 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000108 }
109 comment = ''
110 if command == 'bdist_rpm':
111 dist, version, id = platform.dist()
112 if dist:
113 comment = 'built for %s %s' % (dist, version)
114 elif command == 'bdist_dumb':
115 comment = 'built for %s' % platform.platform(terse=1)
116 data['comment'] = comment
117
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000118 if self.sign:
119 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
120 open(filename+".asc").read())
121
Martin v. Löwis98858c92005-03-21 21:00:59 +0000122 # set up the authentication
123 auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip()
124
125 # Build up the MIME payload for the POST data
126 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
127 sep_boundary = '\n--' + boundary
128 end_boundary = sep_boundary + '--'
129 body = StringIO.StringIO()
130 for key, value in data.items():
131 # handle multiple entries for the same name
132 if type(value) != type([]):
133 value = [value]
134 for value in value:
135 if type(value) is tuple:
136 fn = ';filename="%s"' % value[0]
137 value = value[1]
138 else:
139 fn = ""
140 value = str(value)
141 body.write(sep_boundary)
142 body.write('\nContent-Disposition: form-data; name="%s"'%key)
143 body.write(fn)
144 body.write("\n\n")
145 body.write(value)
146 if value and value[-1] == '\r':
147 body.write('\n') # write an extra newline (lurve Macs)
148 body.write(end_boundary)
149 body.write("\n")
150 body = body.getvalue()
151
152 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
153
154 # build the Request
155 # We can't use urllib2 since we need to send the Basic
156 # auth right with the first request
157 schema, netloc, url, params, query, fragments = \
158 urlparse.urlparse(self.repository)
159 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000160 if schema == 'http':
Martin v. Löwis98858c92005-03-21 21:00:59 +0000161 http = httplib.HTTPConnection(netloc)
162 elif schema == 'https':
163 http = httplib.HTTPSConnection(netloc)
164 else:
165 raise AssertionError, "unsupported schema "+schema
166
167 data = ''
168 loglevel = log.INFO
169 try:
170 http.connect()
171 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000172 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000173 'multipart/form-data; boundary=%s'%boundary)
174 http.putheader('Content-length', str(len(body)))
175 http.putheader('Authorization', auth)
176 http.endheaders()
177 http.send(body)
178 except socket.error, e:
179 self.announce(e.msg, log.ERROR)
180 return
181
182 r = http.getresponse()
183 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000184 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000185 log.INFO)
186 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000187 self.announce('Upload failed (%s): %s' % (r.status, r.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:
190 print '-'*75, r.read(), '-'*75