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