Пример #1
0
    def _zip_func(self, func_path):
        buff = BytesIO()
        if not os.path.exists(func_path):
            raise ContextException(
                "Function file or path not found by CodeUri '{}'".format(
                    func_path))

        zip_file_name = str(uuid.uuid1()) + '.zip'
        cwd = os.getcwd()
        os.chdir(func_path)
        with ZipFile(buff, mode='w', compression=ZIP_DEFLATED) as zip_object:
            for current_path, sub_folders, files_name in os.walk(_CURRENT_DIR):
                for file in files_name:
                    zip_object.write(os.path.join(current_path, file))

        os.chdir(cwd)
        buff.seek(0)
        buff.name = zip_file_name

        # a temporary support for upload func from local zipfile
        with open(zip_file_name, 'wb') as f:
            f.write(buff.read())
            buff.seek(0)
        # click.secho("Compress function '{}' to zipfile '{}' success".format(func_config.name, zip_file_name))

        return buff, zip_file_name
Пример #2
0
    def get_template_data(template_file):
        if not os.path.exists(template_file):
            return {}

        with io.open(template_file, mode='r', encoding='utf-8') as f:
            try:
                return yaml_parse(f.read())
            except (ValueError, yaml.YAMLError) as ex:
                raise ContextException("Parse template failed: {}".format(str(ex)))
Пример #3
0
    def get_template_data(template_file):
        if not os.path.exists(template_file):
            return {}

        with open(template_file, 'r') as f:
            try:
                return yaml_parse(f.read())
            except (ValueError, yaml.YAMLError) as ex:
                Operation(ex, err_msg=traceback.format_exc(), level="ERROR").no_output()
                raise ContextException("Parse template failed: {}".format(str(ex)))
        return
Пример #4
0
    def _zip_func(self, func_path, namespace, func_name):
        buff = BytesIO()
        if not os.path.exists(func_path):
            raise ContextException(
                "Function file or path not found by CodeUri '{}'".format(
                    func_path))

        if self.deploy_namespace and self.deploy_namespace != namespace:
            namespace = self.deploy_namespace

        zip_file_name = str(namespace) + '-' + str(func_name) + '-latest.zip'
        zip_file_name_cos = str(namespace) + '-' + str(
            func_name) + '-latest' + time.strftime(
                "-%Y-%m-%d-%H-%M-%S", time.localtime(int(
                    time.time()))) + '.zip'
        cwd = os.getcwd()
        os.chdir(self.template_file_dir)
        os.chdir(func_path)

        with ZipFile(buff, mode='w', compression=ZIP_DEFLATED) as zip_object:
            for current_path, sub_folders, files_name in os.walk(_CURRENT_DIR):
                if current_path == _BUILD_DIR:
                    continue
                for file in files_name:
                    zip_object.write(os.path.join(current_path, file))

        os.chdir(cwd)
        buff.seek(0)
        buff.name = zip_file_name

        if not os.path.exists(_BUILD_DIR):
            os.mkdir(_BUILD_DIR)
        zip_file_path = os.path.join(_BUILD_DIR, zip_file_name)

        if os.path.exists(zip_file_path):
            os.remove(zip_file_path)

        # a temporary support for upload func from local zipfile
        with open(zip_file_path, 'wb') as f:
            f.write(buff.read())
            buff.seek(0)
        click.secho("Compress function '{}' to zipfile '{}' success".format(
            zip_file_path, zip_file_name))

        return buff, zip_file_name, zip_file_name_cos