<s> """<STR_LIT>""" <EOL> import os <EOL> import os . path as op <EOL> import datetime <EOL> import shutil <EOL> from xml . etree import ElementTree <EOL> import urllib2 <EOL> import re <EOL> from getpass import getpass <EOL> try : <EOL> from myproxy . client import MyProxyClient <EOL> import OpenSSL <EOL> _has_myproxy = True <EOL> except ImportError : <EOL> _has_myproxy = False <EOL> from . exceptions import OpenidResolutionError <EOL> ESGF_DIR = op . join ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> ESGF_CERTS_DIR = '<STR_LIT>' <EOL> ESGF_CREDENTIALS = '<STR_LIT>' <EOL> DAP_CONFIG = op . join ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> DAP_CONFIG_MARKER = '<STR_LIT>' <EOL> XRI_NS = '<STR_LIT>' <EOL> MYPROXY_URN = '<STR_LIT>' <EOL> ESGF_OPENID_REXP = r'<STR_LIT>' <EOL> MYPROXY_URI_REXP = r'<STR_LIT>' <EOL> class LogonManager ( object ) : <EOL> """<STR_LIT>""" <EOL> STATE_LOGGED_ON = <NUM_LIT:0> <EOL> STATE_NO_CREDENTIALS = <NUM_LIT:1> <EOL> STATE_EXPIRED_CREDENTIALS = <NUM_LIT:2> <EOL> STATE_INVALID_CREDENTIALS = <NUM_LIT:3> <EOL> def __init__ ( self , esgf_dir = ESGF_DIR , dap_config = DAP_CONFIG ) : <EOL> """<STR_LIT>""" <EOL> if not _has_myproxy : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> self . esgf_dir = esgf_dir <EOL> self . esgf_credentials = op . join ( self . esgf_dir , ESGF_CREDENTIALS ) <EOL> self . esgf_certs_dir = op . join ( self . esgf_dir , ESGF_CERTS_DIR ) <EOL> self . dap_config = dap_config <EOL> self . _write_dap_config ( ) <EOL> @ property <EOL> def state ( self ) : <EOL> if not op . exists ( self . esgf_credentials ) : <EOL> return self . STATE_NO_CREDENTIALS <EOL> else : <EOL> with open ( self . esgf_credentials ) as fh : <EOL> data = fh . read ( ) <EOL> cert = OpenSSL . crypto . load_certificate ( OpenSSL . SSL . FILETYPE_PEM , data ) <EOL> if cert . has_expired ( ) : <EOL> return self . STATE_EXPIRED_CREDENTIALS <EOL> return self . STATE_LOGGED_ON <EOL> def is_logged_on ( self ) : <EOL> return self . state == self . STATE_LOGGED_ON <EOL> def logon_with_openid ( self , openid , password = None , <EOL> bootstrap = False , update_trustroots = True , <EOL> interactive = True ) : <EOL> """<STR_LIT>""" <EOL> username , myproxy = self . _get_logon_details ( openid ) <EOL> return self . logon ( username , password , myproxy , <EOL> bootstrap = bootstrap , <EOL> update_trustroots = update_trustroots , <EOL> interactive = interactive ) <EOL> def logon ( self , username = None , password = None , hostname = None , <EOL> bootstrap = False , update_trustroots = True , <EOL> interactive = True ) : <EOL> """<STR_LIT>""" <EOL> if interactive : <EOL> if hostname is None : <EOL> print '<STR_LIT>' , <EOL> hostname = raw_input ( ) <EOL> if username is None : <EOL> print '<STR_LIT>' , <EOL> username = raw_input ( ) <EOL> if password is None : <EOL> password = getpass ( '<STR_LIT>' % username ) <EOL> if None in ( hostname , username , password ) : <EOL> raise OpenidResolutionError ( '<STR_LIT>' ) <EOL> c = MyProxyClient ( hostname = hostname , caCertDir = self . esgf_certs_dir ) <EOL> creds = c . logon ( username , password , <EOL> bootstrap = bootstrap , updateTrustRoots = update_trustroots ) <EOL> with open ( self . esgf_credentials , '<STR_LIT:w>' ) as fh : <EOL> for cred in creds : <EOL> fh . write ( cred ) <EOL> def logoff ( self , clear_trustroots = False ) : <EOL> """<STR_LIT>""" <EOL> if op . exists ( self . esgf_credentials ) : <EOL> os . remove ( self . esgf_credentials ) <EOL> if clear_trustroots : <EOL> shutil . rmtree ( self . esgf_certs_dir ) <EOL> def _get_logon_details ( self , openid ) : <EOL> openid_doc = urllib2 . urlopen ( openid ) . read ( ) <EOL> xml = ElementTree . fromstring ( openid_doc ) <EOL> hostname = None <EOL> port = None <EOL> username = None <EOL> services = xml . findall ( '<STR_LIT>' % XRI_NS ) <EOL> for service in services : <EOL> try : <EOL> service_type = service . find ( '<STR_LIT>' % XRI_NS ) . text <EOL> except AttributeError : <EOL> continue <EOL> if service_type == MYPROXY_URN : <EOL> myproxy_uri = service . find ( '<STR_LIT>' % XRI_NS ) . text <EOL> mo = re . match ( MYPROXY_URI_REXP , myproxy_uri ) <EOL> if mo : <EOL> hostname , port = mo . groups ( ) <EOL> mo = re . match ( ESGF_OPENID_REXP , openid ) <EOL> if mo : <EOL> username = mo . group ( <NUM_LIT:1> ) <EOL> if port is not None : <EOL> assert int ( port ) == <NUM_LIT> <EOL> return username , hostname <EOL> def _write_dap_config ( self , verbose = False , validate = False ) : <EOL> preamble , managed , postamble = self . _parse_dap_config ( ) <EOL> with open ( self . dap_config , '<STR_LIT:w>' ) as fh : <EOL> fh . write ( """<STR_LIT>""" . format ( verbose = <NUM_LIT:1> if verbose else <NUM_LIT:0> , <EOL> validate = <NUM_LIT:1> if validate else <NUM_LIT:0> , <EOL> esgf_certs_dir = self . esgf_certs_dir , <EOL> esgf_dir = self . esgf_dir , <EOL> marker = DAP_CONFIG_MARKER , <EOL> preamble = preamble , <EOL> postamble = postamble , <EOL> ) ) <EOL> def _parse_dap_config ( self , config_str = None ) : <EOL> """<STR_LIT>""" <EOL> if config_str is None : <EOL> if not op . exists ( self . dap_config ) : <EOL> return ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> config_str = open ( self . dap_config ) . read ( ) <EOL> sections = re . split ( r'<STR_LIT>' . format ( DAP_CONFIG_MARKER ) , <EOL> config_str , re . M ) <EOL> if len ( sections ) < <NUM_LIT:2> : <EOL> preamble , managed , postamble = sections [ <NUM_LIT:0> ] , '<STR_LIT>' , '<STR_LIT>' <EOL> elif len ( sections ) == <NUM_LIT:2> : <EOL> preamble , managed , postamble = sections + [ '<STR_LIT>' ] <EOL> elif len ( sections ) == <NUM_LIT:3> : <EOL> preamble , managed , postamble = sections <EOL> else : <EOL> managed , unmanaged = [ ] , [ ] <EOL> sections . reverse ( ) <EOL> while sections : <EOL> unmanaged . append ( sections . pop ( ) ) <EOL> if sections : <EOL> managed . append ( sections . pop ( ) ) <EOL> preamble = '<STR_LIT:\n>' . join ( unmanaged [ : - <NUM_LIT:1> ] ) <EOL> postamble = unmanaged [ - <NUM_LIT:1> ] <EOL> managed = managed [ - <NUM_LIT:1> ] <EOL> return preamble . strip ( ) , managed . strip ( ) , postamble . strip ( ) </s>
