<s> from datetime import datetime <EOL> from distutils import spawn <EOL> import argparse <EOL> import json <EOL> import os <EOL> import platform <EOL> import shutil <EOL> import socket <EOL> import sys <EOL> import urllib <EOL> import urllib2 <EOL> import main <EOL> from main import config <EOL> PARSER = argparse . ArgumentParser ( ) <EOL> PARSER . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> PARSER . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:start>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> PARSER . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:host>' , action = '<STR_LIT:store>' , default = '<STR_LIT:127.0.0.1>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> PARSER . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:port>' , action = '<STR_LIT:store>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> PARSER . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT:args>' , nargs = argparse . REMAINDER , default = [ ] , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> PARSER . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> PARSER . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '''<STR_LIT>''' , <EOL> ) <EOL> PARSER . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '''<STR_LIT>''' , <EOL> ) <EOL> PARSER . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> ARGS = PARSER . parse_args ( ) <EOL> BAD_ENDINGS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> GAE_PATH = '<STR_LIT>' <EOL> IS_WINDOWS = platform . system ( ) == '<STR_LIT>' <EOL> DIR_MAIN = '<STR_LIT>' <EOL> DIR_TEMP = '<STR_LIT>' <EOL> DIR_VENV = os . path . join ( DIR_TEMP , '<STR_LIT>' ) <EOL> DIR_LIB = os . path . join ( DIR_MAIN , '<STR_LIT>' ) <EOL> DIR_LIBX = os . path . join ( DIR_MAIN , '<STR_LIT>' ) <EOL> FILE_LIB = '<STR_LIT>' % DIR_LIB <EOL> FILE_REQUIREMENTS = '<STR_LIT>' <EOL> FILE_PIP_GUARD = os . path . join ( DIR_TEMP , '<STR_LIT>' ) <EOL> FILE_VENV = os . path . join ( DIR_VENV , '<STR_LIT>' , '<STR_LIT>' ) if IS_WINDOWS else os . path . join ( DIR_VENV , '<STR_LIT>' , '<STR_LIT>' ) <EOL> DIR_STORAGE = os . path . join ( DIR_TEMP , '<STR_LIT>' ) <EOL> FILE_UPDATE = os . path . join ( DIR_TEMP , '<STR_LIT>' ) <EOL> DIR_TRANSLATIONS = os . path . join ( DIR_MAIN , '<STR_LIT>' ) <EOL> FILE_BABEL_CFG = os . path . join ( DIR_TRANSLATIONS , '<STR_LIT>' ) <EOL> FILE_MESSAGES_POT = os . path . join ( DIR_TRANSLATIONS , '<STR_LIT>' ) <EOL> CORE_VERSION_URL = '<STR_LIT>' <EOL> INTERNET_TEST_URL = '<STR_LIT>' <EOL> REQUIREMENTS_URL = '<STR_LIT>' <EOL> def print_out ( script , filename = '<STR_LIT>' ) : <EOL> timestamp = datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> if not filename : <EOL> filename = '<STR_LIT:->' * <NUM_LIT> <EOL> script = script . rjust ( <NUM_LIT:12> , '<STR_LIT:->' ) <EOL> print '<STR_LIT>' % ( timestamp , script , filename ) <EOL> def make_dirs ( directory ) : <EOL> if not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> def listdir ( directory , split_ext = False ) : <EOL> try : <EOL> if split_ext : <EOL> return [ os . path . splitext ( dir_ ) [ <NUM_LIT:0> ] for dir_ in os . listdir ( directory ) ] <EOL> else : <EOL> return os . listdir ( directory ) <EOL> except OSError : <EOL> return [ ] <EOL> def site_packages_path ( ) : <EOL> if IS_WINDOWS : <EOL> return os . path . join ( DIR_VENV , '<STR_LIT>' , '<STR_LIT>' ) <EOL> py_version = '<STR_LIT>' % sys . version_info [ : <NUM_LIT:2> ] <EOL> return os . path . join ( DIR_VENV , '<STR_LIT>' , py_version , '<STR_LIT>' ) <EOL> def create_virtualenv ( ) : <EOL> if not os . path . exists ( FILE_VENV ) : <EOL> os . system ( '<STR_LIT>' % DIR_VENV ) <EOL> os . system ( '<STR_LIT>' % ( <EOL> '<STR_LIT>' if IS_WINDOWS else '<STR_LIT>' , FILE_VENV <EOL> ) ) <EOL> pth_file = os . path . join ( site_packages_path ( ) , '<STR_LIT>' ) <EOL> echo_to = '<STR_LIT>' . format ( pth = pth_file ) <EOL> os . system ( echo_to % find_gae_path ( ) ) <EOL> os . system ( echo_to % os . path . abspath ( DIR_LIBX ) ) <EOL> fix_path_cmd = '<STR_LIT>' <EOL> os . system ( echo_to % ( <EOL> fix_path_cmd if IS_WINDOWS else '<STR_LIT>' % fix_path_cmd <EOL> ) ) <EOL> return True <EOL> def exec_pip_commands ( command ) : <EOL> script = [ ] <EOL> if create_virtualenv ( ) : <EOL> activate_cmd = '<STR_LIT>' if IS_WINDOWS else '<STR_LIT>' <EOL> activate_cmd %= FILE_VENV <EOL> script . append ( activate_cmd ) <EOL> script . append ( '<STR_LIT>' % command ) <EOL> script . append ( '<STR_LIT>' % <EOL> ( '<STR_LIT>' if IS_WINDOWS else '<STR_LIT>' ) ) <EOL> script . append ( command ) <EOL> script = '<STR_LIT:&>' . join ( script ) if IS_WINDOWS else '<STR_LIT>' % '<STR_LIT:;>' . join ( script ) <EOL> os . system ( script ) <EOL> def make_guard ( fname , cmd , spec ) : <EOL> with open ( fname , '<STR_LIT:w>' ) as guard : <EOL> guard . write ( '<STR_LIT>' % ( cmd , spec ) ) <EOL> def guard_is_newer ( guard , watched ) : <EOL> if os . path . exists ( guard ) : <EOL> return os . path . getmtime ( guard ) > os . path . getmtime ( watched ) <EOL> return False <EOL> def check_if_pip_should_run ( ) : <EOL> return not guard_is_newer ( FILE_PIP_GUARD , FILE_REQUIREMENTS ) <EOL> def install_py_libs ( ) : <EOL> if not check_if_pip_should_run ( ) and os . path . exists ( DIR_LIB ) : <EOL> return <EOL> exec_pip_commands ( '<STR_LIT>' % FILE_REQUIREMENTS ) <EOL> exclude_ext = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> exclude_prefix = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> exclude = [ <EOL> '<STR_LIT:test>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> def _exclude_prefix ( pkg ) : <EOL> for prefix in exclude_prefix : <EOL> if pkg . startswith ( prefix ) : <EOL> return True <EOL> return False <EOL> def _exclude_ext ( pkg ) : <EOL> for ext in exclude_ext : <EOL> if pkg . endswith ( ext ) : <EOL> return True <EOL> return False <EOL> def _get_dest ( pkg ) : <EOL> make_dirs ( DIR_LIB ) <EOL> return os . path . join ( DIR_LIB , pkg ) <EOL> site_packages = site_packages_path ( ) <EOL> dir_libs = listdir ( DIR_LIB ) <EOL> dir_libs . extend ( listdir ( DIR_LIBX ) ) <EOL> for dir_ in listdir ( site_packages ) : <EOL> if dir_ in dir_libs or dir_ in exclude : <EOL> continue <EOL> if _exclude_prefix ( dir_ ) or _exclude_ext ( dir_ ) : <EOL> continue <EOL> src_path = os . path . join ( site_packages , dir_ ) <EOL> copy = shutil . copy if os . path . isfile ( src_path ) else shutil . copytree <EOL> copy ( src_path , _get_dest ( dir_ ) ) <EOL> make_guard ( FILE_PIP_GUARD , '<STR_LIT>' , FILE_REQUIREMENTS ) <EOL> def install_dependencies ( ) : <EOL> make_dirs ( DIR_TEMP ) <EOL> install_py_libs ( ) <EOL> def check_for_update ( ) : <EOL> if os . path . exists ( FILE_UPDATE ) : <EOL> mtime = os . path . getmtime ( FILE_UPDATE ) <EOL> last = datetime . utcfromtimestamp ( mtime ) . strftime ( '<STR_LIT>' ) <EOL> today = datetime . utcnow ( ) . strftime ( '<STR_LIT>' ) <EOL> if last == today : <EOL> return <EOL> try : <EOL> with open ( FILE_UPDATE , '<STR_LIT:a>' ) : <EOL> os . utime ( FILE_UPDATE , None ) <EOL> request = urllib2 . Request ( <EOL> CORE_VERSION_URL , <EOL> urllib . urlencode ( { '<STR_LIT:version>' : main . __version__ } ) , <EOL> ) <EOL> response = urllib2 . urlopen ( request ) <EOL> with open ( FILE_UPDATE , '<STR_LIT:w>' ) as update_json : <EOL> update_json . write ( response . read ( ) ) <EOL> except ( urllib2 . HTTPError , urllib2 . URLError ) : <EOL> pass <EOL> def print_out_update ( force_show = False ) : <EOL> try : <EOL> import pip <EOL> SemVer = pip . util . version . SemanticVersion <EOL> except AttributeError : <EOL> import pip . _vendor . distlib . version <EOL> SemVer = pip . _vendor . distlib . version . SemanticVersion <EOL> try : <EOL> with open ( FILE_UPDATE , '<STR_LIT:r>' ) as update_json : <EOL> data = json . load ( update_json ) <EOL> if SemVer ( main . __version__ ) < SemVer ( data [ '<STR_LIT:version>' ] ) or force_show : <EOL> print_out ( '<STR_LIT>' ) <EOL> print_out ( data [ '<STR_LIT:version>' ] , '<STR_LIT>' ) <EOL> print_out ( main . __version__ , '<STR_LIT>' ) <EOL> print_out ( '<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> except ( ValueError , KeyError ) : <EOL> os . remove ( FILE_UPDATE ) <EOL> except IOError : <EOL> pass <EOL> def internet_on ( ) : <EOL> try : <EOL> urllib2 . urlopen ( INTERNET_TEST_URL , timeout = <NUM_LIT:2> ) <EOL> return True <EOL> except ( urllib2 . URLError , socket . timeout ) : <EOL> return False <EOL> def check_requirement ( check_func ) : <EOL> result , name , help_url_id = check_func ( ) <EOL> if not result : <EOL> print_out ( '<STR_LIT>' , name ) <EOL> if help_url_id : <EOL> print '<STR_LIT>' % ( REQUIREMENTS_URL , help_url_id ) <EOL> return False <EOL> return True <EOL> def find_gae_path ( ) : <EOL> global GAE_PATH <EOL> if GAE_PATH : <EOL> return GAE_PATH <EOL> if IS_WINDOWS : <EOL> gae_path = None <EOL> for path in os . environ [ '<STR_LIT>' ] . split ( os . pathsep ) : <EOL> if os . path . isfile ( os . path . join ( path , '<STR_LIT>' ) ) : <EOL> gae_path = path <EOL> else : <EOL> gae_path = spawn . find_executable ( '<STR_LIT>' ) <EOL> if gae_path : <EOL> gae_path = os . path . dirname ( os . path . realpath ( gae_path ) ) <EOL> if not gae_path : <EOL> return '<STR_LIT>' <EOL> gcloud_exec = '<STR_LIT>' if IS_WINDOWS else '<STR_LIT>' <EOL> if not os . path . isfile ( os . path . join ( gae_path , gcloud_exec ) ) : <EOL> GAE_PATH = gae_path <EOL> else : <EOL> gae_path = os . path . join ( gae_path , '<STR_LIT:..>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if os . path . exists ( gae_path ) : <EOL> GAE_PATH = os . path . realpath ( gae_path ) <EOL> return GAE_PATH <EOL> def check_internet ( ) : <EOL> return internet_on ( ) , '<STR_LIT>' , '<STR_LIT>' <EOL> def check_gae ( ) : <EOL> return bool ( find_gae_path ( ) ) , '<STR_LIT>' , '<STR_LIT>' <EOL> def check_git ( ) : <EOL> return bool ( spawn . find_executable ( '<STR_LIT>' ) ) , '<STR_LIT>' , '<STR_LIT>' <EOL> def check_nodejs ( ) : <EOL> return bool ( spawn . find_executable ( '<STR_LIT>' ) ) , '<STR_LIT>' , '<STR_LIT>' <EOL> def check_pip ( ) : <EOL> return bool ( spawn . find_executable ( '<STR_LIT>' ) ) , '<STR_LIT>' , '<STR_LIT>' <EOL> def check_virtualenv ( ) : <EOL> return bool ( spawn . find_executable ( '<STR_LIT>' ) ) , '<STR_LIT>' , '<STR_LIT>' <EOL> def doctor_says_ok ( ) : <EOL> checkers = [ check_gae , check_git , check_nodejs , check_pip , check_virtualenv ] <EOL> if False in [ check_requirement ( check ) for check in checkers ] : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return check_requirement ( check_internet ) <EOL> def pybabel_extract ( ) : <EOL> exec_pip_commands ( <EOL> '<STR_LIT>' % ( <EOL> FILE_BABEL_CFG , FILE_MESSAGES_POT , DIR_MAIN , <EOL> ) ) <EOL> def pybabel_update ( ) : <EOL> exec_pip_commands ( '<STR_LIT>' % ( <EOL> FILE_MESSAGES_POT , DIR_TRANSLATIONS , <EOL> ) ) <EOL> def pybabel_init ( locale ) : <EOL> exec_pip_commands ( '<STR_LIT>' % ( <EOL> FILE_MESSAGES_POT , DIR_TRANSLATIONS , locale , <EOL> ) ) <EOL> def pybabel_init_missing ( ) : <EOL> if not os . path . exists ( FILE_MESSAGES_POT ) : <EOL> pybabel_extract ( ) <EOL> for locale in config . LOCALE : <EOL> msg = os . path . join ( DIR_TRANSLATIONS , locale , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not os . path . exists ( msg ) : <EOL> pybabel_init ( locale ) <EOL> def pybabel_compile ( ) : <EOL> exec_pip_commands ( '<STR_LIT>' % ( DIR_TRANSLATIONS ) ) <EOL> def run_start ( ) : <EOL> port = int ( ARGS . port ) <EOL> run_command = '<STR_LIT:U+0020>' . join ( map ( str , [ <EOL> '<STR_LIT>' , <EOL> DIR_MAIN , <EOL> '<STR_LIT>' % ARGS . host , <EOL> '<STR_LIT>' % port , <EOL> '<STR_LIT>' % ( port + <NUM_LIT:1> ) , <EOL> '<STR_LIT>' % DIR_STORAGE , <EOL> '<STR_LIT>' , <EOL> ] + ARGS . args ) ) <EOL> os . system ( run_command ) <EOL> def run ( ) : <EOL> if len ( sys . argv ) == <NUM_LIT:1> or ( ARGS . args and not ARGS . start ) : <EOL> PARSER . print_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> os . chdir ( os . path . dirname ( os . path . realpath ( __file__ ) ) ) <EOL> if doctor_says_ok ( ) : <EOL> install_dependencies ( ) <EOL> check_for_update ( ) <EOL> if ARGS . pybabel_init : <EOL> pybabel_init_missing ( ) <EOL> if ARGS . pybabel_update : <EOL> pybabel_extract ( ) <EOL> pybabel_init_missing ( ) <EOL> pybabel_update ( ) <EOL> pybabel_compile ( ) <EOL> if ARGS . pybabel_locale : <EOL> pybabel_init ( ARGS . pybabel_locale ) <EOL> if ARGS . show_version : <EOL> print_out_update ( force_show = True ) <EOL> else : <EOL> print_out_update ( ) <EOL> if ARGS . start : <EOL> run_start ( ) <EOL> if ARGS . install_dependencies : <EOL> install_dependencies ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run ( ) </s>
