<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import sys , os , string , urllib2 , getpass , urlparse <EOL> import StringIO , ConfigParser <EOL> from distutils . core import Command <EOL> from distutils . errors import * <EOL> class register ( Command ) : <EOL> description = ( "<STR_LIT>" ) <EOL> DEFAULT_REPOSITORY = '<STR_LIT>' <EOL> user_options = [ <EOL> ( '<STR_LIT>' , '<STR_LIT:r>' , <EOL> "<STR_LIT>" % DEFAULT_REPOSITORY ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> boolean_options = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def initialize_options ( self ) : <EOL> self . repository = None <EOL> self . show_response = <NUM_LIT:0> <EOL> self . list_classifiers = <NUM_LIT:0> <EOL> def finalize_options ( self ) : <EOL> if self . repository is None : <EOL> self . repository = self . DEFAULT_REPOSITORY <EOL> def run ( self ) : <EOL> self . check_metadata ( ) <EOL> if self . dry_run : <EOL> self . verify_metadata ( ) <EOL> elif self . list_classifiers : <EOL> self . classifiers ( ) <EOL> else : <EOL> self . send_metadata ( ) <EOL> def check_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> metadata = self . distribution . metadata <EOL> missing = [ ] <EOL> for attr in ( '<STR_LIT:name>' , '<STR_LIT:version>' , '<STR_LIT:url>' ) : <EOL> if not ( hasattr ( metadata , attr ) and getattr ( metadata , attr ) ) : <EOL> missing . append ( attr ) <EOL> if missing : <EOL> self . warn ( "<STR_LIT>" + <EOL> string . join ( missing , "<STR_LIT:U+002CU+0020>" ) ) <EOL> if metadata . author : <EOL> if not metadata . author_email : <EOL> self . warn ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> elif metadata . maintainer : <EOL> if not metadata . maintainer_email : <EOL> self . warn ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . warn ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> def classifiers ( self ) : <EOL> '''<STR_LIT>''' <EOL> response = urllib2 . urlopen ( self . repository + '<STR_LIT>' ) <EOL> print response . read ( ) <EOL> def verify_metadata ( self ) : <EOL> '''<STR_LIT>''' <EOL> ( code , result ) = self . post_to_server ( self . build_post_data ( '<STR_LIT>' ) ) <EOL> print '<STR_LIT>' % ( code , result ) <EOL> def send_metadata ( self ) : <EOL> '''<STR_LIT>''' <EOL> choice = '<STR_LIT:x>' <EOL> username = password = '<STR_LIT>' <EOL> config = None <EOL> if os . environ . has_key ( '<STR_LIT>' ) : <EOL> rc = os . path . join ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> if os . path . exists ( rc ) : <EOL> print '<STR_LIT>' % rc <EOL> config = ConfigParser . ConfigParser ( ) <EOL> config . read ( rc ) <EOL> username = config . get ( '<STR_LIT>' , '<STR_LIT:username>' ) <EOL> password = config . get ( '<STR_LIT>' , '<STR_LIT:password>' ) <EOL> choice = '<STR_LIT:1>' <EOL> choices = '<STR_LIT>' . split ( ) <EOL> while choice not in choices : <EOL> print '''<STR_LIT>''' , <EOL> choice = raw_input ( ) <EOL> if not choice : <EOL> choice = '<STR_LIT:1>' <EOL> elif choice not in choices : <EOL> print '<STR_LIT>' <EOL> if choice == '<STR_LIT:1>' : <EOL> while not username : <EOL> username = raw_input ( '<STR_LIT>' ) <EOL> while not password : <EOL> password = getpass . getpass ( '<STR_LIT>' ) <EOL> auth = urllib2 . HTTPPasswordMgr ( ) <EOL> host = urlparse . urlparse ( self . repository ) [ <NUM_LIT:1> ] <EOL> auth . add_password ( '<STR_LIT>' , host , username , password ) <EOL> code , result = self . post_to_server ( self . build_post_data ( '<STR_LIT>' ) , <EOL> auth ) <EOL> print '<STR_LIT>' % ( code , result ) <EOL> if os . environ . has_key ( '<STR_LIT>' ) and config is None and code == <NUM_LIT:200> : <EOL> rc = os . path . join ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % rc <EOL> choice = '<STR_LIT:X>' <EOL> while choice . lower ( ) not in '<STR_LIT>' : <EOL> choice = raw_input ( '<STR_LIT>' ) <EOL> if not choice : <EOL> choice = '<STR_LIT:n>' <EOL> if choice . lower ( ) == '<STR_LIT:y>' : <EOL> f = open ( rc , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT>' % ( <EOL> username , password ) ) <EOL> f . close ( ) <EOL> try : <EOL> os . chmod ( rc , <NUM_LIT:0> <NUM_LIT> ) <EOL> except : <EOL> pass <EOL> elif choice == '<STR_LIT:2>' : <EOL> data = { '<STR_LIT>' : '<STR_LIT:user>' } <EOL> data [ '<STR_LIT:name>' ] = data [ '<STR_LIT:password>' ] = data [ '<STR_LIT:email>' ] = '<STR_LIT>' <EOL> data [ '<STR_LIT>' ] = None <EOL> while not data [ '<STR_LIT:name>' ] : <EOL> data [ '<STR_LIT:name>' ] = raw_input ( '<STR_LIT>' ) <EOL> while data [ '<STR_LIT:password>' ] != data [ '<STR_LIT>' ] : <EOL> while not data [ '<STR_LIT:password>' ] : <EOL> data [ '<STR_LIT:password>' ] = getpass . getpass ( '<STR_LIT>' ) <EOL> while not data [ '<STR_LIT>' ] : <EOL> data [ '<STR_LIT>' ] = getpass . getpass ( '<STR_LIT>' ) <EOL> if data [ '<STR_LIT:password>' ] != data [ '<STR_LIT>' ] : <EOL> data [ '<STR_LIT:password>' ] = '<STR_LIT>' <EOL> data [ '<STR_LIT>' ] = None <EOL> print "<STR_LIT>" <EOL> while not data [ '<STR_LIT:email>' ] : <EOL> data [ '<STR_LIT:email>' ] = raw_input ( '<STR_LIT>' ) <EOL> code , result = self . post_to_server ( data ) <EOL> if code != <NUM_LIT:200> : <EOL> print '<STR_LIT>' % ( code , result ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> elif choice == '<STR_LIT:3>' : <EOL> data = { '<STR_LIT>' : '<STR_LIT>' } <EOL> data [ '<STR_LIT:email>' ] = '<STR_LIT>' <EOL> while not data [ '<STR_LIT:email>' ] : <EOL> data [ '<STR_LIT:email>' ] = raw_input ( '<STR_LIT>' ) <EOL> code , result = self . post_to_server ( data ) <EOL> print '<STR_LIT>' % ( code , result ) <EOL> def build_post_data ( self , action ) : <EOL> meta = self . distribution . metadata <EOL> data = { <EOL> '<STR_LIT>' : action , <EOL> '<STR_LIT>' : '<STR_LIT:1.0>' , <EOL> '<STR_LIT:name>' : meta . get_name ( ) , <EOL> '<STR_LIT:version>' : meta . get_version ( ) , <EOL> '<STR_LIT>' : meta . get_description ( ) , <EOL> '<STR_LIT>' : meta . get_url ( ) , <EOL> '<STR_LIT>' : meta . get_contact ( ) , <EOL> '<STR_LIT>' : meta . get_contact_email ( ) , <EOL> '<STR_LIT>' : meta . get_licence ( ) , <EOL> '<STR_LIT:description>' : meta . get_long_description ( ) , <EOL> '<STR_LIT>' : meta . get_keywords ( ) , <EOL> '<STR_LIT>' : meta . get_platforms ( ) , <EOL> '<STR_LIT>' : meta . get_classifiers ( ) , <EOL> '<STR_LIT>' : meta . get_download_url ( ) , <EOL> '<STR_LIT>' : meta . get_provides ( ) , <EOL> '<STR_LIT>' : meta . get_requires ( ) , <EOL> '<STR_LIT>' : meta . get_obsoletes ( ) , <EOL> } <EOL> if data [ '<STR_LIT>' ] or data [ '<STR_LIT>' ] or data [ '<STR_LIT>' ] : <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return data <EOL> def post_to_server ( self , data , auth = None ) : <EOL> '''<STR_LIT>''' <EOL> boundary = '<STR_LIT>' <EOL> sep_boundary = '<STR_LIT>' + boundary <EOL> end_boundary = sep_boundary + '<STR_LIT>' <EOL> body = StringIO . StringIO ( ) <EOL> for key , value in data . items ( ) : <EOL> if type ( value ) not in ( type ( [ ] ) , type ( ( ) ) ) : <EOL> value = [ value ] <EOL> for value in value : <EOL> value = unicode ( value ) . encode ( "<STR_LIT:utf-8>" ) <EOL> body . write ( sep_boundary ) <EOL> body . write ( '<STR_LIT>' % key ) <EOL> body . write ( "<STR_LIT>" ) <EOL> body . write ( value ) <EOL> if value and value [ - <NUM_LIT:1> ] == '<STR_LIT:\r>' : <EOL> body . write ( '<STR_LIT:\n>' ) <EOL> body . write ( end_boundary ) <EOL> body . write ( "<STR_LIT:\n>" ) <EOL> body = body . getvalue ( ) <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' % boundary , <EOL> '<STR_LIT>' : str ( len ( body ) ) <EOL> } <EOL> req = urllib2 . Request ( self . repository , body , headers ) <EOL> opener = urllib2 . build_opener ( <EOL> urllib2 . HTTPBasicAuthHandler ( password_mgr = auth ) <EOL> ) <EOL> data = '<STR_LIT>' <EOL> try : <EOL> result = opener . open ( req ) <EOL> except urllib2 . HTTPError , e : <EOL> if self . show_response : <EOL> data = e . fp . read ( ) <EOL> result = e . code , e . msg <EOL> except urllib2 . URLError , e : <EOL> result = <NUM_LIT> , str ( e ) <EOL> else : <EOL> if self . show_response : <EOL> data = result . read ( ) <EOL> result = <NUM_LIT:200> , '<STR_LIT:OK>' <EOL> if self . show_response : <EOL> print '<STR_LIT:->' * <NUM_LIT> , data , '<STR_LIT:->' * <NUM_LIT> <EOL> return result </s>
