<s> import logging <EOL> import os <EOL> import runpy <EOL> import shutil <EOL> import sys <EOL> import textwrap <EOL> import argparse <EOL> import config <EOL> from path import path <EOL> import utils <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> GLOBAL_CONFIG_FILE = '<STR_LIT>' <EOL> else : <EOL> GLOBAL_CONFIG_FILE = '<STR_LIT>' <EOL> MISSING_GAE_SDK_MSG = "<STR_LIT>" "<STR_LIT>" <EOL> class Action ( object ) : <EOL> """<STR_LIT>""" <EOL> name = None <EOL> description = None <EOL> epilog = None <EOL> def __init__ ( self , manager , name ) : <EOL> self . manager = manager <EOL> self . name = name <EOL> self . logger = logging . getLogger ( name ) <EOL> def __call__ ( self , argv ) : <EOL> raise NotImplementedError ( ) <EOL> def get_config_section ( self ) : <EOL> sections = [ self . manager . app , '<STR_LIT>' ] <EOL> return [ '<STR_LIT>' % ( s , self . name ) for s in sections if s ] <EOL> def error ( self , message , status = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . logger . error ( message ) <EOL> sys . exit ( status ) <EOL> def run_hooks ( self , import_names , args ) : <EOL> """<STR_LIT>""" <EOL> hooks = [ ] <EOL> for import_name in import_names : <EOL> hook = utils . import_string ( import_name , True ) <EOL> if hook is None : <EOL> self . error ( '<STR_LIT>' % import_name ) <EOL> hooks . append ( hook ) <EOL> for hook in hooks : <EOL> hook ( self . manager , args ) <EOL> class CreateAppAction ( Action ) : <EOL> """<STR_LIT>""" <EOL> description = '<STR_LIT>' <EOL> def get_parser ( self ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = self . description <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> nargs = '<STR_LIT:+>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> return parser <EOL> def __call__ ( self , argv ) : <EOL> manager = self . manager <EOL> section = self . get_config_section ( ) <EOL> parser = self . get_parser ( ) <EOL> args = parser . parse_args ( args = argv ) <EOL> template_dir = args . template <EOL> if not template_dir : <EOL> template_dir = manager . config . get ( section , '<STR_LIT>' ) <EOL> if not template_dir : <EOL> curr_dir = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> template_dir = os . path . join ( curr_dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> template_dir = os . path . abspath ( template_dir ) <EOL> if not os . path . exists ( template_dir ) : <EOL> self . error ( '<STR_LIT>' % template_dir ) <EOL> for app_dir in args . app_dir : <EOL> app_dir = os . path . abspath ( app_dir ) <EOL> self . create_app ( app_dir , template_dir ) <EOL> def create_app ( self , app_dir , template_dir ) : <EOL> if os . path . exists ( app_dir ) : <EOL> self . error ( '<STR_LIT>' % app_dir ) <EOL> shutil . copytree ( template_dir , app_dir ) <EOL> class GaeSdkAction ( Action ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , argv ) : <EOL> sys . argv = [ self . name ] + argv <EOL> try : <EOL> runpy . run_module ( self . name , run_name = '<STR_LIT:__main__>' , alter_sys = True ) <EOL> except ImportError : <EOL> self . error ( MISSING_GAE_SDK_MSG % dict ( script = self . name ) ) <EOL> class GaeSdkExtendedAction ( Action ) : <EOL> """<STR_LIT>""" <EOL> options = [ ] <EOL> def get_base_gae_argv ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def get_getopt_options ( self ) : <EOL> for option in self . options : <EOL> if isinstance ( option , tuple ) : <EOL> long_option , short_option = option <EOL> else : <EOL> long_option = option <EOL> short_option = None <EOL> is_bool = not long_option . endswith ( '<STR_LIT:=>' ) <EOL> long_option = long_option . strip ( '<STR_LIT:=>' ) <EOL> yield long_option , short_option , is_bool <EOL> def get_parser_from_getopt_options ( self ) : <EOL> manager = self . manager <EOL> section = self . get_config_section ( ) <EOL> usage = '<STR_LIT>' '<STR_LIT>' % dict ( action = self . name ) <EOL> parser = argparse . ArgumentParser ( <EOL> description = self . description , <EOL> usage = usage , <EOL> formatter_class = argparse . RawDescriptionHelpFormatter , <EOL> add_help = False <EOL> ) <EOL> for long_option , short_option , is_bool in self . get_getopt_options ( ) : <EOL> args = [ '<STR_LIT>' % long_option ] <EOL> kwargs = { } <EOL> if short_option : <EOL> args . append ( '<STR_LIT>' % short_option ) <EOL> if is_bool : <EOL> kwargs [ '<STR_LIT:action>' ] = '<STR_LIT:store_true>' <EOL> kwargs [ '<STR_LIT:default>' ] = manager . config . getboolean ( section , <EOL> long_option ) <EOL> else : <EOL> kwargs [ '<STR_LIT:default>' ] = manager . config . get ( section , long_option ) <EOL> parser . add_argument ( * args , ** kwargs ) <EOL> app_path = manager . config . get ( section , '<STR_LIT:path>' , '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = app_path ) <EOL> return parser <EOL> def get_gae_argv ( self , argv ) : <EOL> manager = self . manager <EOL> parser = self . get_parser_from_getopt_options ( ) <EOL> args , extras = parser . parse_known_args ( args = argv ) <EOL> if args . help : <EOL> parser . print_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> gae_argv = self . get_base_gae_argv ( ) <EOL> for long_option , short_option , is_bool in self . get_getopt_options ( ) : <EOL> value = getattr ( args , long_option ) <EOL> if value is not None : <EOL> if is_bool and value : <EOL> value = '<STR_LIT>' % long_option <EOL> elif not is_bool : <EOL> value = '<STR_LIT>' % ( long_option , value ) <EOL> if value : <EOL> gae_argv . append ( value ) <EOL> gae_argv . append ( os . path . abspath ( args . app ) ) <EOL> return gae_argv <EOL> class GaeRunserverAction ( GaeSdkExtendedAction ) : <EOL> """<STR_LIT>""" <EOL> description = textwrap . dedent ( __doc__ ) <EOL> options = [ <EOL> ( '<STR_LIT>' , '<STR_LIT:a>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT:c>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT:d>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT:h>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT:p>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def get_base_gae_argv ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> def __call__ ( self , argv ) : <EOL> manager = self . manager <EOL> section = self . get_config_section ( ) <EOL> before_hooks = manager . config . getlist ( section , '<STR_LIT>' , [ ] ) <EOL> after_hooks = manager . config . getlist ( section , '<STR_LIT>' , [ ] ) <EOL> sys . argv = self . get_gae_argv ( argv ) <EOL> self . run_hooks ( before_hooks , argv ) <EOL> script = '<STR_LIT>' <EOL> try : <EOL> self . logger . info ( '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( sys . argv ) ) <EOL> runpy . run_module ( script , run_name = '<STR_LIT:__main__>' , alter_sys = True ) <EOL> except ImportError : <EOL> self . error ( MISSING_GAE_SDK_MSG % dict ( script = script ) ) <EOL> finally : <EOL> self . run_hooks ( after_hooks , argv ) <EOL> class GaeDeployAction ( GaeSdkExtendedAction ) : <EOL> """<STR_LIT>""" <EOL> description = textwrap . dedent ( __doc__ ) <EOL> options = [ <EOL> ( '<STR_LIT>' , '<STR_LIT:h>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:q>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:v>' ) , <EOL> '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT:s>' ) , <EOL> '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT:e>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:H>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT:A>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:S>' ) , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def get_base_gae_argv ( self ) : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __call__ ( self , argv ) : <EOL> manager = self . manager <EOL> section = self . get_config_section ( ) <EOL> before_hooks = manager . config . getlist ( section , '<STR_LIT>' , [ ] ) <EOL> after_hooks = manager . config . getlist ( section , '<STR_LIT>' , [ ] ) <EOL> sys . argv = self . get_gae_argv ( argv ) <EOL> self . run_hooks ( before_hooks , argv ) <EOL> script = '<STR_LIT>' <EOL> try : <EOL> self . logger . info ( '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( sys . argv ) ) <EOL> runpy . run_module ( script , run_name = '<STR_LIT:__main__>' , alter_sys = True ) <EOL> except ImportError : <EOL> self . error ( MISSING_GAE_SDK_MSG % dict ( script = script ) ) <EOL> finally : <EOL> self . run_hooks ( after_hooks , argv ) <EOL> class BuildAction ( Action ) : <EOL> description = '<STR_LIT>' <EOL> cache_path = '<STR_LIT>' <EOL> pin_file = '<STR_LIT>' <EOL> def get_parser ( self ) : <EOL> manager = self . manager <EOL> section = self . get_config_section ( ) <EOL> parser = argparse . ArgumentParser ( <EOL> description = self . description <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = manager . config . get ( section , '<STR_LIT>' ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = manager . config . get ( section , '<STR_LIT>' , self . pin_file ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = manager . config . getboolean ( section , '<STR_LIT>' , False ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = manager . config . get ( section , '<STR_LIT>' , self . cache_path ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = manager . config . getboolean ( section , '<STR_LIT>' , False ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = manager . config . getboolean ( section , '<STR_LIT>' , False ) <EOL> ) <EOL> return parser <EOL> def __call__ ( self , argv ) : <EOL> manager = self . manager <EOL> if not manager . app : <EOL> self . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser = self . get_parser ( ) <EOL> args = parser . parse_args ( args = argv ) <EOL> if args . from_pin_file : <EOL> packages_to_install = self . read_pin_file ( args . from_pin_file ) <EOL> else : <EOL> packages_to_install = manager . config . getlist ( section , '<STR_LIT>' , <EOL> [ ] ) <EOL> if not packages_to_install : <EOL> self . error ( '<STR_LIT>' ) <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> args . no_symlink = True <EOL> packages = [ ] <EOL> if not args . no_pin_file : <EOL> pin_file = args . pin_file % dict ( app = manager . app ) <EOL> self . save_pin_file ( pin_file , packages ) <EOL> def save_pin_file ( self , pin_file , packages ) : <EOL> f = open ( pin_file , '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT:\n>' . join ( packages ) ) <EOL> f . close ( ) <EOL> def read_pin_file ( self , pin_file ) : <EOL> f = open ( pin_file , '<STR_LIT:r>' ) <EOL> contents = f . read ( ) <EOL> f . close ( ) <EOL> packages = [ line . strip ( ) for line in contents . splitlines ( ) ] <EOL> return [ line for line in packages if line ] <EOL> def _get_package_finder ( self ) : <EOL> from pip . index import PackageFinder <EOL> find_links = [ ] <EOL> use_mirrors = False <EOL> mirrors = [ ] <EOL> index_urls = [ '<STR_LIT>' ] <EOL> return PackageFinder ( find_links = find_links , index_urls = index_urls , <EOL> use_mirrors = use_mirrors , mirrors = mirrors ) <EOL> class InstallAppengineSdkAction ( Action ) : <EOL> """<STR_LIT>""" <EOL> description = '<STR_LIT>' <EOL> def get_parser ( self ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = self . description <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> return parser <EOL> def __call__ ( self , argv ) : <EOL> manager = self . manager <EOL> parser = self . get_parser ( ) <EOL> raise NotImplementedError ( ) <EOL> class TestAction ( Action ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , argv ) : <EOL> manager = self . manager <EOL> print manager . app <EOL> class TipfyManager ( object ) : <EOL> description = '<STR_LIT>' <EOL> epilog = '<STR_LIT>' <EOL> actions = { <EOL> '<STR_LIT>' : GaeSdkAction , <EOL> '<STR_LIT>' : GaeSdkAction , <EOL> '<STR_LIT>' : GaeSdkAction , <EOL> '<STR_LIT>' : GaeSdkAction , <EOL> '<STR_LIT>' : GaeSdkAction , <EOL> '<STR_LIT>' : GaeRunserverAction , <EOL> '<STR_LIT>' : GaeDeployAction , <EOL> '<STR_LIT>' : CreateAppAction , <EOL> '<STR_LIT>' : BuildAction , <EOL> '<STR_LIT:test>' : TestAction , <EOL> } <EOL> def __init__ ( self ) : <EOL> pass <EOL> def __call__ ( self , argv ) : <EOL> parser = self . get_parser ( ) <EOL> args , extras = parser . parse_known_args ( args = argv ) <EOL> self . parse_config ( args . config ) <EOL> if args . action not in self . actions : <EOL> return parser . print_help ( ) <EOL> self . app = args . app or self . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . config_section = [ '<STR_LIT>' ] <EOL> if self . app : <EOL> self . config_section . insert ( <NUM_LIT:0> , self . app ) <EOL> if self . app : <EOL> self . config . set ( '<STR_LIT>' , '<STR_LIT>' , self . app ) <EOL> sys . path [ : <NUM_LIT:0> ] = self . config . getlist ( self . config_section , '<STR_LIT>' , [ ] ) <EOL> self . cwd_path = path . getcwd ( ) <EOL> if self . app : <EOL> default_app_path = self . cwd_path . joinpath ( self . app ) <EOL> self . app_path = path ( self . config . get ( self . config_section , '<STR_LIT:path>' , <EOL> default_app_path ) ) . abspath ( ) <EOL> else : <EOL> self . app_path = None <EOL> if args . help : <EOL> extras . append ( '<STR_LIT>' ) <EOL> action = self . actions [ args . action ] ( self , args . action ) <EOL> return action ( extras ) <EOL> def get_parser ( self ) : <EOL> actions = '<STR_LIT:U+002CU+0020>' . join ( sorted ( self . actions . keys ( ) ) ) <EOL> parser = argparse . ArgumentParser ( <EOL> description = self . description , <EOL> epilog = self . epilog , <EOL> add_help = False <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:action>' , <EOL> help = '<STR_LIT>' % actions , <EOL> nargs = '<STR_LIT:?>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' <EOL> ) <EOL> return parser <EOL> def parse_config ( self , config_file ) : <EOL> """<STR_LIT>""" <EOL> self . config_files = [ <EOL> path ( '<STR_LIT>' ) . expanduser ( ) . joinpath ( GLOBAL_CONFIG_FILE ) . realpath ( ) , <EOL> path ( config_file ) . abspath ( ) . realpath ( ) , <EOL> ] <EOL> self . config = config . Config ( ) <EOL> self . config_loaded = self . config . read ( self . config_files ) <EOL> def main ( ) : <EOL> manager = TipfyManager ( ) <EOL> manager ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
