<s> import locale <EOL> import os <EOL> import os . path <EOL> import re <EOL> import subprocess <EOL> import sys <EOL> import tempfile <EOL> def print_error ( message ) : <EOL> """<STR_LIT>""" <EOL> if not message . endswith ( '<STR_LIT:\n>' ) : <EOL> message += '<STR_LIT:\n>' <EOL> sys . stderr . write ( message ) <EOL> def die ( message ) : <EOL> """<STR_LIT>""" <EOL> print_error ( message ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def read_git_config ( key ) : <EOL> """<STR_LIT>""" <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> cmd . append ( key ) <EOL> output = subprocess . Popen ( cmd , stdout = subprocess . PIPE ) . communicate ( ) <EOL> encoding = locale . getpreferredencoding ( False ) <EOL> return output [ <NUM_LIT:0> ] . strip ( ) . decode ( encoding ) <EOL> def read_user_input ( prompt , validator_func ) : <EOL> """<STR_LIT>""" <EOL> user_text = '<STR_LIT>' <EOL> valid_input = False <EOL> while not valid_input : <EOL> sys . stdout . write ( '<STR_LIT>' % prompt ) <EOL> sys . stdout . flush ( ) <EOL> user_text = sys . stdin . readline ( ) <EOL> if not user_text : <EOL> die ( '<STR_LIT>' ) <EOL> user_text = user_text [ <NUM_LIT:0> : - <NUM_LIT:1> ] <EOL> if validator_func ( user_text ) : <EOL> valid_input = True <EOL> return user_text <EOL> def get_repository_name ( name ) : <EOL> """<STR_LIT>""" <EOL> if not name : <EOL> name = read_git_config ( '<STR_LIT>' ) <EOL> match = re . match ( r'''<STR_LIT>''' , name ) <EOL> if not match : <EOL> die ( <EOL> """<STR_LIT>""" ) <EOL> name = match . group ( <NUM_LIT:1> ) <EOL> return name <EOL> def split_repo_name ( repository ) : <EOL> """<STR_LIT>""" <EOL> nameparts = repository . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> if len ( nameparts ) == <NUM_LIT:1> : <EOL> return ( None , nameparts [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return ( nameparts [ <NUM_LIT:0> ] , nameparts [ <NUM_LIT:1> ] ) <EOL> def get_working_repo ( client , full_reponame ) : <EOL> full_reponame = get_repository_name ( full_reponame ) <EOL> username , reponame = split_repo_name ( full_reponame ) <EOL> if username : <EOL> user = client . get_user ( username ) <EOL> else : <EOL> user = client . get_user ( ) <EOL> repository = user . get_repo ( reponame ) <EOL> return repository <EOL> def get_named_user ( client , username ) : <EOL> try : <EOL> user = client . get_user ( username ) <EOL> except GithubException as e : <EOL> die ( '''<STR_LIT>''' % ( username , str ( e ) ) ) <EOL> return user <EOL> def find_executable ( executable ) : <EOL> path = os . environ [ '<STR_LIT>' ] . split ( os . pathsep ) <EOL> for subdirectory in path : <EOL> tryname = os . path . join ( subdirectory , executable ) <EOL> if os . access ( tryname , os . X_OK ) : <EOL> return tryname <EOL> return None <EOL> def choose_editor ( ) : <EOL> if '<STR_LIT>' in os . environ : <EOL> editor = os . environ [ '<STR_LIT>' ] <EOL> else : <EOL> print_error ( '<STR_LIT>' ) <EOL> editor = find_executable ( '<STR_LIT>' ) <EOL> if not editor : <EOL> die ( <EOL> """<STR_LIT>""" ) <EOL> return editor <EOL> def text_from_editor ( original_text = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> text = '<STR_LIT>' <EOL> editor_cmd = choose_editor ( ) <EOL> my_tempfile = None <EOL> try : <EOL> my_tempfile = tempfile . NamedTemporaryFile ( delete = False , mode = '<STR_LIT>' ) <EOL> if original_text : <EOL> my_tempfile . write ( original_text ) <EOL> my_tempfile . flush ( ) <EOL> my_tempfile . seek ( <NUM_LIT:0> ) <EOL> editor_status = subprocess . call ( [ editor_cmd , my_tempfile . name ] ) <EOL> if editor_status != <NUM_LIT:0> : <EOL> die ( <EOL> """<STR_LIT>""" ) <EOL> text = my_tempfile . read ( ) <EOL> finally : <EOL> if my_tempfile : <EOL> my_tempfile . close ( ) <EOL> os . unlink ( my_tempfile . name ) <EOL> return text </s>
