<s> from collections import OrderedDict <EOL> import copy <EOL> import json <EOL> import os <EOL> import re <EOL> import sys <EOL> try : <EOL> input = raw_input <EOL> except NameError : <EOL> pass <EOL> KNOWN_COMMANDS = [ ] <EOL> def get_commands ( ) : <EOL> commands = OrderedDict ( ) <EOL> for c in KNOWN_COMMANDS : <EOL> cmd = c ( ) <EOL> commands [ c . name ] = cmd . copy ( ) <EOL> return commands <EOL> class CommandMeta ( type ) : <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> super_new = type . __new__ <EOL> parents = [ b for b in bases if isinstance ( b , CommandMeta ) ] <EOL> if not parents : <EOL> return super_new ( cls , name , bases , attrs ) <EOL> attrs [ "<STR_LIT>" ] = len ( KNOWN_COMMANDS ) <EOL> new_class = super_new ( cls , name , bases , attrs ) <EOL> KNOWN_COMMANDS . append ( new_class ) <EOL> return new_class <EOL> VALID_APPNAME = re . compile ( r'<STR_LIT>' ) <EOL> class Command ( object ) : <EOL> name = None <EOL> short_descr = None <EOL> order = <NUM_LIT:0> <EOL> def copy ( self ) : <EOL> return copy . copy ( self ) <EOL> def run ( self , config , args ) : <EOL> raise NotImplementedError <EOL> def parse_concurrency ( self , args ) : <EOL> if not args [ "<STR_LIT>" ] : <EOL> return { } <EOL> settings = { } <EOL> for setting in args [ "<STR_LIT>" ] : <EOL> kv = setting . split ( "<STR_LIT:=>" ) <EOL> if len ( kv ) == <NUM_LIT:2> : <EOL> key = kv [ <NUM_LIT:0> ] . strip ( ) <EOL> try : <EOL> val = int ( kv [ <NUM_LIT:1> ] . strip ( ) ) <EOL> except ValueError : <EOL> continue <EOL> settings [ key ] = val <EOL> return settings <EOL> def default_appname ( self , config , args ) : <EOL> if args [ '<STR_LIT>' ] : <EOL> appname = args [ '<STR_LIT>' ] <EOL> if appname == "<STR_LIT:.>" : <EOL> if config . use_procfile : <EOL> appname = config . procfile . get_appname ( ) <EOL> else : <EOL> appname = os . path . split ( os . getcwd ( ) ) [ <NUM_LIT:1> ] <EOL> elif config . procfile is not None : <EOL> appname = config . procfile . get_appname ( ) <EOL> else : <EOL> appname = "<STR_LIT:default>" <EOL> if not VALID_APPNAME . match ( appname ) : <EOL> raise ValueError ( "<STR_LIT>" % appname ) <EOL> return appname <EOL> def parse_name ( self , name , default = "<STR_LIT:default>" ) : <EOL> if "<STR_LIT:.>" in name : <EOL> appname , name = name . split ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> elif "<STR_LIT:/>" in name : <EOL> appname , name = name . split ( "<STR_LIT:/>" , <NUM_LIT:1> ) <EOL> else : <EOL> appname = default <EOL> return appname , name <EOL> def use_procfile ( self , config , appname ) : <EOL> return ( config . use_procfile and <EOL> appname == config . procfile . get_appname ( ) ) <EOL> def confirm ( self , prompt , resp = True ) : <EOL> if resp : <EOL> prompt = '<STR_LIT>' % ( prompt , '<STR_LIT:y>' , '<STR_LIT:n>' ) <EOL> else : <EOL> prompt = '<STR_LIT>' % ( prompt , '<STR_LIT:n>' , '<STR_LIT:y>' ) <EOL> while True : <EOL> ret = input ( prompt ) . lower ( ) <EOL> if not ret : <EOL> return resp <EOL> if ret not in ( '<STR_LIT:y>' , '<STR_LIT:n>' ) : <EOL> print ( '<STR_LIT>' ) <EOL> continue <EOL> if ret == "<STR_LIT:y>" : <EOL> return True <EOL> return False <EOL> def load_jsonconfig ( self , fname ) : <EOL> if fname == "<STR_LIT:->" : <EOL> content = [ ] <EOL> while True : <EOL> data = sys . stdin . readline ( ) <EOL> if not data : <EOL> break <EOL> content . append ( data ) <EOL> content = '<STR_LIT>' . join ( content ) <EOL> else : <EOL> if not os . path . isfile ( fname ) : <EOL> raise RuntimeError ( "<STR_LIT>" % fname ) <EOL> with open ( fname , '<STR_LIT:rb>' ) as f : <EOL> content = f . read ( ) <EOL> if isinstance ( content , bytes ) : <EOL> content = content . decode ( '<STR_LIT:utf-8>' ) <EOL> obj = json . loads ( content ) <EOL> if "<STR_LIT>" in obj : <EOL> configs = obj [ '<STR_LIT>' ] <EOL> else : <EOL> configs = [ obj ] <EOL> return configs <EOL> Command = CommandMeta ( '<STR_LIT>' , ( Command , ) , { } ) </s>
