Best Python code snippet using localstack_python
34072_lambda_api.py
Source:34072_lambda_api.py  
...297        if suppress_output:298            sys.stdout = stdout_299            sys.stderr = stderr_300    return result301def exec_lambda_code(script, handler_function='handler', lambda_cwd=None, lambda_env=None):302    if lambda_cwd or lambda_env:303        exec_mutex.acquire()304        if lambda_cwd:305            previous_cwd = os.getcwd()306            os.chdir(lambda_cwd)307            sys.path = [lambda_cwd] + sys.path308        if lambda_env:309            previous_env = dict(os.environ)310            os.environ.update(lambda_env)311    # generate lambda file name312    lambda_id = 'l_%s' % short_uid()313    lambda_file = LAMBDA_SCRIPT_PATTERN.replace('*', lambda_id)314    save_file(lambda_file, script)315    # delete temporary .py and .pyc files on exit316    TMP_FILES.append(lambda_file)317    TMP_FILES.append('%sc' % lambda_file)318    try:319        handler_module = imp.load_source(lambda_id, lambda_file)320        module_vars = handler_module.__dict__321    except Exception as e:322        LOG.error('Unable to exec: %s %s' % (script, traceback.format_exc()))323        raise e324    finally:325        if lambda_cwd or lambda_env:326            if lambda_cwd:327                os.chdir(previous_cwd)328                sys.path.pop(0)329            if lambda_env:330                os.environ = previous_env331            exec_mutex.release()332    return module_vars[handler_function]333def get_handler_file_from_name(handler_name, runtime=LAMBDA_RUNTIME_PYTHON27):334    # TODO: support Java Lambdas in the future335    file_ext = '.js' if runtime.startswith(LAMBDA_RUNTIME_NODEJS) else '.py'336    return '%s%s' % (handler_name.split('.')[0], file_ext)337def get_handler_function_from_name(handler_name, runtime=LAMBDA_RUNTIME_PYTHON27):338    # TODO: support Java Lambdas in the future339    return handler_name.split('.')[-1]340def error_response(msg, code=500, error_type='InternalFailure'):341    LOG.warning(msg)342    return aws_responses.flask_error_response(msg, code=code, error_type=error_type)343def run_lambda_executor(cmd, env_vars={}):344    process = run(cmd, async=True, stderr=subprocess.PIPE, outfile=subprocess.PIPE, env_vars=env_vars)345    return_code = process.wait()346    result = to_str(process.stdout.read())347    log_output = to_str(process.stderr.read())348    if return_code != 0:349        raise Exception('Lambda process returned error status code: %s. Output:\n%s' %350            (return_code, log_output))351    return result, log_output352def set_function_code(code, lambda_name):353    def generic_handler(event, context):354        raise Exception(('Unable to find executor for Lambda function "%s". ' +355            'Note that Node.js Lambdas currently require LAMBDA_EXECUTOR=docker') % lambda_name)356    lambda_handler = generic_handler357    lambda_cwd = None358    arn = func_arn(lambda_name)359    runtime = arn_to_lambda[arn].runtime360    handler_name = arn_to_lambda.get(arn).handler361    lambda_environment = arn_to_lambda.get(arn).envvars362    if not handler_name:363        handler_name = LAMBDA_DEFAULT_HANDLER364    handler_file = get_handler_file_from_name(handler_name, runtime=runtime)365    handler_function = get_handler_function_from_name(handler_name, runtime=runtime)366    if 'S3Bucket' in code:367        s3_client = aws_stack.connect_to_service('s3')368        bytes_io = BytesIO()369        try:370            s3_client.download_fileobj(code['S3Bucket'], code['S3Key'], bytes_io)371            zip_file_content = bytes_io.getvalue()372        except Exception as e:373            return error_response('Unable to fetch Lambda archive from S3: %s' % e, 404)374    elif 'ZipFile' in code:375        zip_file_content = code['ZipFile']376        zip_file_content = base64.b64decode(zip_file_content)377    else:378        return error_response('No valid Lambda archive specified.', 400)379    # save tmp file380    tmp_dir = '%s/zipfile.%s' % (config.TMP_FOLDER, short_uid())381    run('mkdir -p %s' % tmp_dir)382    tmp_file = '%s/%s' % (tmp_dir, LAMBDA_ZIP_FILE_NAME)383    save_file(tmp_file, zip_file_content)384    TMP_FILES.append(tmp_dir)385    lambda_cwd = tmp_dir386    # check if this is a ZIP file387    is_zip = is_zip_file(zip_file_content)388    if is_zip:389        unzip(tmp_file, tmp_dir)390        main_file = '%s/%s' % (tmp_dir, handler_file)391        if not os.path.isfile(main_file):392            # check if this is a zip file that contains a single JAR file393            jar_files = glob.glob('%s/*.jar' % tmp_dir)394            if len(jar_files) == 1:395                main_file = jar_files[0]396        if os.path.isfile(main_file):397            with open(main_file, 'rb') as file_obj:398                zip_file_content = file_obj.read()399        else:400            file_list = run('ls -la %s' % tmp_dir)401            LOG.debug('Lambda archive content:\n%s' % file_list)402            return error_response('Unable to find handler script in Lambda archive.', 400, error_type='ValidationError')403    # it could be a JAR file (regardless of whether wrapped in a ZIP file or not)404    is_jar = is_jar_archive(zip_file_content)405    if is_jar:406        def execute(event, context):407            event_file = EVENT_FILE_PATTERN.replace('*', short_uid())408            save_file(event_file, json.dumps(event))409            TMP_FILES.append(event_file)410            class_name = arn_to_lambda[arn].handler.split('::')[0]411            classpath = '%s:%s' % (LAMBDA_EXECUTOR_JAR, main_file)412            cmd = 'java -cp %s %s %s %s' % (classpath, LAMBDA_EXECUTOR_CLASS, class_name, event_file)413            result, log_output = run_lambda_executor(cmd)414            LOG.info('Lambda output: %s' % log_output.replace('\n', '\n> '))415            return result416        lambda_handler = execute417    elif runtime.startswith('python') and not use_docker():418        try:419            lambda_handler = exec_lambda_code(zip_file_content,420                handler_function=handler_function, lambda_cwd=lambda_cwd,421                lambda_env=lambda_environment)422        except Exception as e:423            raise Exception('Unable to get handler function from lambda code.', e)424    if not is_zip and not is_jar:425        raise Exception('Uploaded Lambda code is neither a ZIP nor JAR file.')426    add_function_mapping(lambda_name, lambda_handler, lambda_cwd)427    return {'FunctionName': lambda_name}428def do_list_functions():429    funcs = []430    for f_arn, func in iteritems(arn_to_lambda):431        func_name = f_arn.split(':function:')[-1]432        arn = func_arn(func_name)433        funcs.append({...lambda_api.py
Source:lambda_api.py  
...288        if suppress_output:289            sys.stdout = stdout_290            sys.stderr = stderr_291    return result292def exec_lambda_code(script, handler_function='handler', lambda_cwd=None, lambda_env=None):293    if lambda_cwd or lambda_env:294        exec_mutex.acquire()295        if lambda_cwd:296            previous_cwd = os.getcwd()297            os.chdir(lambda_cwd)298            sys.path = [lambda_cwd] + sys.path299        if lambda_env:300            previous_env = dict(os.environ)301            os.environ.update(lambda_env)302    # generate lambda file name303    lambda_id = 'l_%s' % short_uid()304    lambda_file = LAMBDA_SCRIPT_PATTERN.replace('*', lambda_id)305    save_file(lambda_file, script)306    # delete temporary .py and .pyc files on exit307    TMP_FILES.append(lambda_file)308    TMP_FILES.append('%sc' % lambda_file)309    try:310        handler_module = imp.load_source(lambda_id, lambda_file)311        module_vars = handler_module.__dict__312    except Exception as e:313        LOG.error('Unable to exec: %s %s' % (script, traceback.format_exc()))314        raise e315    finally:316        if lambda_cwd or lambda_env:317            if lambda_cwd:318                os.chdir(previous_cwd)319                sys.path.pop(0)320            if lambda_env:321                os.environ = previous_env322            exec_mutex.release()323    return module_vars[handler_function]324def get_handler_file_from_name(handler_name, runtime=LAMBDA_RUNTIME_PYTHON27):325    # TODO: support Java Lambdas in the future326    file_ext = '.js' if runtime.startswith(LAMBDA_RUNTIME_NODEJS) else '.py'327    return '%s%s' % (handler_name.split('.')[0], file_ext)328def get_handler_function_from_name(handler_name, runtime=LAMBDA_RUNTIME_PYTHON27):329    # TODO: support Java Lambdas in the future330    return handler_name.split('.')[-1]331def error_response(msg, code=500, error_type='InternalFailure'):332    LOG.warning(msg)333    return aws_responses.flask_error_response(msg, code=code, error_type=error_type)334def run_lambda_executor(cmd, env_vars={}):335    process = run(cmd, async=True, stderr=subprocess.PIPE, outfile=subprocess.PIPE, env_vars=env_vars)336    return_code = process.wait()337    result = to_str(process.stdout.read())338    log_output = to_str(process.stderr.read())339    if return_code != 0:340        raise Exception('Lambda process returned error status code: %s. Output:\n%s' %341            (return_code, log_output))342    return result, log_output343def set_function_code(code, lambda_name):344    def generic_handler(event, context):345        raise Exception(('Unable to find executor for Lambda function "%s". ' +346            'Note that Node.js Lambdas currently require LAMBDA_EXECUTOR=docker') % lambda_name)347    lambda_handler = generic_handler348    lambda_cwd = None349    arn = func_arn(lambda_name)350    runtime = arn_to_lambda[arn].runtime351    handler_name = arn_to_lambda.get(arn).handler352    lambda_environment = arn_to_lambda.get(arn).envvars353    if not handler_name:354        handler_name = LAMBDA_DEFAULT_HANDLER355    handler_file = get_handler_file_from_name(handler_name, runtime=runtime)356    handler_function = get_handler_function_from_name(handler_name, runtime=runtime)357    if 'S3Bucket' in code:358        s3_client = aws_stack.connect_to_service('s3')359        bytes_io = BytesIO()360        try:361            s3_client.download_fileobj(code['S3Bucket'], code['S3Key'], bytes_io)362            zip_file_content = bytes_io.getvalue()363        except Exception as e:364            return error_response('Unable to fetch Lambda archive from S3: %s' % e, 404)365    elif 'ZipFile' in code:366        zip_file_content = code['ZipFile']367        zip_file_content = base64.b64decode(zip_file_content)368    else:369        return error_response('No valid Lambda archive specified.', 400)370    # save tmp file371    tmp_dir = '%s/zipfile.%s' % (config.TMP_FOLDER, short_uid())372    run('mkdir -p %s' % tmp_dir)373    tmp_file = '%s/%s' % (tmp_dir, LAMBDA_ZIP_FILE_NAME)374    save_file(tmp_file, zip_file_content)375    TMP_FILES.append(tmp_dir)376    lambda_cwd = tmp_dir377    # check if this is a ZIP file378    is_zip = is_zip_file(zip_file_content)379    if is_zip:380        unzip(tmp_file, tmp_dir)381        main_file = '%s/%s' % (tmp_dir, handler_file)382        if not os.path.isfile(main_file):383            # check if this is a zip file that contains a single JAR file384            jar_files = glob.glob('%s/*.jar' % tmp_dir)385            if len(jar_files) == 1:386                main_file = jar_files[0]387        if os.path.isfile(main_file):388            with open(main_file, 'rb') as file_obj:389                zip_file_content = file_obj.read()390        else:391            file_list = run('ls -la %s' % tmp_dir)392            LOG.debug('Lambda archive content:\n%s' % file_list)393            return error_response('Unable to find handler script in Lambda archive.', 400, error_type='ValidationError')394    # it could be a JAR file (regardless of whether wrapped in a ZIP file or not)395    is_jar = is_jar_archive(zip_file_content)396    if is_jar:397        def execute(event, context):398            event_file = EVENT_FILE_PATTERN.replace('*', short_uid())399            save_file(event_file, json.dumps(event))400            TMP_FILES.append(event_file)401            class_name = arn_to_lambda[arn].handler.split('::')[0]402            classpath = '%s:%s' % (LAMBDA_EXECUTOR_JAR, main_file)403            cmd = 'java -cp %s %s %s %s' % (classpath, LAMBDA_EXECUTOR_CLASS, class_name, event_file)404            result, log_output = run_lambda_executor(cmd)405            LOG.info('Lambda output: %s' % log_output.replace('\n', '\n> '))406            return result407        lambda_handler = execute408    elif runtime.startswith('python') and not use_docker():409        try:410            lambda_handler = exec_lambda_code(zip_file_content,411                handler_function=handler_function, lambda_cwd=lambda_cwd,412                lambda_env=lambda_environment)413        except Exception as e:414            raise Exception('Unable to get handler function from lambda code.', e)415    if not is_zip and not is_jar:416        raise Exception('Uploaded Lambda code is neither a ZIP nor JAR file.')417    add_function_mapping(lambda_name, lambda_handler, lambda_cwd)418    return {'FunctionName': lambda_name}419def do_list_functions():420    funcs = []421    for f_arn, func in iteritems(arn_to_lambda):422        func_name = f_arn.split(':function:')[-1]423        arn = func_arn(func_name)424        funcs.append({...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
