<s> """<STR_LIT>""" <EOL> import json <EOL> import os <EOL> import getpass <EOL> import uuid <EOL> from distutils . dir_util import copy_tree <EOL> import sh <EOL> from jinja2 import Environment , FileSystemLoader <EOL> from . common import utils <EOL> from . common . utils import log , CONTRACTFILE <EOL> from . common . runtime import rpc <EOL> from . common . runtime . backends import LocalBackend <EOL> from . common . runtime . runner import ServiceRunner <EOL> from . common . commands import BaseCmd , HutCmd <EOL> from . common . config import HutfileCfg , UserCfg <EOL> from . common . exceptions import ConfigError <EOL> from . import __version__ <EOL> from . toolkit_utils import * <EOL> from . builder import Service , bases , stacks , is_stack_supported , get_docker , OS_TYPE <EOL> class UserCmd ( BaseCmd ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , args ) : <EOL> super ( ) . __init__ ( args ) <EOL> self . usercfg = UserCfg ( ) <EOL> keen_client . start ( self . usercfg ) <EOL> def run ( self ) : <EOL> super ( ) . run ( ) <EOL> args = { k : v for ( k , v ) <EOL> in vars ( self . args ) . items ( ) <EOL> if k not in [ '<STR_LIT>' , '<STR_LIT>' ] } <EOL> keen_client . send ( '<STR_LIT>' , dict ( cmd = self . name , args = args ) ) <EOL> class LoginCmd ( UserCmd ) : <EOL> name = '<STR_LIT>' <EOL> description = "<STR_LIT>" <EOL> def __init__ ( self , args ) : <EOL> super ( ) . __init__ ( args ) <EOL> def run ( self ) : <EOL> import hashlib <EOL> super ( ) . run ( ) <EOL> username = input ( "<STR_LIT>" ) <EOL> password = getpass . getpass ( "<STR_LIT>" ) <EOL> r = stackhut_api_call ( '<STR_LIT>' , dict ( username = username , password = password ) ) <EOL> if r [ '<STR_LIT:success>' ] : <EOL> self . usercfg [ '<STR_LIT:username>' ] = username <EOL> self . usercfg [ '<STR_LIT>' ] = r [ '<STR_LIT>' ] <EOL> self . usercfg [ '<STR_LIT>' ] = hashlib . sha256 ( ( "<STR_LIT>" + username ) . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> self . usercfg . save ( ) <EOL> log . info ( "<STR_LIT>" . format ( username ) ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return <NUM_LIT:0> <EOL> class LogoutCmd ( UserCmd ) : <EOL> name = '<STR_LIT>' <EOL> description = "<STR_LIT>" <EOL> def __init__ ( self , args ) : <EOL> super ( ) . __init__ ( args ) <EOL> def run ( self ) : <EOL> super ( ) . run ( ) <EOL> log . info ( "<STR_LIT>" . format ( self . usercfg . get ( '<STR_LIT:email>' , '<STR_LIT>' ) ) ) <EOL> self . usercfg . wipe ( ) <EOL> return <NUM_LIT:0> <EOL> class InfoCmd ( UserCmd ) : <EOL> name = '<STR_LIT:info>' <EOL> description = "<STR_LIT>" <EOL> def __init__ ( self , args ) : <EOL> super ( ) . __init__ ( args ) <EOL> def run ( self ) : <EOL> super ( ) . run ( ) <EOL> log . info ( "<STR_LIT>" . format ( __version__ ) ) <EOL> docker = get_docker ( _exit = False , verbose = False ) <EOL> if docker is not None : <EOL> log . info ( "<STR_LIT>" . format ( docker . client . version ( ) . get ( '<STR_LIT>' ) ) ) <EOL> else : <EOL> log . info ( "<STR_LIT>" ) <EOL> for x in self . usercfg . show_keys : <EOL> log . info ( "<STR_LIT>" . format ( x , self . usercfg . get ( x ) ) ) <EOL> return <NUM_LIT:0> <EOL> class StackBuildCmd ( UserCmd ) : <EOL> """<STR_LIT>""" <EOL> visible = False <EOL> name = '<STR_LIT>' <EOL> @ staticmethod <EOL> def register ( sp ) : <EOL> sp . add_argument ( "<STR_LIT>" , '<STR_LIT>' , default = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> sp . add_argument ( "<STR_LIT>" , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> sp . add_argument ( "<STR_LIT>" , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> def __init__ ( self , args ) : <EOL> super ( ) . __init__ ( args ) <EOL> self . outdir = args . outdir <EOL> if not os . path . exists ( self . outdir ) : <EOL> os . mkdir ( self . outdir ) <EOL> def run ( self ) : <EOL> super ( ) . run ( ) <EOL> assert self . usercfg . username == '<STR_LIT>' , "<STR_LIT>" <EOL> [ b . build_push ( self . outdir , self . args . push , self . args . no_cache ) for b in bases . values ( ) ] <EOL> [ s . build_push ( b , self . outdir , self . args . push , self . args . no_cache ) <EOL> for b in bases . values ( ) <EOL> for s in stacks . values ( ) ] <EOL> log . info ( "<STR_LIT>" ) <EOL> return <NUM_LIT:0> <EOL> class InitCmd ( UserCmd ) : <EOL> name = '<STR_LIT>' <EOL> description = "<STR_LIT>" <EOL> @ staticmethod <EOL> def register ( sp ) : <EOL> sp . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" , choices = bases . keys ( ) ) <EOL> sp . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" , choices = stacks . keys ( ) ) <EOL> sp . add_argument ( "<STR_LIT>" , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> def __init__ ( self , args ) : <EOL> super ( ) . __init__ ( args ) <EOL> self . baseos = bases [ args . baseos ] <EOL> self . stack = stacks [ args . stack ] <EOL> self . task_id = uuid . uuid4 ( ) <EOL> def render_file ( self , env , fname , params ) : <EOL> rendered_template = env . get_template ( fname ) . render ( params ) <EOL> with open ( fname , '<STR_LIT:w>' ) as f : <EOL> f . write ( rendered_template ) <EOL> def run ( self ) : <EOL> super ( ) . run ( ) <EOL> if os . path . exists ( '<STR_LIT>' ) or os . path . exists ( '<STR_LIT>' ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if not is_stack_supported ( self . baseos , self . stack ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( self . baseos , self . stack ) ) <EOL> self . name = os . path . basename ( os . getcwd ( ) ) <EOL> HutfileCfg . assert_valid_name ( self . name ) <EOL> self . author = self . usercfg . username <EOL> log . info ( "<STR_LIT>" . format ( self . author , self . name ) ) <EOL> def copy_scaffold ( name ) : <EOL> dir_path = get_res_path ( os . path . join ( '<STR_LIT>' , name ) ) <EOL> copy_tree ( dir_path , '<STR_LIT:.>' ) <EOL> return os . listdir ( dir_path ) <EOL> common_files = copy_scaffold ( '<STR_LIT>' ) <EOL> stack_files = copy_scaffold ( self . stack . name ) <EOL> template_env = Environment ( loader = FileSystemLoader ( '<STR_LIT:.>' ) ) <EOL> [ self . render_file ( template_env , f , dict ( scaffold = self ) ) <EOL> for f in ( common_files + stack_files ) if os . path . exists ( f ) and os . path . isfile ( f ) ] <EOL> if not self . args . no_git : <EOL> sh . git . init ( ) <EOL> sh . git . add ( "<STR_LIT:.>" ) <EOL> sh . git . commit ( m = "<STR_LIT>" ) <EOL> sh . git . branch ( "<STR_LIT>" ) <EOL> return <NUM_LIT:0> <EOL> class HutBuildCmd ( HutCmd , UserCmd ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> description = "<STR_LIT>" <EOL> @ staticmethod <EOL> def register ( sp ) : <EOL> sp . add_argument ( "<STR_LIT>" , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> sp . add_argument ( "<STR_LIT>" , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> sp . add_argument ( "<STR_LIT>" , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> def __init__ ( self , args ) : <EOL> super ( ) . __init__ ( args ) <EOL> self . no_cache = args . full <EOL> self . force = args . force <EOL> self . dev = args . dev <EOL> def run ( self ) : <EOL> super ( ) . run ( ) <EOL> service = Service ( self . hutcfg , self . usercfg . username ) <EOL> service . build_push ( force = self . force , dev = self . dev , no_cache = self . no_cache ) <EOL> return <NUM_LIT:0> <EOL> class RemoteBuildCmd ( HutCmd ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> description = "<STR_LIT>" <EOL> visible = False <EOL> @ staticmethod <EOL> def register ( sp ) : <EOL> sp . add_argument ( "<STR_LIT>" , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> sp . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> def __init__ ( self , args ) : <EOL> super ( ) . __init__ ( args ) <EOL> self . dev = args . dev <EOL> self . author = args . author <EOL> def run ( self ) : <EOL> super ( ) . run ( ) <EOL> service = Service ( self . hutcfg , self . author ) <EOL> service . build_push ( force = True , dev = self . dev , no_cache = False , push = True ) <EOL> return <NUM_LIT:0> <EOL> class DeployCmd ( HutCmd , UserCmd ) : <EOL> name = '<STR_LIT>' <EOL> description = "<STR_LIT>" <EOL> @ staticmethod <EOL> def register ( sp ) : <EOL> sp . add_argument ( "<STR_LIT>" , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> sp . add_argument ( "<STR_LIT>" , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> sp . add_argument ( "<STR_LIT>" , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> sp . add_argument ( "<STR_LIT>" , default = False , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> def __init__ ( self , args ) : <EOL> super ( ) . __init__ ( args ) <EOL> self . no_build = args . no_build <EOL> self . force = args . force <EOL> self . local = args . local <EOL> self . dev = args . dev <EOL> def create_methods ( self ) : <EOL> contract = rpc . load_contract_file ( ) <EOL> for i in contract . interfaces . values ( ) : <EOL> for f in i . functions . values ( ) : <EOL> f . signature = rpc . render_signature ( f ) <EOL> log . debug ( "<STR_LIT>" . format ( f . full_name , f . signature ) ) <EOL> with open ( CONTRACTFILE , '<STR_LIT:r>' ) as f : <EOL> interfaces = [ x for x in json . load ( f ) if x [ '<STR_LIT:type>' ] == '<STR_LIT>' ] <EOL> for i in interfaces : <EOL> for f in i [ '<STR_LIT>' ] : <EOL> f [ '<STR_LIT>' ] = contract . interface ( i [ '<STR_LIT:name>' ] ) . function ( f [ '<STR_LIT:name>' ] ) . signature <EOL> return interfaces <EOL> def _read_file ( self , fname ) : <EOL> x = None <EOL> if os . path . exists ( fname ) : <EOL> with open ( fname ) as f : <EOL> x = f . read ( ) <EOL> return x <EOL> def run ( self ) : <EOL> super ( ) . run ( ) <EOL> self . usercfg . assert_valid_user ( ) <EOL> service = Service ( self . hutcfg , self . usercfg . username ) <EOL> rpc . generate_contract_file ( ) <EOL> if self . local : <EOL> if not self . no_build : <EOL> service . build_push ( force = self . force , push = True , dev = self . dev ) <EOL> else : <EOL> import tempfile <EOL> import requests <EOL> import os . path <EOL> from . common import utils <EOL> from stackhut_client import client <EOL> log . info ( "<STR_LIT>" ) <EOL> with tempfile . NamedTemporaryFile ( suffix = '<STR_LIT>' , delete = False ) as f : <EOL> f . close ( ) <EOL> r_file = stackhut_api_user_call ( '<STR_LIT:file>' , dict ( filename = os . path . basename ( f . name ) ) , self . usercfg ) <EOL> sh . tar ( '<STR_LIT>' , f . name , '<STR_LIT>' , "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT>" , '<STR_LIT:.>' ) <EOL> log . debug ( "<STR_LIT>" . format ( f . name , os . path . getsize ( f . name ) / <NUM_LIT> ) ) <EOL> with open ( f . name , '<STR_LIT:rb>' ) as f1 : <EOL> with Spinner ( ) : <EOL> r = requests . put ( r_file [ '<STR_LIT:url>' ] , data = f1 ) <EOL> r . raise_for_status ( ) <EOL> os . unlink ( f . name ) <EOL> auth = client . SHAuth ( self . usercfg . username , hash = self . usercfg [ '<STR_LIT>' ] ) <EOL> sh_client = client . SHService ( '<STR_LIT>' , '<STR_LIT>' , auth = auth , host = utils . SERVER_URL ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> try : <EOL> with Spinner ( ) : <EOL> r = sh_client . Default . remoteBuild ( r_file [ '<STR_LIT:key>' ] , self . dev ) <EOL> except client . SHRPCError as e : <EOL> log . error ( "<STR_LIT>" ) <EOL> log . error ( e . data [ '<STR_LIT>' ] ) <EOL> return <NUM_LIT:1> <EOL> else : <EOL> log . debug ( "<STR_LIT>" + r [ '<STR_LIT>' ] ) <EOL> if not r [ '<STR_LIT:success>' ] : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> log . info ( "<STR_LIT>" ) <EOL> test_request = json . loads ( self . _read_file ( '<STR_LIT>' ) ) <EOL> readme = self . _read_file ( '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : service . short_name , <EOL> '<STR_LIT>' : self . hutcfg . github_url , <EOL> '<STR_LIT>' : test_request , <EOL> '<STR_LIT:description>' : self . hutcfg . description , <EOL> '<STR_LIT>' : self . hutcfg . private , <EOL> '<STR_LIT>' : readme , <EOL> '<STR_LIT>' : self . create_methods ( ) <EOL> } <EOL> log . info ( "<STR_LIT>" . format ( service . short_name ) ) <EOL> r = stackhut_api_user_call ( '<STR_LIT>' , data , self . usercfg ) <EOL> log . info ( "<STR_LIT>" . format ( service . short_name , r [ '<STR_LIT:message>' ] ) ) <EOL> log . info ( "<STR_LIT>" ) <EOL> return <NUM_LIT:0> <EOL> COMMANDS = [ <EOL> LoginCmd , LogoutCmd , InfoCmd , <EOL> InitCmd , <EOL> HutBuildCmd , DeployCmd , <EOL> StackBuildCmd , RemoteBuildCmd <EOL> ] </s>
