Mention Giampolo R's new FTP TLS support in the what's new file
[python.git] / Lib / distutils / config.py
blob9166199ea9f9df8be24ff31f7ad0231dbbbe4493
1 """distutils.pypirc
3 Provides the PyPIRCCommand class, the base class for the command classes
4 that uses .pypirc in the distutils.command package.
5 """
6 import os
7 import sys
8 from ConfigParser import ConfigParser
10 from distutils.cmd import Command
12 DEFAULT_PYPIRC = """\
13 [distutils]
14 index-servers =
15 pypi
17 [pypi]
18 username:%s
19 password:%s
20 """
22 class PyPIRCCommand(Command):
23 """Base command that knows how to handle the .pypirc file
24 """
25 DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi'
26 DEFAULT_REALM = 'pypi'
27 repository = None
28 realm = None
30 user_options = [
31 ('repository=', 'r',
32 "url of repository [default: %s]" % \
33 DEFAULT_REPOSITORY),
34 ('show-response', None,
35 'display full response text from server')]
37 boolean_options = ['show-response']
39 def _get_rc_file(self):
40 """Returns rc file path."""
41 return os.path.join(os.path.expanduser('~'), '.pypirc')
43 def _store_pypirc(self, username, password):
44 """Creates a default .pypirc file."""
45 rc = self._get_rc_file()
46 f = open(rc, 'w')
47 try:
48 f.write(DEFAULT_PYPIRC % (username, password))
49 finally:
50 f.close()
51 try:
52 os.chmod(rc, 0600)
53 except OSError:
54 # should do something better here
55 pass
57 def _read_pypirc(self):
58 """Reads the .pypirc file."""
59 rc = self._get_rc_file()
60 if os.path.exists(rc):
61 self.announce('Using PyPI login from %s' % rc)
62 repository = self.repository or self.DEFAULT_REPOSITORY
63 realm = self.realm or self.DEFAULT_REALM
65 config = ConfigParser()
66 config.read(rc)
67 sections = config.sections()
68 if 'distutils' in sections:
69 # let's get the list of servers
70 index_servers = config.get('distutils', 'index-servers')
71 _servers = [server.strip() for server in
72 index_servers.split('\n')
73 if server.strip() != '']
74 if _servers == []:
75 # nothing set, let's try to get the default pypi
76 if 'pypi' in sections:
77 _servers = ['pypi']
78 else:
79 # the file is not properly defined, returning
80 # an empty dict
81 return {}
82 for server in _servers:
83 current = {'server': server}
84 current['username'] = config.get(server, 'username')
86 # optional params
87 for key, default in (('repository',
88 self.DEFAULT_REPOSITORY),
89 ('realm', self.DEFAULT_REALM),
90 ('password', None)):
91 if config.has_option(server, key):
92 current[key] = config.get(server, key)
93 else:
94 current[key] = default
95 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}
111 return {}
113 def initialize_options(self):
114 """Initialize options."""
115 self.repository = None
116 self.realm = None
117 self.show_response = 0
119 def finalize_options(self):
120 """Finalizes options."""
121 if self.repository is None:
122 self.repository = self.DEFAULT_REPOSITORY
123 if self.realm is None:
124 self.realm = self.DEFAULT_REALM