blob: 9e18c00726c6536ff23a11db1ba07825f61610ec [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 """
Donald Stufft692497a2016-08-03 18:58:12 -040024 DEFAULT_REPOSITORY = 'https://upload.pypi.org/legacy/'
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000025 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()
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000050
51 def _read_pypirc(self):
52 """Reads the .pypirc file."""
53 rc = self._get_rc_file()
54 if os.path.exists(rc):
Martin v. Löwis68faf5b2008-05-24 09:00:04 +000055 self.announce('Using PyPI login from %s' % rc)
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000056 repository = self.repository or self.DEFAULT_REPOSITORY
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000057 config = ConfigParser()
58 config.read(rc)
59 sections = config.sections()
60 if 'distutils' in sections:
61 # let's get the list of servers
62 index_servers = config.get('distutils', 'index-servers')
63 _servers = [server.strip() for server in
64 index_servers.split('\n')
65 if server.strip() != '']
66 if _servers == []:
67 # nothing set, let's try to get the default pypi
68 if 'pypi' in sections:
69 _servers = ['pypi']
70 else:
71 # the file is not properly defined, returning
72 # an empty dict
73 return {}
74 for server in _servers:
75 current = {'server': server}
76 current['username'] = config.get(server, 'username')
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000077
78 # optional params
79 for key, default in (('repository',
80 self.DEFAULT_REPOSITORY),
Tarek Ziadé1a240fb2009-01-08 23:56:31 +000081 ('realm', self.DEFAULT_REALM),
82 ('password', None)):
Andrew M. Kuchling2bca2122008-05-10 22:12:38 +000083 if config.has_option(server, key):
84 current[key] = config.get(server, key)
85 else:
86 current[key] = default
87 if (current['server'] == repository or
88 current['repository'] == repository):
89 return current
90 elif 'server-login' in sections:
91 # old format
92 server = 'server-login'
93 if config.has_option(server, 'repository'):
94 repository = config.get(server, 'repository')
95 else:
96 repository = self.DEFAULT_REPOSITORY
97 return {'username': config.get(server, 'username'),
98 'password': config.get(server, 'password'),
99 'repository': repository,
100 'server': server,
101 'realm': self.DEFAULT_REALM}
102
103 return {}
104
105 def initialize_options(self):
106 """Initialize options."""
107 self.repository = None
108 self.realm = None
109 self.show_response = 0
110
111 def finalize_options(self):
112 """Finalizes options."""
113 if self.repository is None:
114 self.repository = self.DEFAULT_REPOSITORY
115 if self.realm is None:
116 self.realm = self.DEFAULT_REALM