blob: 382aca8fc12e610ab5c58dad76c8e19ee96aaadb [file] [log] [blame]
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +00001"""distutils.pypirc
2
3Provides the PyPIRCCommand class, the base class for the command classes
4that uses .pypirc in the distutils.command package.
5"""
6import os
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +00007from configparser import ConfigParser
8
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +00009from distutils.cmd import Command
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +000010
11DEFAULT_PYPIRC = """\
Benjamin Peterson92035012008-12-27 16:00:54 +000012[distutils]
13index-servers =
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +000014 pypi
15
16[pypi]
17username:%s
18password:%s
19"""
20
21class PyPIRCCommand(Command):
22 """Base command that knows how to handle the .pypirc file
23 """
Benjamin Peterson45c97042013-03-18 15:20:56 -070024 DEFAULT_REPOSITORY = 'https://pypi.python.org/pypi'
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +000025 DEFAULT_REALM = 'pypi'
26 repository = None
27 realm = None
28
29 user_options = [
30 ('repository=', 'r',
31 "url of repository [default: %s]" % \
32 DEFAULT_REPOSITORY),
33 ('show-response', None,
34 'display full response text from server')]
35
36 boolean_options = ['show-response']
37
38 def _get_rc_file(self):
39 """Returns rc file path."""
40 return os.path.join(os.path.expanduser('~'), '.pypirc')
41
42 def _store_pypirc(self, username, password):
43 """Creates a default .pypirc file."""
44 rc = self._get_rc_file()
Éric Araujod61926e2012-12-08 14:51:47 -050045 with os.fdopen(os.open(rc, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +000046 f.write(DEFAULT_PYPIRC % (username, password))
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +000047
48 def _read_pypirc(self):
49 """Reads the .pypirc file."""
50 rc = self._get_rc_file()
51 if os.path.exists(rc):
Georg Brandl1b466f22008-05-26 17:01:57 +000052 self.announce('Using PyPI login from %s' % rc)
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +000053 repository = self.repository or self.DEFAULT_REPOSITORY
Tarek Ziadé36797272010-07-22 12:50:05 +000054 realm = self.realm or self.DEFAULT_REALM
55
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +000056 config = ConfigParser()
57 config.read(rc)
58 sections = config.sections()
59 if 'distutils' in sections:
60 # let's get the list of servers
61 index_servers = config.get('distutils', 'index-servers')
62 _servers = [server.strip() for server in
63 index_servers.split('\n')
64 if server.strip() != '']
65 if _servers == []:
66 # nothing set, let's try to get the default pypi
67 if 'pypi' in sections:
68 _servers = ['pypi']
69 else:
70 # the file is not properly defined, returning
71 # an empty dict
72 return {}
73 for server in _servers:
74 current = {'server': server}
75 current['username'] = config.get(server, 'username')
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +000076
77 # optional params
78 for key, default in (('repository',
79 self.DEFAULT_REPOSITORY),
Tarek Ziadé13f7c3b2009-01-09 00:15:45 +000080 ('realm', self.DEFAULT_REALM),
81 ('password', None)):
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +000082 if config.has_option(server, key):
83 current[key] = config.get(server, key)
84 else:
85 current[key] = default
Benjamin Peterson45c97042013-03-18 15:20:56 -070086
87 # work around people having "repository" for the "pypi"
88 # section of their config set to the HTTP (rather than
89 # HTTPS) URL
90 if (server == 'pypi' and
91 repository in (self.DEFAULT_REPOSITORY, 'pypi')):
92 current['repository'] = self.DEFAULT_REPOSITORY
93 return current
94
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +000095 if (current['server'] == repository or
96 current['repository'] == repository):
97 return current
98 elif 'server-login' in sections:
99 # old format
100 server = 'server-login'
101 if config.has_option(server, 'repository'):
102 repository = config.get(server, 'repository')
103 else:
104 repository = self.DEFAULT_REPOSITORY
105 return {'username': config.get(server, 'username'),
106 'password': config.get(server, 'password'),
107 'repository': repository,
108 'server': server,
109 'realm': self.DEFAULT_REALM}
110
111 return {}
112
Antoine Pitrou335a5122013-12-22 18:13:51 +0100113 def _read_pypi_response(self, response):
114 """Read and decode a PyPI HTTP response."""
Antoine Pitroue62a4042013-12-22 19:37:17 +0100115 import cgi
Antoine Pitrou335a5122013-12-22 18:13:51 +0100116 content_type = response.getheader('content-type', 'text/plain')
117 encoding = cgi.parse_header(content_type)[1].get('charset', 'ascii')
118 return response.read().decode(encoding)
119
Alexandre Vassalottifc02aef2008-05-15 02:14:05 +0000120 def initialize_options(self):
121 """Initialize options."""
122 self.repository = None
123 self.realm = None
124 self.show_response = 0
125
126 def finalize_options(self):
127 """Finalizes options."""
128 if self.repository is None:
129 self.repository = self.DEFAULT_REPOSITORY
130 if self.realm is None:
131 self.realm = self.DEFAULT_REALM