<s> import argparse <EOL> import collections <EOL> import os <EOL> import sys <EOL> from spinnaker . run import check_run_and_monitor <EOL> from spinnaker . run import check_run_quick <EOL> from spinnaker . run import run_and_monitor <EOL> from spinnaker . run import run_quick <EOL> def get_repository_dir ( name ) : <EOL> """<STR_LIT>""" <EOL> if name == '<STR_LIT>' : <EOL> return os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> else : <EOL> return name <EOL> class SourceRepository ( <EOL> collections . namedtuple ( '<STR_LIT>' , [ '<STR_LIT:name>' , '<STR_LIT>' ] ) ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Refresher ( object ) : <EOL> """<STR_LIT>""" <EOL> __OPTIONAL_REPOSITORIES = [ SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> __REQUIRED_REPOSITORIES = [ <EOL> SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> SourceRepository ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> @ property <EOL> def pull_branch ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __options . pull_origin : <EOL> if ( self . __options . pull_branch <EOL> and self . __options . pull_branch != '<STR_LIT>' ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> . format ( branch = self . __options . pull_branch ) ) <EOL> return '<STR_LIT>' <EOL> return self . __options . pull_branch <EOL> @ property <EOL> def push_branch ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __options . push_master : <EOL> if ( self . __options . push_branch <EOL> and self . __options . push_branch != '<STR_LIT>' ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> . format ( branch = self . __options . push_branch ) ) <EOL> return '<STR_LIT>' <EOL> return self . __options . push_branch <EOL> def __init__ ( self , options ) : <EOL> self . __options = options <EOL> self . __extra_repositories = self . __OPTIONAL_REPOSITORIES <EOL> if options . extra_repos : <EOL> for extra in options . extra_repos . split ( '<STR_LIT:U+002C>' ) : <EOL> pair = extra . split ( '<STR_LIT:=>' ) <EOL> if len ( pair ) != <NUM_LIT:2> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' . format ( extra = extra ) ) <EOL> self . __extra_repositories . append ( SourceRepository ( pair [ <NUM_LIT:0> ] , pair [ <NUM_LIT:1> ] ) ) <EOL> def get_remote_repository_url ( self , path , which = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> result = run_quick ( '<STR_LIT>' <EOL> . format ( path = path , which = which ) , <EOL> echo = False ) <EOL> if result . returncode : <EOL> return None <EOL> return result . stdout . strip ( ) <EOL> def get_local_branch_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> result = run_quick ( '<STR_LIT>' <EOL> . format ( dir = get_repository_dir ( name ) ) , <EOL> echo = False ) <EOL> if result . returncode : <EOL> error = '<STR_LIT>' + result . stdout . strip ( ) <EOL> raise RuntimeError ( error ) <EOL> return result . stdout . strip ( ) <EOL> def get_github_repository_url ( self , repository , owner = None ) : <EOL> """<STR_LIT>""" <EOL> user = owner or self . __options . github_user <EOL> if not user : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if user == '<STR_LIT:default>' or user == '<STR_LIT>' : <EOL> user = repository . owner <EOL> url_pattern = ( '<STR_LIT>' <EOL> if self . __options . use_https <EOL> else '<STR_LIT>' ) <EOL> return url_pattern . format ( user = user , name = repository . name ) <EOL> def git_clone ( self , repository , owner = None ) : <EOL> """<STR_LIT>""" <EOL> name = repository . name <EOL> repository_dir = get_repository_dir ( name ) <EOL> upstream_user = repository . owner <EOL> branch = self . pull_branch or '<STR_LIT>' <EOL> origin_url = self . get_github_repository_url ( repository , owner = owner ) <EOL> upstream_url = '<STR_LIT>' . format ( <EOL> upstream_user = upstream_user , name = name ) <EOL> print '<STR_LIT>' . format ( <EOL> name = name , origin_url = origin_url , branch = branch ) <EOL> shell_result = run_and_monitor ( <EOL> '<STR_LIT>' . format ( url = origin_url , branch = branch ) , <EOL> echo = False ) <EOL> if not shell_result . returncode : <EOL> if shell_result . stdout : <EOL> print shell_result . stdout <EOL> else : <EOL> if repository in self . __extra_repositories : <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> . format ( name = name ) ) <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> return <EOL> sys . stderr . write ( shell_result . stderr or shell_result . stdout ) <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . <EOL> format ( name = name , upstream = upstream_url ) ) <EOL> raise SystemExit ( '<STR_LIT>' . format ( url = origin_url ) ) <EOL> if self . __options . add_upstream and origin_url != upstream_url : <EOL> print '<STR_LIT>' . format ( <EOL> upstream = upstream_url ) <EOL> check_run_quick ( '<STR_LIT>' <EOL> . format ( dir = repository_dir , url = upstream_url ) , <EOL> echo = False ) <EOL> if self . __options . disable_upstream_push : <EOL> which = '<STR_LIT>' if origin_url != upstream_url else '<STR_LIT>' <EOL> print '<STR_LIT>' . format ( <EOL> which = which , upstream = upstream_url ) <EOL> check_run_quick ( <EOL> '<STR_LIT>' <EOL> . format ( dir = repository_dir , which = which ) , <EOL> echo = False ) <EOL> def pull_from_origin ( self , repository ) : <EOL> """<STR_LIT>""" <EOL> name = repository . name <EOL> repository_dir = get_repository_dir ( name ) <EOL> if not os . path . exists ( repository_dir ) : <EOL> self . git_clone ( repository ) <EOL> return <EOL> print '<STR_LIT>' . format ( name = name ) <EOL> branch = self . get_local_branch_name ( name ) <EOL> if branch != self . pull_branch : <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' <EOL> . format ( name = name , branch = branch , want = self . pull_branch ) ) <EOL> try : <EOL> check_run_and_monitor ( '<STR_LIT>' <EOL> . format ( dir = repository_dir , branch = branch ) , <EOL> echo = True ) <EOL> except RuntimeError : <EOL> result = check_run_and_monitor ( '<STR_LIT>' <EOL> . format ( dir = repository_dir ) , <EOL> echo = False ) <EOL> if result . stdout . find ( '<STR_LIT>' ) >= <NUM_LIT:0> : <EOL> raise <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' <EOL> . format ( name = name , branch = branch ) ) <EOL> def pull_from_upstream_if_master ( self , repository ) : <EOL> """<STR_LIT>""" <EOL> name = repository . name <EOL> repository_dir = get_repository_dir ( name ) <EOL> if not os . path . exists ( repository_dir ) : <EOL> self . pull_from_origin ( repository ) <EOL> branch = self . get_local_branch_name ( name ) <EOL> if branch != '<STR_LIT>' : <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> . format ( name = name , branch = branch ) ) <EOL> return <EOL> print '<STR_LIT>' . format ( name = name ) <EOL> check_run_and_monitor ( '<STR_LIT>' <EOL> . format ( dir = repository_dir ) , <EOL> echo = True ) <EOL> def push_to_origin_if_target_branch ( self , repository ) : <EOL> """<STR_LIT>""" <EOL> name = repository . name <EOL> repository_dir = get_repository_dir ( name ) <EOL> if not os . path . exists ( repository_dir ) : <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> . format ( name = name ) ) <EOL> return <EOL> branch = self . get_local_branch_name ( name ) <EOL> if branch != self . push_branch : <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' <EOL> . format ( name = name , branch = branch , want = self . push_branch ) ) <EOL> return <EOL> print '<STR_LIT>' . format ( name = name ) <EOL> check_run_and_monitor ( '<STR_LIT>' . format ( <EOL> dir = repository_dir , branch = self . push_branch ) , <EOL> echo = True ) <EOL> def push_all_to_origin_if_target_branch ( self ) : <EOL> """<STR_LIT>""" <EOL> all_repos = self . __REQUIRED_REPOSITORIES + self . __extra_repositories <EOL> for repository in all_repos : <EOL> self . push_to_origin_if_target_branch ( repository ) <EOL> def pull_all_from_upstream_if_master ( self ) : <EOL> """<STR_LIT>""" <EOL> all_repos = self . __REQUIRED_REPOSITORIES + self . __extra_repositories <EOL> for repository in all_repos : <EOL> self . pull_from_upstream_if_master ( repository ) <EOL> def pull_all_from_origin ( self ) : <EOL> """<STR_LIT>""" <EOL> all_repos = self . __REQUIRED_REPOSITORIES + self . __extra_repositories <EOL> for repository in all_repos : <EOL> try : <EOL> self . pull_from_origin ( repository ) <EOL> except RuntimeError as ex : <EOL> if repository in self . __extra_repositories and not os . path . exists ( <EOL> get_repository_dir ( repository ) ) : <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( msg = ex . message , name = repository . name ) ) <EOL> else : <EOL> raise <EOL> def __determine_spring_config_location ( self ) : <EOL> root = '<STR_LIT>' . format ( <EOL> dir = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) ) <EOL> home = os . path . join ( os . environ [ '<STR_LIT>' ] + '<STR_LIT>' ) <EOL> return '<STR_LIT>' . format ( home = home , root = root ) <EOL> def write_gradle_run_script ( self , repository ) : <EOL> """<STR_LIT>""" <EOL> name = repository . name <EOL> path = '<STR_LIT>' . format ( name = name ) <EOL> with open ( path , '<STR_LIT:w>' ) as f : <EOL> f . write ( """<STR_LIT>""" . format ( name = name , <EOL> spring_location = self . __determine_spring_config_location ( ) ) ) <EOL> os . chmod ( path , <NUM_LIT:0> <NUM_LIT> ) <EOL> def write_deck_run_script ( self , repository ) : <EOL> """<STR_LIT>""" <EOL> name = repository . name <EOL> path = '<STR_LIT>' . format ( name = name ) <EOL> with open ( path , '<STR_LIT:w>' ) as f : <EOL> f . write ( """<STR_LIT>""" . format ( name = name ) ) <EOL> os . chmod ( path , <NUM_LIT:0> <NUM_LIT> ) <EOL> def update_spinnaker_run_scripts ( self ) : <EOL> """<STR_LIT>""" <EOL> for repository in self . __REQUIRED_REPOSITORIES : <EOL> name = repository . name <EOL> if not os . path . exists ( name ) : <EOL> continue <EOL> if name == '<STR_LIT>' : <EOL> self . write_deck_run_script ( repository ) <EOL> else : <EOL> self . write_gradle_run_script ( repository ) <EOL> @ classmethod <EOL> def init_extra_argument_parser ( cls , parser ) : <EOL> """<STR_LIT>""" <EOL> parser . add_argument ( '<STR_LIT>' , default = False , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> @ classmethod <EOL> def init_argument_parser ( cls , parser ) : <EOL> """<STR_LIT>""" <EOL> parser . add_argument ( '<STR_LIT>' , default = True , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = True , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = True , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = False , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> @ classmethod <EOL> def main ( cls ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> cls . init_argument_parser ( parser ) <EOL> cls . init_extra_argument_parser ( parser ) <EOL> options = parser . parse_args ( ) <EOL> refresher = cls ( options ) <EOL> in_repository_url = refresher . get_remote_repository_url ( '<STR_LIT:.>' ) <EOL> if in_repository_url : <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( url = in_repository_url ) ) <EOL> return - <NUM_LIT:1> <EOL> try : <EOL> if ( refresher . pull_branch != refresher . push_branch <EOL> and refresher . pull_branch and refresher . push_branch ) : <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' <EOL> . format ( pull = refresher . pull_branch , <EOL> push = refresher . push_branch ) ) <EOL> except Exception as ex : <EOL> sys . stderr . write ( '<STR_LIT>' . format ( ex . message ) ) <EOL> return - <NUM_LIT:1> <EOL> nothing = True <EOL> if options . pull_upstream : <EOL> nothing = False <EOL> refresher . pull_all_from_upstream_if_master ( ) <EOL> if refresher . push_branch : <EOL> nothing = False <EOL> refresher . push_all_to_origin_if_target_branch ( ) <EOL> if refresher . pull_branch : <EOL> nothing = False <EOL> refresher . pull_all_from_origin ( ) <EOL> refresher . update_spinnaker_run_scripts ( ) <EOL> if nothing : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> return <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( Refresher . main ( ) ) </s>
