<s> from __future__ import print_function , unicode_literals <EOL> import difflib <EOL> import os <EOL> import six <EOL> from six . moves import input <EOL> from rbtools . commands import Command , CommandError <EOL> from rbtools . utils . console import confirm <EOL> from rbtools . utils . filesystem import CONFIG_FILE <EOL> class SetupRepo ( Command ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> author = '<STR_LIT>' <EOL> description = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % CONFIG_FILE ) <EOL> args = '<STR_LIT>' <EOL> option_list = [ <EOL> Command . server_options , <EOL> Command . perforce_options , <EOL> Command . tfs_options , <EOL> ] <EOL> def prompt_rb_repository ( self , tool_name , repository_info , api_root ) : <EOL> """<STR_LIT>""" <EOL> for repository_page in api_root . get_repositories ( ) . all_pages : <EOL> repo_paths = { } <EOL> for repository in repository_page : <EOL> if repository . tool != tool_name : <EOL> continue <EOL> repo_paths [ repository [ '<STR_LIT:path>' ] ] = repository <EOL> if '<STR_LIT>' in repository : <EOL> repo_paths [ repository [ '<STR_LIT>' ] ] = repository <EOL> closest_path = difflib . get_close_matches ( repository_info . path , <EOL> six . iterkeys ( repo_paths ) , <EOL> n = <NUM_LIT:4> , cutoff = <NUM_LIT> ) <EOL> for path in closest_path : <EOL> repo = repo_paths [ path ] <EOL> question = ( '<STR_LIT>' <EOL> % ( tool_name , repo [ '<STR_LIT:name>' ] , repo [ '<STR_LIT:path>' ] ) ) <EOL> if confirm ( question ) : <EOL> return repo <EOL> return None <EOL> def _get_output ( self , config ) : <EOL> """<STR_LIT>""" <EOL> settings = [ ] <EOL> for setting , value in config : <EOL> settings . append ( '<STR_LIT>' % ( setting , value ) ) <EOL> settings . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT:\n>' . join ( settings ) <EOL> def generate_config_file ( self , file_path , config ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with open ( file_path , '<STR_LIT:w>' ) as outfile : <EOL> output = self . _get_output ( config ) <EOL> outfile . write ( output ) <EOL> except IOError as e : <EOL> raise CommandError ( '<STR_LIT>' <EOL> % ( e . errno , e . strerror ) ) <EOL> print ( '<STR_LIT>' % file_path ) <EOL> def main ( self , * args ) : <EOL> server = self . options . server <EOL> if not server : <EOL> server = input ( '<STR_LIT>' ) <EOL> repository_info , tool = self . initialize_scm_tool ( ) <EOL> api_client , api_root = self . get_api ( server ) <EOL> self . setup_tool ( tool , api_root = api_root ) <EOL> repository_info = repository_info . find_server_repository_info ( api_root ) <EOL> selected_repo = self . prompt_rb_repository ( <EOL> tool . name , repository_info , api_root ) <EOL> if not selected_repo : <EOL> print ( '<STR_LIT>' <EOL> % ( tool . name , server , CONFIG_FILE ) ) <EOL> return <EOL> config = [ <EOL> ( '<STR_LIT>' , server ) , <EOL> ( '<STR_LIT>' , selected_repo [ '<STR_LIT:name>' ] ) <EOL> ] <EOL> try : <EOL> branch = tool . get_current_branch ( ) <EOL> config . append ( ( '<STR_LIT>' , branch ) ) <EOL> config . append ( ( '<STR_LIT>' , branch ) ) <EOL> except NotImplementedError : <EOL> pass <EOL> outfile_path = os . path . join ( os . getcwd ( ) , CONFIG_FILE ) <EOL> output = self . _get_output ( config ) <EOL> if not os . path . exists ( outfile_path ) : <EOL> question = ( '<STR_LIT>' <EOL> % ( outfile_path , output ) ) <EOL> else : <EOL> question = ( '<STR_LIT>' <EOL> % ( outfile_path , output ) ) <EOL> if not confirm ( question ) : <EOL> return <EOL> self . generate_config_file ( outfile_path , config ) </s>
