ยปCore Development>Code coverage>Lib/distutils/config.py

Python code coverage for Lib/distutils/config.py

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