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