<s> import json <EOL> import os <EOL> import shutil <EOL> import stat <EOL> import sys <EOL> from optparse import OptionParser <EOL> GIT_HOOKS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def fail ( msg ) : <EOL> print msg <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def get_action_directory ( ) : <EOL> return os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT:action>" ) <EOL> def clean_up_dotgit ( options ) : <EOL> git_hook_path = get_git_path ( options ) <EOL> for hook in GIT_HOOKS : <EOL> git_hook_name = git_hook_rename ( hook ) <EOL> hook_path = os . path . join ( git_hook_path , git_hook_name ) <EOL> if os . path . exists ( hook_path ) : <EOL> os . remove ( hook_path ) <EOL> shutil . rmtree ( os . path . join ( git_hook_path , "<STR_LIT:action>" ) ) <EOL> def git_hook_rename ( hook ) : <EOL> return hook . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def make_executable ( file_path ) : <EOL> st = os . stat ( file_path ) <EOL> os . chmod ( file_path , st . st_mode | stat . S_IEXEC ) <EOL> def copy_git_hooks_to_dotgit ( options ) : <EOL> git_hook_path = get_git_path ( options ) <EOL> for hook in GIT_HOOKS : <EOL> git_hook_name = git_hook_rename ( hook ) <EOL> git_hook_full_path = os . path . join ( git_hook_path , git_hook_name ) <EOL> if os . path . exists ( git_hook_full_path ) : <EOL> backup_path = git_hook_full_path + "<STR_LIT>" <EOL> shutil . copy ( git_hook_full_path , backup_path ) <EOL> shutil . copy ( hook , git_hook_full_path ) <EOL> make_executable ( git_hook_full_path ) <EOL> def copy_action_dir_to_dotgit ( options ) : <EOL> git_hook_path = get_git_path ( options ) <EOL> shutil . copytree ( get_action_directory ( ) , <EOL> os . path . join ( git_hook_path , "<STR_LIT:action>" ) ) <EOL> def get_git_path ( options ) : <EOL> if os . path . exists ( options . gitroot ) : <EOL> git_hook_path = os . path . join ( <EOL> options . gitroot , "<STR_LIT>" ) <EOL> if os . path . exists ( git_hook_path ) : <EOL> return git_hook_path <EOL> else : <EOL> fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> fail ( "<STR_LIT>" ) <EOL> def find_file_root ( full_filename ) : <EOL> filename = os . path . basename ( full_filename ) <EOL> return filename [ : filename . rfind ( "<STR_LIT>" ) ] <EOL> def inject_file ( options ) : <EOL> if not os . path . exists ( options . injectlocation ) : <EOL> fail ( "<STR_LIT>" ) <EOL> if os . path . isfile ( options . injectlocation ) : <EOL> files = [ options . injectlocation ] <EOL> elif os . path . isdir ( options . injectlocation ) : <EOL> for dirname , _ , files in os . walk ( options . injectlocation ) : <EOL> if dirname == options . injectlocation : <EOL> break <EOL> files = [ os . path . join ( options . injectlocation , f ) <EOL> for f in files <EOL> if f . endswith ( "<STR_LIT>" ) ] <EOL> git_hook_path = get_git_path ( options ) <EOL> action_path = os . path . join ( git_hook_path , "<STR_LIT>" ) <EOL> action_config_filename = os . path . join ( action_path , "<STR_LIT>" ) <EOL> config = { } <EOL> for fname in files : <EOL> shutil . copy ( fname , action_path ) <EOL> with open ( action_config_filename , "<STR_LIT:r>" ) as action_config_file : <EOL> config = json . load ( action_config_file ) <EOL> for fname in files : <EOL> hook_name = find_file_root ( fname ) <EOL> if hook_name not in config [ "<STR_LIT>" ] : <EOL> config [ "<STR_LIT>" ] . append ( hook_name ) <EOL> with open ( action_config_filename , "<STR_LIT:w>" ) as action_config_file : <EOL> json . dump ( config , action_config_file ) <EOL> def check_command_line_arguments ( options ) : <EOL> mandatory = [ "<STR_LIT>" ] <EOL> for field in mandatory : <EOL> if not getattr ( options , field , False ) : <EOL> raise Exception ( "<STR_LIT>" % field ) <EOL> def get_command_line_arguments ( ) : <EOL> usage = "<STR_LIT>" <EOL> parser = OptionParser ( usage = usage ) <EOL> parser . add_option ( "<STR_LIT>" , dest = '<STR_LIT>' , <EOL> help = ( '<STR_LIT>' ) ) <EOL> parser . add_option ( "<STR_LIT>" , dest = '<STR_LIT>' , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> parser . add_option ( "<STR_LIT>" , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> default = False , help = ( '<STR_LIT>' ) ) <EOL> ( options , _ ) = parser . parse_args ( ) <EOL> return options <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> options = get_command_line_arguments ( ) <EOL> check_command_line_arguments ( options ) <EOL> if options . clean : <EOL> clean_up_dotgit ( options ) <EOL> elif options . injectlocation : <EOL> inject_file ( options ) <EOL> else : <EOL> copy_action_dir_to_dotgit ( options ) <EOL> copy_git_hooks_to_dotgit ( options ) </s>
