blob: e07f8ac998b5aca2fcef599d0eceb18e229c8806 [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
Alexandre Vassalottieb8cef22008-05-16 02:06:59 +00008try:
9 from configparser import ConfigParser
10except ImportError:
11 # For backward-compatibility with Python versions < 2.6.
12 from ConfigParser import ConfigParser
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000013
Andrew M. Kuchlinga5c38782008-05-15 20:07:39 +000014from distutils.cmd import Command
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000015
16DEFAULT_PYPIRC = """\
17[pypirc]
18servers =
19 pypi
20
21[pypi]
22username:%s
23password:%s
24"""
25
26class PyPIRCCommand(Command):
27 """Base command that knows how to handle the .pypirc file
28 """
29 DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi'
30 DEFAULT_REALM = 'pypi'
31 repository = None
32 realm = None
33
34 user_options = [
35 ('repository=', 'r',
36 "url of repository [default: %s]" % \
37 DEFAULT_REPOSITORY),
38 ('show-response', None,
39 'display full response text from server')]
40
41 boolean_options = ['show-response']
42
43 def _get_rc_file(self):
44 """Returns rc file path."""
45 return os.path.join(os.path.expanduser('~'), '.pypirc')
46
47 def _store_pypirc(self, username, password):
48 """Creates a default .pypirc file."""
49 rc = self._get_rc_file()
50 f = open(rc, 'w')
51 try:
52 f.write(DEFAULT_PYPIRC % (username, password))
53 finally:
54 f.close()
55 try:
56 os.chmod(rc, 0600)
57 except OSError:
58 # should do something better here
59 pass
60
61 def _read_pypirc(self):
62 """Reads the .pypirc file."""
63 rc = self._get_rc_file()
64 if os.path.exists(rc):
Martin v. Löwis68faf5b2008-05-24 09:00:04 +000065 self.announce('Using PyPI login from %s' % rc)
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000066 repository = self.repository or self.DEFAULT_REPOSITORY
67 realm = self.realm or self.DEFAULT_REALM
68
69 config = ConfigParser()
70 config.read(rc)
71 sections = config.sections()
72 if 'distutils' in sections:
73 # let's get the list of servers
74 index_servers = config.get('distutils', 'index-servers')
75 _servers = [server.strip() for server in
76 index_servers.split('\n')
77 if server.strip() != '']
78 if _servers == []:
79 # nothing set, let's try to get the default pypi
80 if 'pypi' in sections:
81 _servers = ['pypi']
82 else:
83 # the file is not properly defined, returning
84 # an empty dict
85 return {}
86 for server in _servers:
87 current = {'server': server}
88 current['username'] = config.get(server, 'username')
89 current['password'] = config.get(server, 'password')
90
91 # optional params
92 for key, default in (('repository',
93 self.DEFAULT_REPOSITORY),
94 ('realm', self.DEFAULT_REALM)):
95 if config.has_option(server, key):
96 current[key] = config.get(server, key)
97 else:
98 current[key] = default
99 if (current['server'] == repository or
100 current['repository'] == repository):
101 return current
102 elif 'server-login' in sections:
103 # old format
104 server = 'server-login'
105 if config.has_option(server, 'repository'):
106 repository = config.get(server, 'repository')
107 else:
108 repository = self.DEFAULT_REPOSITORY
109 return {'username': config.get(server, 'username'),
110 'password': config.get(server, 'password'),
111 'repository': repository,
112 'server': server,
113 'realm': self.DEFAULT_REALM}
114
115 return {}
116
117 def initialize_options(self):
118 """Initialize options."""
119 self.repository = None
120 self.realm = None
121 self.show_response = 0
122
123 def finalize_options(self):
124 """Finalizes options."""
125 if self.repository is None:
126 self.repository = self.DEFAULT_REPOSITORY
127 if self.realm is None:
128 self.realm = self.DEFAULT_REALM