<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> import contextlib <EOL> import os <EOL> import shutil <EOL> import tempfile <EOL> import phlgit_push <EOL> import phlsys_git <EOL> @ contextlib . contextmanager <EOL> def lone_worker_context ( ) : <EOL> """<STR_LIT>""" <EOL> with temprepo_context ( ) as repo : <EOL> worker = Worker ( repo ) <EOL> worker . commit_new_file ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> yield worker <EOL> @ contextlib . contextmanager <EOL> def temprepo_context ( ) : <EOL> """<STR_LIT>""" <EOL> with contextlib . closing ( TempRepo ( ) ) as temp_repo : <EOL> yield temp_repo . repo <EOL> class TempRepo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( TempRepo , self ) . __init__ ( ) <EOL> self . _tmp_dir = tempfile . mkdtemp ( ) <EOL> self . _repo = phlsys_git . Repo ( self . _tmp_dir ) <EOL> self . _repo ( "<STR_LIT>" ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> shutil . rmtree ( self . _tmp_dir ) <EOL> @ property <EOL> def repo ( self ) : <EOL> return self . _repo <EOL> class Worker ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , repo ) : <EOL> super ( Worker , self ) . __init__ ( ) <EOL> self . _repo = repo <EOL> def create_new_file ( self , relative_path , contents = None ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . join ( <EOL> self . _repo . working_dir , <EOL> relative_path ) <EOL> with open ( path , '<STR_LIT:w>' ) as f : <EOL> if contents : <EOL> f . write ( contents ) <EOL> def add_new_file ( self , relative_path , contents = None ) : <EOL> """<STR_LIT>""" <EOL> self . create_new_file ( relative_path , contents ) <EOL> self . _repo ( '<STR_LIT>' , relative_path ) <EOL> def commit_new_file ( self , message , relative_path , contents = None ) : <EOL> """<STR_LIT>""" <EOL> self . add_new_file ( relative_path , contents ) <EOL> self . _repo ( '<STR_LIT>' , '<STR_LIT>' , message , '<STR_LIT>' , relative_path ) <EOL> def commit_new_file_on_new_branch ( <EOL> self , branch , message , relative_path , contents = None , base = None ) : <EOL> """<STR_LIT>""" <EOL> base_ref = base if base is not None else '<STR_LIT>' <EOL> self . _repo ( '<STR_LIT>' , '<STR_LIT>' , branch , base_ref ) <EOL> self . commit_new_file ( message , relative_path , contents ) <EOL> def append_to_file ( self , relative_path , to_append ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . join ( <EOL> self . _repo . working_dir , <EOL> relative_path ) <EOL> with open ( path , '<STR_LIT:a>' ) as f : <EOL> f . write ( to_append ) <EOL> def add_append_to_file ( self , relative_path , to_append ) : <EOL> """<STR_LIT>""" <EOL> self . append_to_file ( relative_path , to_append ) <EOL> self . _repo ( '<STR_LIT>' , relative_path ) <EOL> def commit_append_to_file ( self , message , relative_path , to_append ) : <EOL> """<STR_LIT>""" <EOL> self . add_append_to_file ( relative_path , to_append ) <EOL> self . _repo ( '<STR_LIT>' , '<STR_LIT>' , message , '<STR_LIT>' , relative_path ) <EOL> def append_to_file_on_new_branch ( <EOL> self , branch , message , relative_path , to_append , base = None ) : <EOL> """<STR_LIT>""" <EOL> base_ref = base if base is not None else '<STR_LIT>' <EOL> self . _repo ( '<STR_LIT>' , '<STR_LIT>' , branch , base_ref ) <EOL> self . commit_append_to_file ( message , relative_path , to_append ) <EOL> def checkout_master ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _repo ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ property <EOL> def repo ( self ) : <EOL> return self . _repo <EOL> class CentralisedWithWorkers ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , contributor_count ) : <EOL> super ( CentralisedWithWorkers , self ) . __init__ ( ) <EOL> if contributor_count < <NUM_LIT:1> : <EOL> raise ( <EOL> Exception ( "<STR_LIT>" . format ( <EOL> contributor_count ) ) ) <EOL> self . _central_repo = phlsys_git . Repo ( tempfile . mkdtemp ( ) ) <EOL> self . _central_repo ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . _workers = [ ] <EOL> for i in xrange ( contributor_count ) : <EOL> self . _workers . append ( <EOL> Worker ( phlsys_git . Repo ( tempfile . mkdtemp ( ) ) ) ) <EOL> self . workers [ - <NUM_LIT:1> ] . repo ( "<STR_LIT>" ) <EOL> self . workers [ - <NUM_LIT:1> ] . repo ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , self . _central_repo . working_dir ) <EOL> self . workers [ - <NUM_LIT:1> ] . repo ( "<STR_LIT>" ) <EOL> if i == <NUM_LIT:0> : <EOL> self . workers [ <NUM_LIT:0> ] . commit_new_file ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> phlgit_push . push ( self . workers [ <NUM_LIT:0> ] . repo , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> self . workers [ i ] . repo ( '<STR_LIT>' ) <EOL> self . workers [ i ] . repo ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def close ( self ) : <EOL> shutil . rmtree ( self . _central_repo . working_dir ) <EOL> for worker in self . _workers : <EOL> shutil . rmtree ( worker . repo . working_dir ) <EOL> @ property <EOL> def central_repo ( self ) : <EOL> return self . _central_repo <EOL> @ property <EOL> def workers ( self ) : <EOL> return self . _workers <EOL> class CentralisedWithTwoWorkers ( CentralisedWithWorkers ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( CentralisedWithTwoWorkers , self ) . __init__ ( <NUM_LIT:2> ) <EOL> @ property <EOL> def w0 ( self ) : <EOL> return self . workers [ <NUM_LIT:0> ] <EOL> @ property <EOL> def w1 ( self ) : <EOL> return self . workers [ <NUM_LIT:1> ] </s>
