3 Provides the PyPIRCCommand class, the base class for the command classes
4 that uses .pypirc in the distutils.command package.
8 from ConfigParser
import ConfigParser
10 from distutils
.cmd
import Command
22 class PyPIRCCommand(Command
):
23 """Base command that knows how to handle the .pypirc file
25 DEFAULT_REPOSITORY
= 'http://pypi.python.org/pypi'
26 DEFAULT_REALM
= 'pypi'
32 "url of repository [default: %s]" % \
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
()
48 f
.write(DEFAULT_PYPIRC
% (username
, password
))
54 # should do something better here
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()
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() != '']
75 # nothing set, let's try to get the default pypi
76 if 'pypi' in sections
:
79 # the file is not properly defined, returning
82 for server
in _servers
:
83 current
= {'server': server
}
84 current
['username'] = config
.get(server
, 'username')
87 for key
, default
in (('repository',
88 self
.DEFAULT_REPOSITORY
),
89 ('realm', self
.DEFAULT_REALM
),
91 if config
.has_option(server
, key
):
92 current
[key
] = config
.get(server
, key
)
94 current
[key
] = default
95 if (current
['server'] == repository
or
96 current
['repository'] == repository
):
98 elif 'server-login' in sections
:
100 server
= 'server-login'
101 if config
.has_option(server
, 'repository'):
102 repository
= config
.get(server
, 'repository')
104 repository
= self
.DEFAULT_REPOSITORY
105 return {'username': config
.get(server
, 'username'),
106 'password': config
.get(server
, 'password'),
107 'repository': repository
,
109 'realm': self
.DEFAULT_REALM
}
113 def initialize_options(self
):
114 """Initialize options."""
115 self
.repository
= 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