def create_rst_file(dir_path):
    p = Path(dir_path)
    relpath = p.relative_to(dir_images)
    rst_dirpath = Path(dir_here, "99-附录 (Appendix)", "01-常用图标外链查询", relpath)

    if not rst_dirpath.exists():
        rst_dirpath.mkdir()
    rst_path = Path(rst_dirpath, "index.rst")

    lines = list()
    lines.append(p.basename)
    lines.append("=" * 80)
    lines.append(".. contents:: 索引")
    lines.append("    :local:")

    sub_p_list = Path.sort_by_abspath(dir_path.select_dir(recursive=False))
    if len(sub_p_list):
        lines.append("\n**目录**:\n")
        lines.append("\n.. articles::\n")

    for p_png in Path.sort_by_abspath(p.select_image(recursive=False)):
        lines.append("\n" + p_png.fname)
        lines.append("-" * 80)
        url = "/" + str(p_png.relative_to(dir_here))
        directive = ".. image:: {}".format(url)
        lines.append(directive)

    content = "\n".join(lines)
    rst_path.write_text(content, "utf-8")

    if len(sub_p_list):
        for sub_p in sub_p_list:
            create_rst_file(sub_p)
    def plan(self, workspace_dir):
        """
        This method

        :param workspace_dir:
        :return:

        **中文文档**

        此方法将 ``master_tier``, ``tier``, ``config_dir``, ``plan_file`` 中的
        所有信息汇总, 在 ``workspace_dir`` 下生成多个文件夹, 每个文件夹都是一个单独的
        ``aws cloudformation deploy`` 所需要的的文件.
        """
        env_tag_list = extract_all_env_tag(self._plan)
        config_data_mapper = OrderedDict()  # type: OrderedDict[str, dict]
        for env_tag in env_tag_list:
            p = Path(self._config_dir, "{}.json".format(env_tag))
            if not p.exists():
                raise FileNotFoundError(
                    "the config file of environment `{}` not exists at '{}'".format(env_tag, p))
            config_data_mapper[env_tag] = json.load(
                p.abspath, ignore_comments=True, verbose=False)

        pipeline = resolve_pipeline(self._plan)

        workspace_dir = Path(workspace_dir)
        workspace_dir.mkdir(parents=True, exist_ok=True)

        deploy_execution_counter = 0
        for can_id_list, env_tag in pipeline:
            # counter is used in deploy workspace dir name space
            deploy_execution_counter += 1
            deploy_workspace_dir = Path(
                workspace_dir,
                "{}-{}".format(str(deploy_execution_counter).zfill(3), env_tag)
            )
            deploy_workspace_dir.mkdir(parents=True, exist_ok=True)
            config_data = config_data_mapper[env_tag]

            # collect template instance and file path
            # so we can generate final template files at once
            template_file_list = list()  # type: List[TemplateFile]

            master_can = self._master_tier(**config_data)
            master_can.create_template()

            # master_can_label = self.canlabel_mapper[self.master_canlabel_id]
            # master_can = master_can_label.can_class(**config_data)
            # master_can.CONFIG_DIR = deploy_workspace_dir.abspath
            master_template_path = Path(
                deploy_workspace_dir, master_can.rel_path)
            template_file_list.append(
                TemplateFile(
                    template=master_can.template,
                    filepath=master_template_path,
                )
            )
Beispiel #3
0
    def plan(self, temp_dir):
        pipeline = resolve_pipeline([(note.can_id, note.env_tag)
                                     for note in self.notes])

        nested_can_mapper = dict()  # type: Dict[str, Canned]

        returned_list = list()

        STOP_AT_IND = 4
        counter = 0
        for can_id_list, env_tag in pipeline:
            counter += 1

            deploy_workspace_dir = Path(
                temp_dir, "{}-{}".format(str(counter).zfill(3), env_tag))
            deploy_workspace_dir.mkdir(parents=True, exist_ok=True)
            returned_list.append(deploy_workspace_dir)

            template_file_list = list()
            config_data = self.config_data_mapper[env_tag].data

            master_can_label = self.canlabel_mapper[self.master_canlabel_id]
            master_can = master_can_label.can_class(**config_data)
            master_can.CONFIG_DIR = deploy_workspace_dir.abspath
            master_can.create_template()

            master_template_path = Path(deploy_workspace_dir,
                                        master_can_label.filename)
            template_file_list.append(
                TemplateFile(
                    template=master_can.template,
                    filepath=master_template_path,
                ))

            # construct resource filter
            # based on two
            # 1. The current execution job's ``CanLabel.logic_id`` (Nested Stack Resource Logic Id)
            # 2. Environment specified config data's ``TIER_LIST_TO_DEPLOY``

            allowed_stack_id_list = [
                resource_id for resource_id in can_id_list
                if resource_id in master_can.TIER_LIST_TO_DEPLOY.get_value()
            ]
            r_filter = ResourceFilter(allowed_stack_id_list)

            # remove ignored stacks
            for resource_id, resource in list(
                    master_can.template.resources.items()):
                keep_this_flag = r_filter.filter(resource, master_can.template)
                if not keep_this_flag:
                    master_can.template.remove_resource(resource)
                else:
                    if resource_id in self.canlabel_mapper:
                        nested_canlabel = self.canlabel_mapper[resource_id]
                        nested_can = nested_canlabel.can_class(**config_data)
                        nested_can.create_template()
                        nested_can_mapper[resource_id] = nested_can

                        template_file = TemplateFile(
                            template=nested_can.template,
                            filepath=Path(deploy_workspace_dir,
                                          nested_canlabel.filename))
                        template_file_list.append(template_file)

            # construct ExecutionJob
            print("=" * 10)
            print(can_id_list, env_tag)

            master_can.dump_cloudformation_json_config_file()

            for template_file in template_file_list:
                template_file.make_file(json_or_yml="json")

            # break

            # if STOP_AT_IND == counter:
            #     break
        return returned_list
# -*- coding: utf-8 -*-
"""
dir settings
"""

from __future__ import unicode_literals
from pathlib_mate import PathCls as Path

HOME = Path.home()
"""
User home directory:

- Windows: C:\\Users\\<username>
- MacOS: /Users/<username>
- Ubuntu: /home/<username> 
"""

ALFRED_FTS = Path(HOME, ".alfred-fts")
"""
Alfred Full Text Search Data Folder: ${HOME}/.alfred-fts
"""

if not ALFRED_FTS.exists():
    ALFRED_FTS.mkdir()