<commit_before># Copyright (c) 2014, 2015 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import zipfile
import time
import shutil

from botocore.exceptions import ClientError

import kappa.awsclient
import kappa.log

LOG = logging.getLogger(__name__)


class Function(object):

    def __init__(self, context, config):
        self._context = context
        self._config = config
        self._lambda_client = kappa.awsclient.create_client(
            'lambda', context)
        self._response = None
        self._log = None

    @property
    def name(self):
        return self._context.name

    @property
    def runtime(self):
        return self._config['runtime']

    @property
    def handler(self):
        return self._config['handler']

    @property
    def description(self):
        return self._config['description']

    @property
    def timeout(self):
        return self._config['timeout']

    @property
    def memory_size(self):
        return self._config['memory_size']

    @property
    def zipfile_name(self):
        return self._config['zipfile_name']

    @property
    def path(self):
        return self._config['path']

    @property
    def test_data(self):
        return self._config['test_data']

    @property
    def permissions(self):
        return self._config.get('permissions', list())

    def _get_response(self):
        if self._response is None:
            try:
                self._response = self._lambda_client.call(
                    'get_function',
                    FunctionName=self.name)
                LOG.debug(self._response)
            except Exception:
                LOG.debug('Unable to find ARN for function: %s', self.name)
        return self._response

    @property
    def code_sha_256(self):
        response = self._get_response()
        return response['Configuration']['CodeSha256']

    @property
    def arn(self):
        response = self._get_response()
        return response['Configuration']['FunctionArn']

    @property
    def version(self):
        response = self._get_response()
        return response['Configuration']['Version']

    @property
    def repository_type(self):
        response = self._get_response()
        return response['Code']['RepositoryType']

    @property
    def location(self):
        response = self._get_response()
        return response['Code']['Location']

    def exists(self):
        return self._get_response()

    @property
    def log(self):
        if self._log is None:
            log_group_name = '/aws/lambda/%s' % self.name
            self._log = kappa.log.Log(self._context, log_group_name)
        return self._log

    def tail(self):
        LOG.info('tailing function: %s', self.name)
        return self.log.tail()

    def _zip_lambda_dir(self, zipfile_name, lambda_dir):
        LOG.debug('_zip_lambda_dir: lambda_dir=%s', lambda_dir)
        LOG.debug('zipfile_name=%s', zipfile_name)
        relroot = os.path.abspath(lambda_dir)
        with zipfile.ZipFile(zipfile_name, 'w',
                             compression=zipfile.ZIP_DEFLATED) as zf:
            for root, _, files in os.walk(lambda_dir):
                zf.write(root, os.path.relpath(root, relroot))
                for filename in files:
                    filepath = os.path.join(root, filename)
                    if os.path.isfile(filepath):
                        arcname = os.path.join(
                            os.path.relpath(root, relroot), filename)
                        zf.write(filepath, arcname)

    def _zip_lambda_file(self, zipfile_name, lambda_file):
        LOG.debug('_zip_lambda_file: lambda_file=%s', lambda_file)
        LOG.debug('zipfile_name=%s', zipfile_name)
        with zipfile.ZipFile(zipfile_name, 'w',
                             compression=zipfile.ZIP_DEFLATED) as zf:
            zf.write(lambda_file)

    def zip_lambda_function(self, zipfile_name, lambda_fn):
        if os.path.isdir(lambda_fn):
            self._zip_lambda_dir(zipfile_name, lambda_fn)
        else:
            self._zip_lambda_file(zipfile_name, lambda_fn)

    def add_permissions(self):
        if self.permissions:
            time.sleep(5)
        for permission in self.permissions:
            try:
                kwargs = {
                    'FunctionName': self.name,
                    'StatementId': permission['statement_id'],
                    'Action': permission['action'],
                    'Principal': permission['principal']}
                source_arn = permission.get('source_arn', None)
                if source_arn:
                    kwargs['SourceArn'] = source_arn
                source_account = permission.get('source_account', None)
                if source_account:
                    kwargs['SourceAccount'] = source_account
                response = self._lambda_client.call(
                    'add_permission', **kwargs)
                LOG.debug(response)
            except Exception:
                LOG.exception('Unable to add permission')

    def _copy_config_file(self):
        config_name = '{}_config.json'.format(self._context.environment)
        config_path = os.path.join(self.path, config_name)
        if os.path.exists(config_path):
            dest_path = os.path.join(self.path, 'config.json')
            LOG.info('copy {} to {}'.format(config_path, dest_path))
            shutil.copyfile(config_path, dest_path)

    def create(self):
        LOG.info('creating function %s', self.name)
        self._copy_config_file()
        self.zip_lambda_function(self.zipfile_name, self.path)
        with open(self.zipfile_name, 'rb') as fp:
            exec_role = self._context.exec_role_arn
            LOG.debug('exec_role=%s', exec_role)<commit_msg>Fixing a LOG call<commit_after># Copyright (c) 2014, 2015 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import zipfile
import time
import shutil

from botocore.exceptions import ClientError

import kappa.awsclient
import kappa.log

LOG = logging.getLogger(__name__)


class Function(object):

    def __init__(self, context, config):
        self._context = context
        self._config = config
        self._lambda_client = kappa.awsclient.create_client(
            'lambda', context)
        self._response = None
        self._log = None

    @property
    def name(self):
        return self._context.name

    @property
    def runtime(self):
        return self._config['runtime']

    @property
    def handler(self):
        return self._config['handler']

    @property
    def description(self):
        return self._config['description']

    @property
    def timeout(self):
        return self._config['timeout']

    @property
    def memory_size(self):
        return self._config['memory_size']

    @property
    def zipfile_name(self):
        return self._config['zipfile_name']

    @property
    def path(self):
        return self._config['path']

    @property
    def test_data(self):
        return self._config['test_data']

    @property
    def permissions(self):
        return self._config.get('permissions', list())

    def _get_response(self):
        if self._response is None:
            try:
                self._response = self._lambda_client.call(
                    'get_function',
                    FunctionName=self.name)
                LOG.debug(self._response)
            except Exception:
                LOG.debug('Unable to find ARN for function: %s', self.name)
        return self._response

    @property
    def code_sha_256(self):
        response = self._get_response()
        return response['Configuration']['CodeSha256']

    @property
    def arn(self):
        response = self._get_response()
        return response['Configuration']['FunctionArn']

    @property
    def version(self):
        response = self._get_response()
        return response['Configuration']['Version']

    @property
    def repository_type(self):
        response = self._get_response()
        return response['Code']['RepositoryType']

    @property
    def location(self):
        response = self._get_response()
        return response['Code']['Location']

    def exists(self):
        return self._get_response()

    @property
    def log(self):
        if self._log is None:
            log_group_name = '/aws/lambda/%s' % self.name
            self._log = kappa.log.Log(self._context, log_group_name)
        return self._log

    def tail(self):
        LOG.info('tailing function: %s', self.name)
        return self.log.tail()

    def _zip_lambda_dir(self, zipfile_name, lambda_dir):
        LOG.debug('_zip_lambda_dir: lambda_dir=%s', lambda_dir)
        LOG.debug('zipfile_name=%s', zipfile_name)
        relroot = os.path.abspath(lambda_dir)
        with zipfile.ZipFile(zipfile_name, 'w',
                             compression=zipfile.ZIP_DEFLATED) as zf:
            for root, _, files in os.walk(lambda_dir):
                zf.write(root, os.path.relpath(root, relroot))
                for filename in files:
                    filepath = os.path.join(root, filename)
                    if os.path.isfile(filepath):
                        arcname = os.path.join(
                            os.path.relpath(root, relroot), filename)
                        zf.write(filepath, arcname)

    def _zip_lambda_file(self, zipfile_name, lambda_file):
        LOG.debug('_zip_lambda_file: lambda_file=%s', lambda_file)
        LOG.debug('zipfile_name=%s', zipfile_name)
        with zipfile.ZipFile(zipfile_name, 'w',
                             compression=zipfile.ZIP_DEFLATED) as zf:
            zf.write(lambda_file)

    def zip_lambda_function(self, zipfile_name, lambda_fn):
        if os.path.isdir(lambda_fn):
            self._zip_lambda_dir(zipfile_name, lambda_fn)
        else:
            self._zip_lambda_file(zipfile_name, lambda_fn)

    def add_permissions(self):
        if self.permissions:
            time.sleep(5)
        for permission in self.permissions:
            try:
                kwargs = {
                    'FunctionName': self.name,
                    'StatementId': permission['statement_id'],
                    'Action': permission['action'],
                    'Principal': permission['principal']}
                source_arn = permission.get('source_arn', None)
                if source_arn:
                    kwargs['SourceArn'] = source_arn
                source_account = permission.get('source_account', None)
                if source_account:
                    kwargs['SourceAccount'] = source_account
                response = self._lambda_client.call(
                    'add_permission', **kwargs)
                LOG.debug(response)
            except Exception:
                LOG.exception('Unable to add permission')

    def _copy_config_file(self):
        config_name = '{}_config.json'.format(self._context.environment)
        config_path = os.path.join(self.path, config_name)
        if os.path.exists(config_path):
            dest_path = os.path.join(self.path, 'config.json')
            LOG.info('copy %s to %s', config_path, dest_path)
            shutil.copyfile(config_path, dest_path)

    def create(self):
        LOG.info('creating function %s', self.name)
        self._copy_config_file()
        self.zip_lambda_function(self.zipfile_name, self.path)
        with open(self.zipfile_name, 'rb') as fp:
            exec_role = self._context.exec_role_arn
            LOG.debug('exec_role=%s', exec_role)
{'fraction_non_alphanumeric': 0.10094587075749145, 'fraction_numerical': 0.003020427628964311, 'mean_word_length': 2.226153846153846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7103703', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3425, 'n_words': 818}