88__revision__ = "$Id$"
99
1010import os , string , urllib2 , getpass , urlparse
11- import StringIO , ConfigParser
11+ import StringIO
1212
13- from distutils .core import Command
13+ from distutils .core import PyPIRCCommand
1414from distutils .errors import *
15+ from distutils import log
1516
16- class register (Command ):
17+ class register (PyPIRCCommand ):
1718
1819 description = ("register the distribution with the Python package index" )
19-
20- DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi'
21-
22- user_options = [
23- ('repository=' , 'r' ,
24- "url of repository [default: %s]" % DEFAULT_REPOSITORY ),
20+ user_options = PyPIRCCommand .user_options + [
2521 ('list-classifiers' , None ,
2622 'list the valid Trove classifiers' ),
27- ('show-response' , None ,
28- 'display full response text from server' ),
2923 ]
30- boolean_options = ['verify' , 'show-response' , 'list-classifiers' ]
24+ boolean_options = PyPIRCCommand .boolean_options + [
25+ 'verify' , 'list-classifiers' ]
3126
3227 def initialize_options (self ):
33- self .repository = None
34- self .show_response = 0
28+ PyPIRCCommand .initialize_options (self )
3529 self .list_classifiers = 0
3630
37- def finalize_options (self ):
38- if self .repository is None :
39- self .repository = self .DEFAULT_REPOSITORY
40-
4131 def run (self ):
32+ self .finalize_options ()
33+ self ._set_config ()
4234 self .check_metadata ()
4335 if self .dry_run :
4436 self .verify_metadata ()
@@ -77,6 +69,23 @@ def check_metadata(self):
7769 "or (maintainer and maintainer_email) " +
7870 "must be supplied" )
7971
72+ def _set_config (self ):
73+ ''' Reads the configuration file and set attributes.
74+ '''
75+ config = self ._read_pypirc ()
76+ if config != {}:
77+ self .username = config ['username' ]
78+ self .password = config ['password' ]
79+ self .repository = config ['repository' ]
80+ self .realm = config ['realm' ]
81+ self .has_config = True
82+ else :
83+ if self .repository not in ('pypi' , self .DEFAULT_REPOSITORY ):
84+ raise ValueError ('%s not found in .pypirc' % self .repository )
85+ if self .repository == 'pypi' :
86+ self .repository = self .DEFAULT_REPOSITORY
87+ self .has_config = False
88+
8089 def classifiers (self ):
8190 ''' Fetch the list of classifiers from the server.
8291 '''
@@ -90,6 +99,7 @@ def verify_metadata(self):
9099 (code , result ) = self .post_to_server (self .build_post_data ('verify' ))
91100 print 'Server response (%s): %s' % (code , result )
92101
102+
93103 def send_metadata (self ):
94104 ''' Send the metadata to the package index server.
95105
@@ -99,10 +109,14 @@ def send_metadata(self):
99109
100110 First we try to read the username/password from $HOME/.pypirc,
101111 which is a ConfigParser-formatted file with a section
102- [server-login ] containing username and password entries (both
112+ [distutils ] containing username and password entries (both
103113 in clear text). Eg:
104114
105- [server-login]
115+ [distutils]
116+ index-servers =
117+ pypi
118+
119+ [pypi]
106120 username: fred
107121 password: sekrit
108122
@@ -114,21 +128,15 @@ def send_metadata(self):
114128 3. set the password to a random string and email the user.
115129
116130 '''
117- choice = 'x'
118- username = password = ''
119-
120131 # see if we can short-cut and get the username/password from the
121132 # config
122- config = None
123- if 'HOME' in os .environ :
124- rc = os .path .join (os .environ ['HOME' ], '.pypirc' )
125- if os .path .exists (rc ):
126- print 'Using PyPI login from %s' % rc
127- config = ConfigParser .ConfigParser ()
128- config .read (rc )
129- username = config .get ('server-login' , 'username' )
130- password = config .get ('server-login' , 'password' )
131- choice = '1'
133+ if self .has_config :
134+ choice = '1'
135+ username = self .username
136+ password = self .password
137+ else :
138+ choice = 'x'
139+ username = password = ''
132140
133141 # get the user's login info
134142 choices = '1 2 3 4' .split ()
@@ -155,32 +163,24 @@ def send_metadata(self):
155163 # set up the authentication
156164 auth = urllib2 .HTTPPasswordMgr ()
157165 host = urlparse .urlparse (self .repository )[1 ]
158- auth .add_password ('pypi' , host , username , password )
159-
166+ auth .add_password (self .realm , host , username , password )
160167 # send the info to the server and report the result
161168 code , result = self .post_to_server (self .build_post_data ('submit' ),
162169 auth )
163- print 'Server response (%s): %s' % (code , result )
170+ print 'Server response (%s): %s' % (code , result )
164171
165172 # possibly save the login
166- if 'HOME' in os .environ and config is None and code == 200 :
167- rc = os .path .join (os .environ ['HOME' ], '.pypirc' )
173+ if not self .has_config and code == 200 :
168174 print 'I can store your PyPI login so future submissions will be faster.'
169- print '(the login will be stored in %s)' % rc
175+ print '(the login will be stored in %s)' % self . _get_rc_file ()
170176 choice = 'X'
171177 while choice .lower () not in 'yn' :
172178 choice = raw_input ('Save your login (y/N)?' )
173179 if not choice :
174180 choice = 'n'
175181 if choice .lower () == 'y' :
176- f = open (rc , 'w' )
177- f .write ('[server-login]\n username:%s\n password:%s\n ' % (
178- username , password ))
179- f .close ()
180- try :
181- os .chmod (rc , 0600 )
182- except :
183- pass
182+ self ._store_pypirc (username , password )
183+
184184 elif choice == '2' :
185185 data = {':action' : 'user' }
186186 data ['name' ] = data ['password' ] = data ['email' ] = ''
@@ -243,7 +243,8 @@ def build_post_data(self, action):
243243 def post_to_server (self , data , auth = None ):
244244 ''' Post a query to the server, and return a string response.
245245 '''
246-
246+ self .announce ('Registering %s to %s' % (data ['name' ],
247+ self .repository ), log .INFO )
247248 # Build up the MIME payload for the urllib2 POST data
248249 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
249250 sep_boundary = '\n --' + boundary
0 commit comments