blob: 9d8b30ea30c6ffebda558857569552176fc6daa9 [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
Georg Brandl392c6fc2008-05-25 07:25:25 +00007from ConfigParser import ConfigParser
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +00008
Andrew M. Kuchlinga5c38782008-05-15 20:07:39 +00009from distutils.cmd import Command
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000010
11DEFAULT_PYPIRC = """\
Tarek Ziadée4c75bb2008-12-24 19:10:05 +000012[distutils]
13index-servers =
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +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 Araujoe5567cc2012-07-03 01:23:46 -040045 f = os.fdopen(os.open(rc, os.O_CREAT | os.O_WRONLY, 0600), 'w')
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000046 try:
47 f.write(DEFAULT_PYPIRC % (username, password))
48 finally:
49 f.close()
50 try:
51 os.chmod(rc, 0600)
52 except OSError:
53 # should do something better here
54 pass
55
56 def _read_pypirc(self):
57 """Reads the .pypirc file."""
58 rc = self._get_rc_file()
59 if os.path.exists(rc):
Martin v. Löwis68faf5b2008-05-24 09:00:04 +000060 self.announce('Using PyPI login from %s' % rc)
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000061 repository = self.repository or self.DEFAULT_REPOSITORY
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000062 config = ConfigParser()
63 config.read(rc)
64 sections = config.sections()
65 if 'distutils' in sections:
66 # let's get the list of servers
67 index_servers = config.get('distutils', 'index-servers')
68 _servers = [server.strip() for server in
69 index_servers.split('\n')
70 if server.strip() != '']
71 if _servers == []:
72 # nothing set, let's try to get the default pypi
73 if 'pypi' in sections:
74 _servers = ['pypi']
75 else:
76 # the file is not properly defined, returning
77 # an empty dict
78 return {}
79 for server in _servers:
80 current = {'server': server}
81 current['username'] = config.get(server, 'username')
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000082
83 # optional params
84 for key, default in (('repository',
85 self.DEFAULT_REPOSITORY),
Tarek Ziadé1a240fb2009-01-08 23:56:31 +000086 ('realm', self.DEFAULT_REALM),
87 ('password', None)):
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000088 if config.has_option(server, key):
89 current[key] = config.get(server, key)
90 else:
91 current[key] = default
92 if (current['server'] == repository or
93 current['repository'] == repository):
94 return current
95 elif 'server-login' in sections:
96 # old format
97 server = 'server-login'
98 if config.has_option(server, 'repository'):
99 repository = config.get(server, 'repository')
100 else:
101 repository = self.DEFAULT_REPOSITORY
102 return {'username': config.get(server, 'username'),
103 'password': config.get(server, 'password'),
104 'repository': repository,
105 'server': server,
106 'realm': self.DEFAULT_REALM}
107
108 return {}
109
110 def initialize_options(self):
111 """Initialize options."""
112 self.repository = None
113 self.realm = None
114 self.show_response = 0
115
116 def finalize_options(self):
117 """Finalizes options."""
118 if self.repository is None:
119 self.repository = self.DEFAULT_REPOSITORY
120 if self.realm is None:
121 self.realm = self.DEFAULT_REALM