Пример #1
0
def make_docutils_conf(repo_path: pathlib.Path,
                       templates: Environment) -> List[str]:
    """
	Add configuration for ``Docutils``.

	:param repo_path: Path to the repository root.
	:param templates:
	"""

    file = PathPlus(repo_path / templates.globals["docs_dir"] /
                    "docutils.conf")
    file.parent.maybe_make(parents=True)

    if not file.is_file():
        file.write_text('\n'.join([
            "[restructuredtext parser]",
            "tab_width = 4",
            '',
            '',
        ]))

    conf = ConfigUpdater()
    conf.read(str(file))
    required_sections = ["restructuredtext parser"]

    for section in required_sections:
        if section not in conf.sections():
            conf.add_section(section)

    conf["restructuredtext parser"]["tab_width"] = 4

    file.write_clean(str(conf))

    return [file.relative_to(repo_path).as_posix()]
Пример #2
0
class IniConfigurator:
	"""
	Base class to generate ``.ini`` configuration files.

	:param base_path:
	"""

	managed_sections: List[str]
	_ini: ConfigUpdater
	_output: StringList
	managed_message: str = "This file is managed by 'repo_helper'."
	filename: str

	def __init__(self, base_path: pathlib.Path):
		self.base_path = base_path
		self._ini = ConfigUpdater()

		self._output = StringList([
				f"# {self.managed_message}",
				"# You may add new sections, but any changes made to the following sections will be lost:",
				])

		self.managed_sections = self.managed_sections[:]

		for sec in self.managed_sections:
			self._ini.add_section(sec)
			self._output.append(f"#     * {sec}")

		self._output.blankline(ensure_single=True)

	def merge_existing(self, ini_file: pathlib.Path):
		"""
		Merge existing sections in the configuration file into the new configuration.

		:param ini_file: The existing ``.ini`` file.
		"""

		if ini_file.is_file():
			existing_config = ConfigUpdater()
			existing_config.read(str(ini_file))
			for section in existing_config.sections_blocks():
				if section.name not in self.managed_sections:
					self._ini.add_section(section)

	def write_out(self):
		"""
		Write out to the ``.ini`` file.
		"""

		ini_file = PathPlus(self.base_path / self.filename)

		for section_name in self.managed_sections:
			getattr(self, re.sub("[:.-]", '_', section_name))()

		self.merge_existing(ini_file)
		self._output.append(str(self._ini))
		ini_file.write_lines(self._output)

	def copy_existing_value(self, section: Section, key: str):
		"""
		Copy the existing value for ``key``, if present, to the new configuration.

		:param section:
		:param key:
		"""

		if key in section:
			self._ini[section.name][key] = section[key].value
Пример #3
0
def ensure_bumpversion(repo_path: pathlib.Path,
                       templates: Environment) -> List[str]:
    """
	Add configuration for ``bumpversion`` to the desired repo.

	https://pypi.org/project/bumpversion/

	:param repo_path: Path to the repository root.
	:param templates:
	"""

    bumpversion_file = PathPlus(repo_path / ".bumpversion.cfg")

    if not bumpversion_file.is_file():
        bumpversion_file.write_lines([
            "[bumpversion]",
            f"current_version = {templates.globals['version']}",
            "commit = True",
            "tag = True",
        ])

    bv = ConfigUpdater()
    bv.read(str(bumpversion_file))

    old_sections = [
        "bumpversion:file:git_helper.yml", "bumpversion:file:__pkginfo__.py"
    ]
    required_sections = {
        f"bumpversion:file:{filename}"
        for filename in get_bumpversion_filenames(templates)
    }

    if not templates.globals["enable_docs"]:
        old_sections.append(
            f"bumpversion:file:{templates.globals['docs_dir']}/index.rst")

    if not templates.globals["enable_conda"]:
        old_sections.append(f"bumpversion:file:.github/workflows/conda_ci.yml")

    if templates.globals["use_whey"]:
        old_sections.append("bumpversion:file:setup.cfg")

    for section in old_sections:
        if section in bv.sections():
            bv.remove_section(section)
        if section in required_sections:
            required_sections.remove(section)

    for section in sorted(required_sections):
        if section not in bv.sections():
            bv.add_section(section)

    init_filename = get_init_filename(templates)
    if init_filename is not None:
        init_section = bv[f"bumpversion:file:{init_filename}"]
        if "search" not in init_section:
            init_section["search"] = ': str = "{current_version}"'
            init_section["replace"] = ': str = "{new_version}"'

    if "bumpversion:file:setup.cfg" in bv.sections():
        setup_cfg_section = bv["bumpversion:file:setup.cfg"]
        if ("search" not in setup_cfg_section or
            ("search" in setup_cfg_section and
             setup_cfg_section["search"].value == "name = {current_version}")):
            setup_cfg_section["search"] = "version = {current_version}"
            setup_cfg_section["replace"] = "version = {new_version}"

    if "bumpversion:file:pyproject.toml" in bv.sections():
        pp_toml_section = bv["bumpversion:file:pyproject.toml"]
        if "search" not in pp_toml_section:
            pp_toml_section["search"] = 'version = "{current_version}"'
            pp_toml_section["replace"] = 'version = "{new_version}"'

    bv["bumpversion"]["current_version"] = templates.globals["version"]
    bv["bumpversion"]["commit"] = "True"
    bv["bumpversion"]["tag"] = "True"

    bumpversion_file.write_clean(str(bv))

    return [bumpversion_file.name]