|
1 """distutils.command.register |
|
2 |
|
3 Implements the Distutils 'register' command (register with the repository). |
|
4 """ |
|
5 |
|
6 # created 2002/10/21, Richard Jones |
|
7 |
|
8 __revision__ = "$Id: register.py 63060 2008-05-11 14:00:00Z andrew.kuchling $" |
|
9 |
|
10 import os, string, urllib2, getpass, urlparse |
|
11 import StringIO |
|
12 |
|
13 from distutils.core import PyPIRCCommand |
|
14 from distutils.errors import * |
|
15 from distutils import log |
|
16 |
|
17 class register(PyPIRCCommand): |
|
18 |
|
19 description = ("register the distribution with the Python package index") |
|
20 user_options = PyPIRCCommand.user_options + [ |
|
21 ('list-classifiers', None, |
|
22 'list the valid Trove classifiers'), |
|
23 ] |
|
24 boolean_options = PyPIRCCommand.boolean_options + [ |
|
25 'verify', 'list-classifiers'] |
|
26 |
|
27 def initialize_options(self): |
|
28 PyPIRCCommand.initialize_options(self) |
|
29 self.list_classifiers = 0 |
|
30 |
|
31 def run(self): |
|
32 self.finalize_options() |
|
33 self._set_config() |
|
34 self.check_metadata() |
|
35 if self.dry_run: |
|
36 self.verify_metadata() |
|
37 elif self.list_classifiers: |
|
38 self.classifiers() |
|
39 else: |
|
40 self.send_metadata() |
|
41 |
|
42 def check_metadata(self): |
|
43 """Ensure that all required elements of meta-data (name, version, |
|
44 URL, (author and author_email) or (maintainer and |
|
45 maintainer_email)) are supplied by the Distribution object; warn if |
|
46 any are missing. |
|
47 """ |
|
48 metadata = self.distribution.metadata |
|
49 |
|
50 missing = [] |
|
51 for attr in ('name', 'version', 'url'): |
|
52 if not (hasattr(metadata, attr) and getattr(metadata, attr)): |
|
53 missing.append(attr) |
|
54 |
|
55 if missing: |
|
56 self.warn("missing required meta-data: " + |
|
57 string.join(missing, ", ")) |
|
58 |
|
59 if metadata.author: |
|
60 if not metadata.author_email: |
|
61 self.warn("missing meta-data: if 'author' supplied, " + |
|
62 "'author_email' must be supplied too") |
|
63 elif metadata.maintainer: |
|
64 if not metadata.maintainer_email: |
|
65 self.warn("missing meta-data: if 'maintainer' supplied, " + |
|
66 "'maintainer_email' must be supplied too") |
|
67 else: |
|
68 self.warn("missing meta-data: either (author and author_email) " + |
|
69 "or (maintainer and maintainer_email) " + |
|
70 "must be supplied") |
|
71 |
|
72 def _set_config(self): |
|
73 ''' Reads the configuration file and set attributes. |
|
74 ''' |
|
75 config = self._read_pypirc() |
|
76 if config != {}: |
|
77 self.username = config['username'] |
|
78 self.password = config['password'] |
|
79 self.repository = config['repository'] |
|
80 self.realm = config['realm'] |
|
81 self.has_config = True |
|
82 else: |
|
83 if self.repository not in ('pypi', self.DEFAULT_REPOSITORY): |
|
84 raise ValueError('%s not found in .pypirc' % self.repository) |
|
85 if self.repository == 'pypi': |
|
86 self.repository = self.DEFAULT_REPOSITORY |
|
87 self.has_config = False |
|
88 |
|
89 def classifiers(self): |
|
90 ''' Fetch the list of classifiers from the server. |
|
91 ''' |
|
92 response = urllib2.urlopen(self.repository+'?:action=list_classifiers') |
|
93 print response.read() |
|
94 |
|
95 def verify_metadata(self): |
|
96 ''' Send the metadata to the package index server to be checked. |
|
97 ''' |
|
98 # send the info to the server and report the result |
|
99 (code, result) = self.post_to_server(self.build_post_data('verify')) |
|
100 print 'Server response (%s): %s'%(code, result) |
|
101 |
|
102 |
|
103 def send_metadata(self): |
|
104 ''' Send the metadata to the package index server. |
|
105 |
|
106 Well, do the following: |
|
107 1. figure who the user is, and then |
|
108 2. send the data as a Basic auth'ed POST. |
|
109 |
|
110 First we try to read the username/password from $HOME/.pypirc, |
|
111 which is a ConfigParser-formatted file with a section |
|
112 [distutils] containing username and password entries (both |
|
113 in clear text). Eg: |
|
114 |
|
115 [distutils] |
|
116 index-servers = |
|
117 pypi |
|
118 |
|
119 [pypi] |
|
120 username: fred |
|
121 password: sekrit |
|
122 |
|
123 Otherwise, to figure who the user is, we offer the user three |
|
124 choices: |
|
125 |
|
126 1. use existing login, |
|
127 2. register as a new user, or |
|
128 3. set the password to a random string and email the user. |
|
129 |
|
130 ''' |
|
131 # see if we can short-cut and get the username/password from the |
|
132 # config |
|
133 if self.has_config: |
|
134 choice = '1' |
|
135 username = self.username |
|
136 password = self.password |
|
137 else: |
|
138 choice = 'x' |
|
139 username = password = '' |
|
140 |
|
141 # get the user's login info |
|
142 choices = '1 2 3 4'.split() |
|
143 while choice not in choices: |
|
144 print '''We need to know who you are, so please choose either: |
|
145 1. use your existing login, |
|
146 2. register as a new user, |
|
147 3. have the server generate a new password for you (and email it to you), or |
|
148 4. quit |
|
149 Your selection [default 1]: ''', |
|
150 choice = raw_input() |
|
151 if not choice: |
|
152 choice = '1' |
|
153 elif choice not in choices: |
|
154 print 'Please choose one of the four options!' |
|
155 |
|
156 if choice == '1': |
|
157 # get the username and password |
|
158 while not username: |
|
159 username = raw_input('Username: ') |
|
160 while not password: |
|
161 password = getpass.getpass('Password: ') |
|
162 |
|
163 # set up the authentication |
|
164 auth = urllib2.HTTPPasswordMgr() |
|
165 host = urlparse.urlparse(self.repository)[1] |
|
166 auth.add_password(self.realm, host, username, password) |
|
167 # send the info to the server and report the result |
|
168 code, result = self.post_to_server(self.build_post_data('submit'), |
|
169 auth) |
|
170 print 'Server response (%s): %s' % (code, result) |
|
171 |
|
172 # possibly save the login |
|
173 if not self.has_config and code == 200: |
|
174 print 'I can store your PyPI login so future submissions will be faster.' |
|
175 print '(the login will be stored in %s)' % self._get_rc_file() |
|
176 choice = 'X' |
|
177 while choice.lower() not in 'yn': |
|
178 choice = raw_input('Save your login (y/N)?') |
|
179 if not choice: |
|
180 choice = 'n' |
|
181 if choice.lower() == 'y': |
|
182 self._store_pypirc(username, password) |
|
183 |
|
184 elif choice == '2': |
|
185 data = {':action': 'user'} |
|
186 data['name'] = data['password'] = data['email'] = '' |
|
187 data['confirm'] = None |
|
188 while not data['name']: |
|
189 data['name'] = raw_input('Username: ') |
|
190 while data['password'] != data['confirm']: |
|
191 while not data['password']: |
|
192 data['password'] = getpass.getpass('Password: ') |
|
193 while not data['confirm']: |
|
194 data['confirm'] = getpass.getpass(' Confirm: ') |
|
195 if data['password'] != data['confirm']: |
|
196 data['password'] = '' |
|
197 data['confirm'] = None |
|
198 print "Password and confirm don't match!" |
|
199 while not data['email']: |
|
200 data['email'] = raw_input(' EMail: ') |
|
201 code, result = self.post_to_server(data) |
|
202 if code != 200: |
|
203 print 'Server response (%s): %s'%(code, result) |
|
204 else: |
|
205 print 'You will receive an email shortly.' |
|
206 print 'Follow the instructions in it to complete registration.' |
|
207 elif choice == '3': |
|
208 data = {':action': 'password_reset'} |
|
209 data['email'] = '' |
|
210 while not data['email']: |
|
211 data['email'] = raw_input('Your email address: ') |
|
212 code, result = self.post_to_server(data) |
|
213 print 'Server response (%s): %s'%(code, result) |
|
214 |
|
215 def build_post_data(self, action): |
|
216 # figure the data to send - the metadata plus some additional |
|
217 # information used by the package server |
|
218 meta = self.distribution.metadata |
|
219 data = { |
|
220 ':action': action, |
|
221 'metadata_version' : '1.0', |
|
222 'name': meta.get_name(), |
|
223 'version': meta.get_version(), |
|
224 'summary': meta.get_description(), |
|
225 'home_page': meta.get_url(), |
|
226 'author': meta.get_contact(), |
|
227 'author_email': meta.get_contact_email(), |
|
228 'license': meta.get_licence(), |
|
229 'description': meta.get_long_description(), |
|
230 'keywords': meta.get_keywords(), |
|
231 'platform': meta.get_platforms(), |
|
232 'classifiers': meta.get_classifiers(), |
|
233 'download_url': meta.get_download_url(), |
|
234 # PEP 314 |
|
235 'provides': meta.get_provides(), |
|
236 'requires': meta.get_requires(), |
|
237 'obsoletes': meta.get_obsoletes(), |
|
238 } |
|
239 if data['provides'] or data['requires'] or data['obsoletes']: |
|
240 data['metadata_version'] = '1.1' |
|
241 return data |
|
242 |
|
243 def post_to_server(self, data, auth=None): |
|
244 ''' Post a query to the server, and return a string response. |
|
245 ''' |
|
246 self.announce('Registering %s to %s' % (data['name'], |
|
247 self.repository), log.INFO) |
|
248 # Build up the MIME payload for the urllib2 POST data |
|
249 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' |
|
250 sep_boundary = '\n--' + boundary |
|
251 end_boundary = sep_boundary + '--' |
|
252 body = StringIO.StringIO() |
|
253 for key, value in data.items(): |
|
254 # handle multiple entries for the same name |
|
255 if type(value) not in (type([]), type( () )): |
|
256 value = [value] |
|
257 for value in value: |
|
258 value = unicode(value).encode("utf-8") |
|
259 body.write(sep_boundary) |
|
260 body.write('\nContent-Disposition: form-data; name="%s"'%key) |
|
261 body.write("\n\n") |
|
262 body.write(value) |
|
263 if value and value[-1] == '\r': |
|
264 body.write('\n') # write an extra newline (lurve Macs) |
|
265 body.write(end_boundary) |
|
266 body.write("\n") |
|
267 body = body.getvalue() |
|
268 |
|
269 # build the Request |
|
270 headers = { |
|
271 'Content-type': 'multipart/form-data; boundary=%s; charset=utf-8'%boundary, |
|
272 'Content-length': str(len(body)) |
|
273 } |
|
274 req = urllib2.Request(self.repository, body, headers) |
|
275 |
|
276 # handle HTTP and include the Basic Auth handler |
|
277 opener = urllib2.build_opener( |
|
278 urllib2.HTTPBasicAuthHandler(password_mgr=auth) |
|
279 ) |
|
280 data = '' |
|
281 try: |
|
282 result = opener.open(req) |
|
283 except urllib2.HTTPError, e: |
|
284 if self.show_response: |
|
285 data = e.fp.read() |
|
286 result = e.code, e.msg |
|
287 except urllib2.URLError, e: |
|
288 result = 500, str(e) |
|
289 else: |
|
290 if self.show_response: |
|
291 data = result.read() |
|
292 result = 200, 'OK' |
|
293 if self.show_response: |
|
294 print '-'*75, data, '-'*75 |
|
295 return result |