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