<s> from __future__ import unicode_literals <EOL> import os <EOL> import paramiko <EOL> from django . utils import six <EOL> from reviewboard . ssh . client import SSHClient <EOL> from reviewboard . ssh . errors import ( BadHostKeyError , SSHAuthenticationError , <EOL> SSHError , SSHInvalidPortError ) <EOL> from reviewboard . ssh . policy import RaiseUnknownHostKeyPolicy <EOL> SSH_PORT = <NUM_LIT> <EOL> try : <EOL> import urlparse <EOL> uses_netloc = urlparse . uses_netloc <EOL> urllib_parse = urlparse . urlparse <EOL> except ImportError : <EOL> import urllib . parse <EOL> uses_netloc = urllib . parse . uses_netloc <EOL> urllib_parse = urllib . parse . urlparse <EOL> ssh_uri_schemes = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> uses_netloc . extend ( ssh_uri_schemes ) <EOL> def humanize_key ( key ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT::>' . join ( [ "<STR_LIT>" % ord ( c ) for c in key . get_fingerprint ( ) ] ) <EOL> def is_ssh_uri ( url ) : <EOL> """<STR_LIT>""" <EOL> return urllib_parse ( url ) [ <NUM_LIT:0> ] in ssh_uri_schemes <EOL> def check_host ( netloc , username = None , password = None , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> client = SSHClient ( namespace = namespace ) <EOL> client . set_missing_host_key_policy ( RaiseUnknownHostKeyPolicy ( ) ) <EOL> kwargs = { } <EOL> if '<STR_LIT::>' in netloc : <EOL> hostname , port = netloc . split ( '<STR_LIT::>' ) <EOL> try : <EOL> port = int ( port ) <EOL> except ValueError : <EOL> raise SSHInvalidPortError ( port ) <EOL> else : <EOL> hostname = netloc <EOL> port = SSH_PORT <EOL> if getattr ( settings , '<STR_LIT>' , False ) : <EOL> client . set_missing_host_key_policy ( paramiko . WarningPolicy ( ) ) <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> try : <EOL> client . connect ( hostname , port , username = username , password = password , <EOL> pkey = client . get_user_key ( ) , ** kwargs ) <EOL> except paramiko . BadHostKeyException as e : <EOL> raise BadHostKeyError ( e . hostname , e . key , e . expected_key ) <EOL> except paramiko . AuthenticationException as e : <EOL> allowed_types = getattr ( e , '<STR_LIT>' , [ ] ) <EOL> if '<STR_LIT>' in allowed_types : <EOL> key = client . get_user_key ( ) <EOL> else : <EOL> key = None <EOL> raise SSHAuthenticationError ( allowed_types = allowed_types , user_key = key ) <EOL> except paramiko . SSHException as e : <EOL> msg = six . text_type ( e ) <EOL> if msg == '<STR_LIT>' : <EOL> raise SSHAuthenticationError <EOL> else : <EOL> raise SSHError ( msg ) <EOL> def register_rbssh ( envvar ) : <EOL> """<STR_LIT>""" <EOL> envvar = envvar . encode ( '<STR_LIT:utf-8>' ) <EOL> os . putenv ( envvar , b'<STR_LIT>' ) <EOL> os . environ [ envvar ] = b'<STR_LIT>' </s>
