3 Provides the PyPIRCCommand class, the base class for the command classes
4 that uses .pypirc in the distutils.command package.
7 from ConfigParser
import ConfigParser
9 from distutils
.cmd
import Command
21 class PyPIRCCommand(Command
):
22 """Base command that knows how to handle the .pypirc file
24 DEFAULT_REPOSITORY
= 'http://pypi.python.org/pypi'
25 DEFAULT_REALM
= 'pypi'
31 "url of repository [default: %s]" % \
33 ('show-response', None,
34 'display full response text from server')]
36 boolean_options
= ['show-response']
38 def _get_rc_file(self
):
39 """Returns rc file path."""
40 return os
.path
.join(os
.path
.expanduser('~'), '.pypirc')
42 def _store_pypirc(self
, username
, password
):
43 """Creates a default .pypirc file."""
44 rc
= self
._get
_rc
_file
()
47 f
.write(DEFAULT_PYPIRC
% (username
, password
))
53 # should do something better here
56 def _read_pypirc(self
):
57 """Reads the .pypirc file."""
58 rc
= self
._get
_rc
_file
()
59 if os
.path
.exists(rc
):
60 self
.announce('Using PyPI login from %s' % rc
)
61 repository
= self
.repository
or self
.DEFAULT_REPOSITORY
62 config
= ConfigParser()
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() != '']
72 # nothing set, let's try to get the default pypi
73 if 'pypi' in sections
:
76 # the file is not properly defined, returning
79 for server
in _servers
:
80 current
= {'server': server
}
81 current
['username'] = config
.get(server
, 'username')
84 for key
, default
in (('repository',
85 self
.DEFAULT_REPOSITORY
),
86 ('realm', self
.DEFAULT_REALM
),
88 if config
.has_option(server
, key
):
89 current
[key
] = config
.get(server
, key
)
91 current
[key
] = default
92 if (current
['server'] == repository
or
93 current
['repository'] == repository
):
95 elif 'server-login' in sections
:
97 server
= 'server-login'
98 if config
.has_option(server
, 'repository'):
99 repository
= config
.get(server
, 'repository')
101 repository
= self
.DEFAULT_REPOSITORY
102 return {'username': config
.get(server
, 'username'),
103 'password': config
.get(server
, 'password'),
104 'repository': repository
,
106 'realm': self
.DEFAULT_REALM
}
110 def initialize_options(self
):
111 """Initialize options."""
112 self
.repository
= None
114 self
.show_response
= 0
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