| 1 | n/a | """distutils.pypirc |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Provides the PyPIRCCommand class, the base class for the command classes |
|---|
| 4 | n/a | that uses .pypirc in the distutils.command package. |
|---|
| 5 | n/a | """ |
|---|
| 6 | n/a | import os |
|---|
| 7 | n/a | from configparser import RawConfigParser |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | from distutils.cmd import Command |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | DEFAULT_PYPIRC = """\ |
|---|
| 12 | n/a | [distutils] |
|---|
| 13 | n/a | index-servers = |
|---|
| 14 | n/a | pypi |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | [pypi] |
|---|
| 17 | n/a | username:%s |
|---|
| 18 | n/a | password:%s |
|---|
| 19 | n/a | """ |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | class PyPIRCCommand(Command): |
|---|
| 22 | n/a | """Base command that knows how to handle the .pypirc file |
|---|
| 23 | n/a | """ |
|---|
| 24 | n/a | DEFAULT_REPOSITORY = 'https://upload.pypi.org/legacy/' |
|---|
| 25 | n/a | DEFAULT_REALM = 'pypi' |
|---|
| 26 | n/a | repository = None |
|---|
| 27 | n/a | realm = None |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | user_options = [ |
|---|
| 30 | n/a | ('repository=', 'r', |
|---|
| 31 | n/a | "url of repository [default: %s]" % \ |
|---|
| 32 | n/a | DEFAULT_REPOSITORY), |
|---|
| 33 | n/a | ('show-response', None, |
|---|
| 34 | n/a | 'display full response text from server')] |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | boolean_options = ['show-response'] |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | def _get_rc_file(self): |
|---|
| 39 | n/a | """Returns rc file path.""" |
|---|
| 40 | n/a | return os.path.join(os.path.expanduser('~'), '.pypirc') |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | def _store_pypirc(self, username, password): |
|---|
| 43 | n/a | """Creates a default .pypirc file.""" |
|---|
| 44 | n/a | rc = self._get_rc_file() |
|---|
| 45 | n/a | with os.fdopen(os.open(rc, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f: |
|---|
| 46 | n/a | f.write(DEFAULT_PYPIRC % (username, password)) |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | def _read_pypirc(self): |
|---|
| 49 | n/a | """Reads the .pypirc file.""" |
|---|
| 50 | n/a | rc = self._get_rc_file() |
|---|
| 51 | n/a | if os.path.exists(rc): |
|---|
| 52 | n/a | self.announce('Using PyPI login from %s' % rc) |
|---|
| 53 | n/a | repository = self.repository or self.DEFAULT_REPOSITORY |
|---|
| 54 | n/a | realm = self.realm or self.DEFAULT_REALM |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | config = RawConfigParser() |
|---|
| 57 | n/a | config.read(rc) |
|---|
| 58 | n/a | sections = config.sections() |
|---|
| 59 | n/a | if 'distutils' in sections: |
|---|
| 60 | n/a | # let's get the list of servers |
|---|
| 61 | n/a | index_servers = config.get('distutils', 'index-servers') |
|---|
| 62 | n/a | _servers = [server.strip() for server in |
|---|
| 63 | n/a | index_servers.split('\n') |
|---|
| 64 | n/a | if server.strip() != ''] |
|---|
| 65 | n/a | if _servers == []: |
|---|
| 66 | n/a | # nothing set, let's try to get the default pypi |
|---|
| 67 | n/a | if 'pypi' in sections: |
|---|
| 68 | n/a | _servers = ['pypi'] |
|---|
| 69 | n/a | else: |
|---|
| 70 | n/a | # the file is not properly defined, returning |
|---|
| 71 | n/a | # an empty dict |
|---|
| 72 | n/a | return {} |
|---|
| 73 | n/a | for server in _servers: |
|---|
| 74 | n/a | current = {'server': server} |
|---|
| 75 | n/a | current['username'] = config.get(server, 'username') |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | # optional params |
|---|
| 78 | n/a | for key, default in (('repository', |
|---|
| 79 | n/a | self.DEFAULT_REPOSITORY), |
|---|
| 80 | n/a | ('realm', self.DEFAULT_REALM), |
|---|
| 81 | n/a | ('password', None)): |
|---|
| 82 | n/a | if config.has_option(server, key): |
|---|
| 83 | n/a | current[key] = config.get(server, key) |
|---|
| 84 | n/a | else: |
|---|
| 85 | n/a | current[key] = default |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | # work around people having "repository" for the "pypi" |
|---|
| 88 | n/a | # section of their config set to the HTTP (rather than |
|---|
| 89 | n/a | # HTTPS) URL |
|---|
| 90 | n/a | if (server == 'pypi' and |
|---|
| 91 | n/a | repository in (self.DEFAULT_REPOSITORY, 'pypi')): |
|---|
| 92 | n/a | current['repository'] = self.DEFAULT_REPOSITORY |
|---|
| 93 | n/a | return current |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | if (current['server'] == repository or |
|---|
| 96 | n/a | current['repository'] == repository): |
|---|
| 97 | n/a | return current |
|---|
| 98 | n/a | elif 'server-login' in sections: |
|---|
| 99 | n/a | # old format |
|---|
| 100 | n/a | server = 'server-login' |
|---|
| 101 | n/a | if config.has_option(server, 'repository'): |
|---|
| 102 | n/a | repository = config.get(server, 'repository') |
|---|
| 103 | n/a | else: |
|---|
| 104 | n/a | repository = self.DEFAULT_REPOSITORY |
|---|
| 105 | n/a | return {'username': config.get(server, 'username'), |
|---|
| 106 | n/a | 'password': config.get(server, 'password'), |
|---|
| 107 | n/a | 'repository': repository, |
|---|
| 108 | n/a | 'server': server, |
|---|
| 109 | n/a | 'realm': self.DEFAULT_REALM} |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | return {} |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | def _read_pypi_response(self, response): |
|---|
| 114 | n/a | """Read and decode a PyPI HTTP response.""" |
|---|
| 115 | n/a | import cgi |
|---|
| 116 | n/a | content_type = response.getheader('content-type', 'text/plain') |
|---|
| 117 | n/a | encoding = cgi.parse_header(content_type)[1].get('charset', 'ascii') |
|---|
| 118 | n/a | return response.read().decode(encoding) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | def initialize_options(self): |
|---|
| 121 | n/a | """Initialize options.""" |
|---|
| 122 | n/a | self.repository = None |
|---|
| 123 | n/a | self.realm = None |
|---|
| 124 | n/a | self.show_response = 0 |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | def finalize_options(self): |
|---|
| 127 | n/a | """Finalizes options.""" |
|---|
| 128 | n/a | if self.repository is None: |
|---|
| 129 | n/a | self.repository = self.DEFAULT_REPOSITORY |
|---|
| 130 | n/a | if self.realm is None: |
|---|
| 131 | n/a | self.realm = self.DEFAULT_REALM |
|---|