| 1 | n/a | """Register a release with a project index.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | # Contributed by Richard Jones |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | import getpass |
|---|
| 6 | n/a | import urllib.error |
|---|
| 7 | n/a | import urllib.parse |
|---|
| 8 | n/a | import urllib.request |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | from packaging import logger |
|---|
| 11 | n/a | from packaging.util import (read_pypirc, generate_pypirc, DEFAULT_REPOSITORY, |
|---|
| 12 | n/a | DEFAULT_REALM, get_pypirc_path, encode_multipart) |
|---|
| 13 | n/a | from packaging.command.cmd import Command |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | class register(Command): |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | description = "register a release with PyPI" |
|---|
| 18 | n/a | user_options = [ |
|---|
| 19 | n/a | ('repository=', 'r', |
|---|
| 20 | n/a | "repository URL [default: %s]" % DEFAULT_REPOSITORY), |
|---|
| 21 | n/a | ('show-response', None, |
|---|
| 22 | n/a | "display full response text from server"), |
|---|
| 23 | n/a | ('list-classifiers', None, |
|---|
| 24 | n/a | "list valid Trove classifiers"), |
|---|
| 25 | n/a | ('strict', None , |
|---|
| 26 | n/a | "stop the registration if the metadata is not fully compliant") |
|---|
| 27 | n/a | ] |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | boolean_options = ['show-response', 'list-classifiers', 'strict'] |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | def initialize_options(self): |
|---|
| 32 | n/a | self.repository = None |
|---|
| 33 | n/a | self.realm = None |
|---|
| 34 | n/a | self.show_response = False |
|---|
| 35 | n/a | self.list_classifiers = False |
|---|
| 36 | n/a | self.strict = False |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | def finalize_options(self): |
|---|
| 39 | n/a | if self.repository is None: |
|---|
| 40 | n/a | self.repository = DEFAULT_REPOSITORY |
|---|
| 41 | n/a | if self.realm is None: |
|---|
| 42 | n/a | self.realm = DEFAULT_REALM |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | def run(self): |
|---|
| 45 | n/a | self._set_config() |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | # Check the package metadata |
|---|
| 48 | n/a | check = self.distribution.get_command_obj('check') |
|---|
| 49 | n/a | if check.strict != self.strict and not check.all: |
|---|
| 50 | n/a | # If check was already run but with different options, |
|---|
| 51 | n/a | # re-run it |
|---|
| 52 | n/a | check.strict = self.strict |
|---|
| 53 | n/a | check.all = True |
|---|
| 54 | n/a | self.distribution.have_run.pop('check', None) |
|---|
| 55 | n/a | self.run_command('check') |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | if self.dry_run: |
|---|
| 58 | n/a | self.verify_metadata() |
|---|
| 59 | n/a | elif self.list_classifiers: |
|---|
| 60 | n/a | self.classifiers() |
|---|
| 61 | n/a | else: |
|---|
| 62 | n/a | self.send_metadata() |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | def _set_config(self): |
|---|
| 65 | n/a | ''' Reads the configuration file and set attributes. |
|---|
| 66 | n/a | ''' |
|---|
| 67 | n/a | config = read_pypirc(self.repository, self.realm) |
|---|
| 68 | n/a | if config != {}: |
|---|
| 69 | n/a | self.username = config['username'] |
|---|
| 70 | n/a | self.password = config['password'] |
|---|
| 71 | n/a | self.repository = config['repository'] |
|---|
| 72 | n/a | self.realm = config['realm'] |
|---|
| 73 | n/a | self.has_config = True |
|---|
| 74 | n/a | else: |
|---|
| 75 | n/a | if self.repository not in ('pypi', DEFAULT_REPOSITORY): |
|---|
| 76 | n/a | raise ValueError('%s not found in .pypirc' % self.repository) |
|---|
| 77 | n/a | if self.repository == 'pypi': |
|---|
| 78 | n/a | self.repository = DEFAULT_REPOSITORY |
|---|
| 79 | n/a | self.has_config = False |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | def classifiers(self): |
|---|
| 82 | n/a | ''' Fetch the list of classifiers from the server. |
|---|
| 83 | n/a | ''' |
|---|
| 84 | n/a | response = urllib.request.urlopen(self.repository+'?:action=list_classifiers') |
|---|
| 85 | n/a | logger.info(response.read()) |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | def verify_metadata(self): |
|---|
| 88 | n/a | ''' Send the metadata to the package index server to be checked. |
|---|
| 89 | n/a | ''' |
|---|
| 90 | n/a | # send the info to the server and report the result |
|---|
| 91 | n/a | code, result = self.post_to_server(self.build_post_data('verify')) |
|---|
| 92 | n/a | logger.info('server response (%s): %s', code, result) |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def send_metadata(self): |
|---|
| 96 | n/a | ''' Send the metadata to the package index server. |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | Well, do the following: |
|---|
| 99 | n/a | 1. figure who the user is, and then |
|---|
| 100 | n/a | 2. send the data as a Basic auth'ed POST. |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | First we try to read the username/password from $HOME/.pypirc, |
|---|
| 103 | n/a | which is a ConfigParser-formatted file with a section |
|---|
| 104 | n/a | [distutils] containing username and password entries (both |
|---|
| 105 | n/a | in clear text). Eg: |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | [distutils] |
|---|
| 108 | n/a | index-servers = |
|---|
| 109 | n/a | pypi |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | [pypi] |
|---|
| 112 | n/a | username: fred |
|---|
| 113 | n/a | password: sekrit |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | Otherwise, to figure who the user is, we offer the user three |
|---|
| 116 | n/a | choices: |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | 1. use existing login, |
|---|
| 119 | n/a | 2. register as a new user, or |
|---|
| 120 | n/a | 3. set the password to a random string and email the user. |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | ''' |
|---|
| 123 | n/a | # TODO factor registration out into another method |
|---|
| 124 | n/a | # TODO use print to print, not logging |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | # see if we can short-cut and get the username/password from the |
|---|
| 127 | n/a | # config |
|---|
| 128 | n/a | if self.has_config: |
|---|
| 129 | n/a | choice = '1' |
|---|
| 130 | n/a | username = self.username |
|---|
| 131 | n/a | password = self.password |
|---|
| 132 | n/a | else: |
|---|
| 133 | n/a | choice = 'x' |
|---|
| 134 | n/a | username = password = '' |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | # get the user's login info |
|---|
| 137 | n/a | choices = '1 2 3 4'.split() |
|---|
| 138 | n/a | while choice not in choices: |
|---|
| 139 | n/a | logger.info('''\ |
|---|
| 140 | n/a | We need to know who you are, so please choose either: |
|---|
| 141 | n/a | 1. use your existing login, |
|---|
| 142 | n/a | 2. register as a new user, |
|---|
| 143 | n/a | 3. have the server generate a new password for you (and email it to you), or |
|---|
| 144 | n/a | 4. quit |
|---|
| 145 | n/a | Your selection [default 1]: ''') |
|---|
| 146 | n/a | |
|---|
| 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 | logger.info('Server response (%s): %s', code, result) |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | # possibly save the login |
|---|
| 170 | n/a | if code == 200: |
|---|
| 171 | n/a | if self.has_config: |
|---|
| 172 | n/a | # sharing the password in the distribution instance |
|---|
| 173 | n/a | # so the upload command can reuse it |
|---|
| 174 | n/a | self.distribution.password = password |
|---|
| 175 | n/a | else: |
|---|
| 176 | n/a | logger.info( |
|---|
| 177 | n/a | 'I can store your PyPI login so future submissions ' |
|---|
| 178 | n/a | 'will be faster.\n(the login will be stored in %s)', |
|---|
| 179 | n/a | get_pypirc_path()) |
|---|
| 180 | n/a | choice = 'X' |
|---|
| 181 | n/a | while choice.lower() not in ('y', 'n'): |
|---|
| 182 | n/a | choice = input('Save your login (y/N)?') |
|---|
| 183 | n/a | if not choice: |
|---|
| 184 | n/a | choice = 'n' |
|---|
| 185 | n/a | if choice.lower() == 'y': |
|---|
| 186 | n/a | generate_pypirc(username, password) |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | elif choice == '2': |
|---|
| 189 | n/a | data = {':action': 'user'} |
|---|
| 190 | n/a | data['name'] = data['password'] = data['email'] = '' |
|---|
| 191 | n/a | data['confirm'] = None |
|---|
| 192 | n/a | while not data['name']: |
|---|
| 193 | n/a | data['name'] = input('Username: ') |
|---|
| 194 | n/a | while data['password'] != data['confirm']: |
|---|
| 195 | n/a | while not data['password']: |
|---|
| 196 | n/a | data['password'] = getpass.getpass('Password: ') |
|---|
| 197 | n/a | while not data['confirm']: |
|---|
| 198 | n/a | data['confirm'] = getpass.getpass(' Confirm: ') |
|---|
| 199 | n/a | if data['password'] != data['confirm']: |
|---|
| 200 | n/a | data['password'] = '' |
|---|
| 201 | n/a | data['confirm'] = None |
|---|
| 202 | n/a | print("Password and confirm don't match!") |
|---|
| 203 | n/a | while not data['email']: |
|---|
| 204 | n/a | data['email'] = input(' EMail: ') |
|---|
| 205 | n/a | code, result = self.post_to_server(data) |
|---|
| 206 | n/a | if code != 200: |
|---|
| 207 | n/a | logger.info('server response (%s): %s', code, result) |
|---|
| 208 | n/a | else: |
|---|
| 209 | n/a | logger.info('you will receive an email shortly; follow the ' |
|---|
| 210 | n/a | 'instructions in it to complete registration.') |
|---|
| 211 | n/a | elif choice == '3': |
|---|
| 212 | n/a | data = {':action': 'password_reset'} |
|---|
| 213 | n/a | data['email'] = '' |
|---|
| 214 | n/a | while not data['email']: |
|---|
| 215 | n/a | data['email'] = input('Your email address: ') |
|---|
| 216 | n/a | code, result = self.post_to_server(data) |
|---|
| 217 | n/a | logger.info('server response (%s): %s', code, result) |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | def build_post_data(self, action): |
|---|
| 220 | n/a | # figure the data to send - the metadata plus some additional |
|---|
| 221 | n/a | # information used by the package server |
|---|
| 222 | n/a | data = self.distribution.metadata.todict() |
|---|
| 223 | n/a | data[':action'] = action |
|---|
| 224 | n/a | return data |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | # XXX to be refactored with upload.upload_file |
|---|
| 227 | n/a | def post_to_server(self, data, auth=None): |
|---|
| 228 | n/a | ''' Post a query to the server, and return a string response. |
|---|
| 229 | n/a | ''' |
|---|
| 230 | n/a | if 'name' in data: |
|---|
| 231 | n/a | logger.info('Registering %s to %s', data['name'], self.repository) |
|---|
| 232 | n/a | # Build up the MIME payload for the urllib2 POST data |
|---|
| 233 | n/a | content_type, body = encode_multipart(data.items(), []) |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | # build the Request |
|---|
| 236 | n/a | headers = { |
|---|
| 237 | n/a | 'Content-type': content_type, |
|---|
| 238 | n/a | 'Content-length': str(len(body)) |
|---|
| 239 | n/a | } |
|---|
| 240 | n/a | req = urllib.request.Request(self.repository, body, headers) |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | # handle HTTP and include the Basic Auth handler |
|---|
| 243 | n/a | opener = urllib.request.build_opener( |
|---|
| 244 | n/a | urllib.request.HTTPBasicAuthHandler(password_mgr=auth) |
|---|
| 245 | n/a | ) |
|---|
| 246 | n/a | data = '' |
|---|
| 247 | n/a | try: |
|---|
| 248 | n/a | result = opener.open(req) |
|---|
| 249 | n/a | except urllib.error.HTTPError as e: |
|---|
| 250 | n/a | if self.show_response: |
|---|
| 251 | n/a | data = e.fp.read() |
|---|
| 252 | n/a | result = e.code, e.msg |
|---|
| 253 | n/a | except urllib.error.URLError as e: |
|---|
| 254 | n/a | result = 500, str(e) |
|---|
| 255 | n/a | else: |
|---|
| 256 | n/a | if self.show_response: |
|---|
| 257 | n/a | data = result.read() |
|---|
| 258 | n/a | result = 200, 'OK' |
|---|
| 259 | n/a | if self.show_response: |
|---|
| 260 | n/a | dashes = '-' * 75 |
|---|
| 261 | n/a | logger.info('%s%s%s', dashes, data, dashes) |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | return result |
|---|