blob: e3a4c57e331dc92064d4e9388ac457d563ba0970 [file] [log] [blame]
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +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
7import sys
Georg Brandl392c6fc2008-05-25 07:25:25 +00008from ConfigParser import ConfigParser
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +00009
Andrew M. Kuchlinga5c38782008-05-15 20:07:39 +000010from distutils.cmd import Command
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000011
12DEFAULT_PYPIRC = """\
13[pypirc]
14servers =
15 pypi
16
17[pypi]
18username:%s
19password:%s
20"""
21
22class 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
29
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')]
36
37 boolean_options = ['show-response']
38
39 def _get_rc_file(self):
40 """Returns rc file path."""
41 return os.path.join(os.path.expanduser('~'), '.pypirc')
42
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
56
57 def _read_pypirc(self):
58 """Reads the .pypirc file."""
59 rc = self._get_rc_file()
60 if os.path.exists(rc):
Martin v. Löwis68faf5b2008-05-24 09:00:04 +000061 self.announce('Using PyPI login from %s' % rc)
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000062 repository = self.repository or self.DEFAULT_REPOSITORY
63 realm = self.realm or self.DEFAULT_REALM
64
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')
85 current['password'] = config.get(server, 'password')
86
87 # optional params
88 for key, default in (('repository',
89 self.DEFAULT_REPOSITORY),
90 ('realm', self.DEFAULT_REALM)):
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}
110
111 return {}
112
113 def initialize_options(self):
114 """Initialize options."""
115 self.repository = None
116 self.realm = None
117 self.show_response = 0
118
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