blob: 1fd53346e96d542c9845b9ad19165e394016d6b2 [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 """
24 DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi'
25 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
86 if (current['server'] == repository or
87 current['repository'] == repository):
88 return current
89 elif 'server-login' in sections:
90 # old format
91 server = 'server-login'
92 if config.has_option(server, 'repository'):
93 repository = config.get(server, 'repository')
94 else:
95 repository = self.DEFAULT_REPOSITORY
96 return {'username': config.get(server, 'username'),
97 'password': config.get(server, 'password'),
98 'repository': repository,
99 'server': server,
100 'realm': self.DEFAULT_REALM}
101
102 return {}
103
104 def initialize_options(self):
105 """Initialize options."""
106 self.repository = None
107 self.realm = None
108 self.show_response = 0
109
110 def finalize_options(self):
111 """Finalizes options."""
112 if self.repository is None:
113 self.repository = self.DEFAULT_REPOSITORY
114 if self.realm is None:
115 self.realm = self.DEFAULT_REALM