Ejemplo n.º 1
0
    def _generic_library(self, directory: str, **kwargs) -> Path:
        # load common repo meta information (metadata that's not language specific).
        if "metadata" in kwargs:
            self._load_generic_metadata(kwargs["metadata"])
            # if no samples were found, don't attempt to render a
            # samples/README.md.
            if "samples" not in kwargs[
                    "metadata"] or not kwargs["metadata"]["samples"]:
                self.excludes.append("samples/README.md")

        t = templates.TemplateGroup(self._template_root / directory,
                                    self.excludes)

        if "repository" in kwargs["metadata"] and "repo" in kwargs["metadata"]:
            kwargs["metadata"]["repo"][
                "default_branch"] = _get_default_branch_name(
                    kwargs["metadata"]["repository"])

        # TODO: migrate to python.py once old sample gen is deprecated
        if directory == "python_samples":
            t.env.globals["get_help"] = lambda filename: shell.run(
                ["python", filename, "--help"]).stdout

        result = t.render(**kwargs)
        _tracked_paths.add(result)

        return result
Ejemplo n.º 2
0
 def _generic_library(self, directory: str, **kwargs) -> Path:
     t = templates.TemplateGroup(_TEMPLATES_DIR / directory)
     result = t.render(**kwargs)
     _tracked_paths.add(result)
     metadata.add_template_source(name=directory,
                                  origin="synthtool.gcp",
                                  version=__main__.VERSION)
     return result
Ejemplo n.º 3
0
def py_samples(*, root: PathOrStr = None, skip_readmes: bool = False) -> None:
    """
    Find all samples projects and render templates.
    Samples projects always have a 'requirements.txt' file and may also have
    README.rst.in

    Args:
        root (Union[Path, str]): The samples directory root.
        skip_readmes (bool): If true, do not generate readmes.
    """
    in_client_library = Path("samples").exists() and Path("setup.py").exists()
    if root is None:
        if in_client_library:
            root = "samples"
        else:
            root = "."

    excludes = []

    # todo(kolea2): temporary exclusion until samples are ready to be migrated to new format
    excludes.append("README.md")

    # TODO(busunkim): Readmegen is disabled as it requires installing the sample
    # requirements in Synthtool. Sample Readmegen should be refactored to stop
    # relying on the output of `python sample.py --help`
    skip_readmes = True
    if skip_readmes:
        excludes.append("README.rst")
    t = templates.TemplateGroup(SAMPLES_TEMPLATE_PATH, excludes=excludes)

    t.env.globals["get_help"] = _get_help  # for sample readmegen

    for req in Path(root).glob("**/requirements.txt"):
        sample_project_dir = req.parent
        log.info(
            f"Generating templates for samples project '{sample_project_dir}'")

        excludes = ["**/*tmpl*"]  # .tmpl. files are partial templates

        sample_readme_metadata: Dict[str, Any] = {}
        if not skip_readmes:
            sample_readme_metadata = _get_sample_readme_metadata(
                sample_project_dir)
            # Don't generate readme if there's no metadata
            if sample_readme_metadata == {}:
                excludes.append("**/README.rst")

        if Path(sample_project_dir / "noxfile_config.py").exists():
            # Don't overwrite existing noxfile configs
            excludes.append("**/noxfile_config.py")

        result = t.render(subdir=sample_project_dir, **sample_readme_metadata)
        _tracked_paths.add(result)
        s.copy([result], excludes=excludes)
Ejemplo n.º 4
0
    def _generic_library(self, directory: str, **kwargs) -> Path:
        # load common repo meta information (metadata that's not language specific).
        if "metadata" in kwargs:
            self._load_generic_metadata(kwargs["metadata"])
            # if no samples were found, don't attempt to render a
            # samples/README.md.
            if not kwargs["metadata"]["samples"]:
                self.excludes.append("samples/README.md")

        t = templates.TemplateGroup(_TEMPLATES_DIR / directory, self.excludes)
        result = t.render(**kwargs)
        _tracked_paths.add(result)
        metadata.add_template_source(name=directory,
                                     origin="synthtool.gcp",
                                     version=__main__.VERSION)
        return result
Ejemplo n.º 5
0
    def _generic_library(self, directory: str, **kwargs) -> Path:
        # load common repo meta information (metadata that's not language specific).
        if "metadata" in kwargs:
            self._load_generic_metadata(kwargs["metadata"])
            # if no samples were found, don't attempt to render a
            # samples/README.md.
            if "samples" not in kwargs[
                    "metadata"] or not kwargs["metadata"]["samples"]:
                self.excludes.append("samples/README.md")

        t = templates.TemplateGroup(self._template_root / directory,
                                    self.excludes)
        result = t.render(**kwargs)
        _tracked_paths.add(result)

        return result
Ejemplo n.º 6
0
def py_samples(*, root: PathOrStr = None, skip_readmes: bool = False) -> None:
    """
    Find all samples projects and render templates.
    Samples projects always have a 'requirements.txt' file and may also have
    README.rst.in

    Args:
        root (Union[Path, str]): The samples directory root.
        skip_readmes (bool): If true, do not generate readmes.
    """
    in_client_library = Path("samples").exists() and Path("setup.py").exists()
    if root is None:
        if in_client_library:
            root = "samples"
        else:
            root = "."

    excludes = []
    if skip_readmes:
        excludes.append("README.rst")
    t = templates.TemplateGroup(SAMPLES_TEMPLATE_PATH, excludes=excludes)

    t.env.globals["get_help"] = _get_help  # for sample readmegen

    for req in Path(root).glob("**/requirements.txt"):
        sample_project_dir = req.parent
        log.info(f"Generating templates for samples project '{sample_project_dir}'")

        excludes = ["**/*tmpl*"]  # .tmpl. files are partial templates

        sample_readme_metadata: Dict[str, Any] = {}
        if not skip_readmes:
            sample_readme_metadata = _get_sample_readme_metadata(sample_project_dir)
            # Don't generate readme if there's no metadata
            if sample_readme_metadata == {}:
                excludes.append("**/README.rst")

        if Path(sample_project_dir / "noxfile_config.py").exists():
            # Don't overwrite existing noxfile configs
            excludes.append("**/noxfile_config.py")

        result = t.render(subdir=sample_project_dir, **sample_readme_metadata)
        _tracked_paths.add(result)
        s.copy([result], excludes=excludes)
Ejemplo n.º 7
0
    def py_samples(self, **kwargs) -> Path:
        """
        Determines whether generation is being done in a client library or in a samples
        folder so it can either generate in the current directory or the client lib's
        'samples' folder. A custom path for where to generate may also be specified.
        Renders README.md according to .repo-metadata.json
        """
        # kwargs["metadata"] is required to load values from .repo-metadata.json
        if "metadata" not in kwargs:
            kwargs["metadata"] = {}
        # load common repo meta information (metadata that's not language specific).
        self._load_generic_metadata(kwargs["metadata"])
        # temporary exclusion prior to old templates being migrated out
        self.excludes.extend([
            "README.rst",
            "auth_api_key.tmpl.rst",
            "auth.tmpl.rst",
            "install_deps.tmpl.rst",
            "install_portaudio.tmpl.rst",
            "noxfile.py.j2",
        ])

        in_client_library = Path("samples").exists()
        sample_project_dir = kwargs["metadata"]["repo"].get(
            "sample_project_dir")

        if sample_project_dir is None:  # Not found in metadata
            if in_client_library:
                sample_project_dir = "samples"
            else:
                sample_project_dir = "."
        elif not Path(sample_project_dir).exists():
            raise Exception(f"'{sample_project_dir}' does not exist")

        logger.debug(
            f"Generating templates for samples directory '{sample_project_dir}'"
        )
        py_samples_templates = Path(self._template_root) / "python_samples"
        t = templates.TemplateGroup(py_samples_templates, self.excludes)
        result = t.render(subdir=sample_project_dir, **kwargs)
        _tracked_paths.add(result)
        return result
Ejemplo n.º 8
0
def test_render_group_with_subdir():
    t = templates.TemplateGroup(FIXTURES / "group")
    result = t.render(subdir="foo/bar", var_a="hello", var_b="world")

    assert (result / "foo/bar" / "1.txt").read_text() == "hello\n"
    assert (result / "foo/bar" / "subdir" / "2.txt").read_text() == "world\n"
Ejemplo n.º 9
0
def test_render_group():
    t = templates.TemplateGroup(FIXTURES / "group")
    result = t.render(var_a="hello", var_b="world")

    assert (result / "1.txt").read_text() == "hello"
    assert (result / "subdir" / "2.txt").read_text() == "world"
Ejemplo n.º 10
0
 def php_library(self, **kwargs) -> Path:
     t = templates.TemplateGroup(_TEMPLATES_DIR / "php_library")
     result = t.render(**kwargs)
     _tracked_paths.add(result)
     return result
Ejemplo n.º 11
0
 def node_library(self, **kwargs) -> Path:
     kwargs["metadata"] = node.read_metadata()
     t = templates.TemplateGroup(_TEMPLATES_DIR / "node_library")
     result = t.render(**kwargs)
     _tracked_paths.add(result)
     return result
Ejemplo n.º 12
0
 def node_library(self, package_name, repo_name) -> Path:
     t = templates.TemplateGroup(_TEMPLATES_DIR / "node_library")
     result = t.render(package_name=package_name, repo_name=repo_name)
     _tracked_paths.add(result)
     return result