<s> import os <EOL> import stat <EOL> from urlparse import urlparse <EOL> import ConfigParser <EOL> from werckercli . cli import puts , get_term <EOL> import netrc <EOL> from werckercli . paths import find_git_root <EOL> VALUE_USER_NAME = "<STR_LIT>" <EOL> VALUE_USER_TOKEN = "<STR_LIT>" <EOL> VALUE_USER_NAME = "<STR_LIT>" <EOL> VALUE_PROJECT_ID = "<STR_LIT>" <EOL> VALUE_HEROKU_TOKEN = "<STR_LIT>" <EOL> VALUE_WERCKER_URL = "<STR_LIT>" <EOL> VALUE_MIXPANEL_TOKEN = "<STR_LIT>" <EOL> VALUE_DISPLAY_DEBUG = "<STR_LIT>" <EOL> ENV_KEY_WERCKER_URL = "<STR_LIT>" <EOL> ENV_KEY_MIXPANEL_TOKEN = "<STR_LIT>" <EOL> ENV_KEY_DISPLAY_DEBUG = "<STR_LIT>" <EOL> DEFAULT_WERCKER_URL = "<STR_LIT>" <EOL> DEFAULT_DOT_WERCKER_NAME = "<STR_LIT>" <EOL> DEFAULT_MIXPANEL_TOKEN = "<STR_LIT>" <EOL> DEFAULT_DISPLAY_DEBUG = False <EOL> DEFAULT_WERCKER_YML = "<STR_LIT>" <EOL> def _get_or_create_netrc_location ( ) : <EOL> term = get_term ( ) <EOL> try : <EOL> file = os . path . join ( os . environ [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> except KeyError : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> if os . path . isfile ( file ) : <EOL> result = os . stat ( file ) <EOL> mode = oct ( stat . S_IMODE ( result . st_mode ) ) <EOL> if mode != '<STR_LIT>' : <EOL> puts ( <EOL> term . yellow ( '<STR_LIT>' ) + <EOL> '<STR_LIT>' % <EOL> ( mode , file ) <EOL> ) <EOL> else : <EOL> with os . fdopen ( os . open ( file , os . O_APPEND | os . O_CREAT , <NUM_LIT> ) ) as out : <EOL> out . close ( ) <EOL> return file <EOL> def get_value ( name , default_value = None , path = os . curdir , print_warnings = True ) : <EOL> value = None <EOL> term = get_term ( ) <EOL> if name == VALUE_WERCKER_URL : <EOL> value = os . getenv ( "<STR_LIT>" , None ) <EOL> if value is None : <EOL> value = os . getenv ( ENV_KEY_WERCKER_URL , DEFAULT_WERCKER_URL ) <EOL> return value <EOL> elif name == VALUE_MIXPANEL_TOKEN : <EOL> value = os . getenv ( ENV_KEY_MIXPANEL_TOKEN , DEFAULT_MIXPANEL_TOKEN ) <EOL> elif name == VALUE_USER_TOKEN : <EOL> wercker_url = get_value ( VALUE_WERCKER_URL ) <EOL> url = urlparse ( wercker_url ) <EOL> file = _get_or_create_netrc_location ( ) <EOL> rc = netrc . netrc ( file ) <EOL> if url . hostname in rc . hosts : <EOL> value = rc . hosts [ url . hostname ] [ <NUM_LIT:2> ] <EOL> elif name == VALUE_USER_NAME : <EOL> wercker_url = get_value ( VALUE_WERCKER_URL ) <EOL> url = urlparse ( wercker_url ) <EOL> file = _get_or_create_netrc_location ( ) <EOL> rc = netrc . netrc ( file ) <EOL> if url . hostname in rc . hosts : <EOL> value = rc . hosts [ url . hostname ] [ <NUM_LIT:0> ] <EOL> elif name == VALUE_HEROKU_TOKEN : <EOL> file = _get_or_create_netrc_location ( ) <EOL> rc = netrc . netrc ( file ) <EOL> result = rc . authenticators ( '<STR_LIT>' ) <EOL> if result and len ( result ) == <NUM_LIT:3> : <EOL> value = result [ <NUM_LIT:2> ] <EOL> elif name == VALUE_PROJECT_ID : <EOL> path = find_git_root ( path ) <EOL> if not path : <EOL> if print_warnings : <EOL> puts ( <EOL> term . red ( "<STR_LIT>" ) + <EOL> "<STR_LIT>" <EOL> ) <EOL> return <EOL> file = os . path . join ( <EOL> path , <EOL> DEFAULT_DOT_WERCKER_NAME <EOL> ) <EOL> if not os . path . isfile ( file ) : <EOL> if print_warnings : <EOL> puts ( <EOL> term . yellow ( "<STR_LIT>" ) + <EOL> "<STR_LIT>" % <EOL> DEFAULT_DOT_WERCKER_NAME <EOL> ) <EOL> return <EOL> Config = ConfigParser . ConfigParser ( ) <EOL> Config . read ( file ) <EOL> try : <EOL> value = Config . get ( '<STR_LIT>' , '<STR_LIT:id>' ) <EOL> except ( <EOL> ConfigParser . NoOptionError , <EOL> ConfigParser . NoSectionError <EOL> ) : <EOL> value = None <EOL> elif name == VALUE_DISPLAY_DEBUG : <EOL> env_value = os . environ . get ( ENV_KEY_DISPLAY_DEBUG ) <EOL> if env_value is not None and env_value . lower ( ) == "<STR_LIT:true>" : <EOL> value = True <EOL> else : <EOL> value = DEFAULT_DISPLAY_DEBUG <EOL> return value <EOL> def set_value ( name , value ) : <EOL> term = get_term ( ) <EOL> if name == VALUE_USER_TOKEN : <EOL> file = _get_or_create_netrc_location ( ) <EOL> rc = netrc . netrc ( file = file ) <EOL> wercker_url = get_value ( VALUE_WERCKER_URL ) <EOL> url = urlparse ( wercker_url ) <EOL> if url . hostname in rc . hosts : <EOL> current_settings = rc . hosts [ url . hostname ] <EOL> else : <EOL> current_settings = ( None , None , None ) <EOL> if value is not None : <EOL> rc . hosts [ url . hostname ] = ( <EOL> current_settings [ <NUM_LIT:0> ] , <EOL> current_settings [ <NUM_LIT:1> ] , <EOL> value <EOL> ) <EOL> else : <EOL> rc . hosts . pop ( url . hostname ) <EOL> with open ( file , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( str ( rc ) ) <EOL> fp . close ( ) <EOL> elif name == VALUE_USER_NAME : <EOL> file = _get_or_create_netrc_location ( ) <EOL> rc = netrc . netrc ( file = file ) <EOL> wercker_url = get_value ( VALUE_WERCKER_URL ) <EOL> url = urlparse ( wercker_url ) <EOL> if url . hostname in rc . hosts : <EOL> current_settings = rc . hosts [ url . hostname ] <EOL> else : <EOL> current_settings = ( None , None , None ) <EOL> if value is not None : <EOL> rc . hosts [ url . hostname ] = ( <EOL> value , <EOL> current_settings [ <NUM_LIT:1> ] , <EOL> current_settings [ <NUM_LIT:2> ] ) <EOL> else : <EOL> rc . hosts . pop ( url . hostname ) <EOL> with open ( file , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( str ( rc ) ) <EOL> fp . close ( ) <EOL> elif name == VALUE_PROJECT_ID : <EOL> path = find_git_root ( os . curdir ) <EOL> if not path : <EOL> puts ( <EOL> term . red ( "<STR_LIT>" ) + <EOL> "<STR_LIT>" <EOL> ) <EOL> return <EOL> file = os . path . join ( <EOL> path , <EOL> DEFAULT_DOT_WERCKER_NAME <EOL> ) <EOL> Config = ConfigParser . ConfigParser ( ) <EOL> if os . path . isfile ( file ) : <EOL> Config . read ( file ) <EOL> if not '<STR_LIT>' in Config . sections ( ) : <EOL> Config . add_section ( '<STR_LIT>' ) <EOL> fp = open ( file , '<STR_LIT:w>' ) <EOL> Config . set ( '<STR_LIT>' , '<STR_LIT:id>' , value ) <EOL> Config . write ( fp ) <EOL> fp . close ( ) </s>
