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