From b47a703382903817ceb6beea3fd637f7eddedf97 Mon Sep 17 00:00:00 2001 From: ggbaro Date: Tue, 3 Aug 2021 17:13:05 +0200 Subject: [PATCH] poetry integration --- .gitignore | 2 ++ pyproject.toml | 45 ++++++++++++++++++++++++++++++ setup.cfg | 48 -------------------------------- setup.py | 3 -- src/pymetager/cli.py | 46 +++++++++++++++++++++++------- src/pymetager/version_manager.py | 38 +++++++++++++++++++------ 6 files changed, 112 insertions(+), 70 deletions(-) create mode 100644 pyproject.toml delete mode 100644 setup.py diff --git a/.gitignore b/.gitignore index 24cb3ce..b611cf7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +poetry.lock + # EDITORS .vscode .idea diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..b4a8aff --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,45 @@ +[build-system] +requires = [ "poetry-core>=1.0.0",] +build-backend = "poetry.core.masonry.api" +package_dir = "src" +include_package_data = true + +[tool.poetry] +name = "pymetager" +version = "0.0.2" +description = "A Python package metadata manager by BuildNN." +authors = [ "BuildNN Team",] +license = "BSD 4-Clause License" +maintainers = [ "BuildNN Team",] +readme = "README.rst" +homepage = "https://pymetager.readthedocs.io/en/latest/" +repository = "https://www.github.com/buildnn/pymetager" +documentation = "https://pymetager.readthedocs.io/en/latest/" +keywords = [ "pymetager", "buildnn",] +classifiers = [ "Environment :: Console", "Intended Audience :: Developers", "Operating System :: POSIX :: Linux", "Operating System :: Unix", "Programming Language :: Python :: 3", "Topic :: Software Development :: Libraries :: Python Modules",] + +[build-system.packages.find] +where = "src" +include = "*" +exclude = "*.egg-info" + +[tool.poetry.urls] +Code = "https://www.github.com/buildnn/pymetager" +"Bug tracker" = "https://github.com/buildnn/pymetager/issues" + +[tool.poetry.dependencies] +python = ">=3.8, <=3.10" +click = ">=6" +packaging = ">=18" + +[tool.poetry.dev-dependencies] +black = ">=20.8b0" +coverage = ">=5.0.1" +flake8 = ">=3.8.0" +nox = ">=2020.5.24" +pytest = ">= 5.4.1" +pytest-cov = ">=2.10.0" +python-dotenv = ">=0.5.1" + +[tool.poetry.scripts] +pymetager = "pymetager.cli:cli" diff --git a/setup.cfg b/setup.cfg index 9399d4b..9e3a105 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,51 +1,3 @@ -[metadata] -name = pymetager -version = 0.0.1 -url = https://github.com/buildnn/pypack-metager -project_urls = - Repository = https://github.com/buildnn/pypack-metager - Documentation = https://github.com/buildnn/pypack-metager - Code = https://www.github.com/buildnn/pypack-metager - Issue tracker = https://github.com/buildnn/pypack-metager/issues -author = BuildNN Team -author_email = dev@buildnn.com -license = BSD 4-Clause License -license_file = LICENSE -maintainer = BuildNN Team -maintainer_email = dev@buildnn.com -description = A Python package metadata manager by BuildNN. -long_description = file: README.rst -classifiers = - Environment :: Console - Intended Audience :: Developers - Operating System :: POSIX :: Linux - Operating System :: Unix - Programming Language :: Python :: 3 - Topic :: Software Development :: Libraries :: Python Modules -keywords = - pypack_metager - buildnn - -[options] -packages = find: -package_dir = = src -include_package_data = true -python_requires = >= 3.8 -install_requires = - click - packaging - -[options.entry_points] -console_scripts = - pymetager = pymetager.cli:cli - -[options.packages.find] -where = src -include = - * -exclude = - *.egg-info - [flake8] max-line-length = 88 per-file-ignores = diff --git a/setup.py b/setup.py deleted file mode 100644 index 6068493..0000000 --- a/setup.py +++ /dev/null @@ -1,3 +0,0 @@ -from setuptools import setup - -setup() diff --git a/src/pymetager/cli.py b/src/pymetager/cli.py index 03fc6f0..445cb86 100644 --- a/src/pymetager/cli.py +++ b/src/pymetager/cli.py @@ -4,6 +4,7 @@ import click import os import configparser +import toml from .version_manager import ( _version_ops, @@ -13,17 +14,36 @@ ) -def get_config(config_fp=os.path.join(".", "setup.cfg")): - config = configparser.ConfigParser() - config.read(config_fp) - return config, config_fp +DEFAULT_SETUP_CFG = os.path.join(os.curdir, "setup.cfg") +DEFAULT_PYPROJECT_TOML = os.path.join(os.curdir, "pyproject.toml") + + +if os.path.isfile(DEFAULT_PYPROJECT_TOML): + DEFAULT_CONFIG = DEFAULT_PYPROJECT_TOML +else: + DEFAULT_CONFIG = DEFAULT_SETUP_CFG + + +def get_config(config_fp=DEFAULT_CONFIG): + _, extension = os.path.splitext(config_fp) + if extension == ".cfg": + config = configparser.ConfigParser() + config.read(config_fp) + return config, config_fp, extension + elif extension == ".toml": + config = toml.load(config_fp) + return config, config_fp, extension + else: + raise NotImplementedError( + "File extension not recognized. Shoud be `.toml` or `.cfg`." + ) @click.group("pypack-metager") @click.option("-q", "--quiet", is_flag=True, help="Flag for minimal output.") @click.option( "--config_fp", - default=os.path.join(".", "setup.cfg"), + default=DEFAULT_CONFIG, show_default=True, type=click.Path(exists=True, file_okay=True, dir_okay=False), help="Custom path for setup.cfg.", @@ -33,9 +53,10 @@ def cli(ctx, quiet, config_fp): ctx.ensure_object(dict) ctx.obj["QUIET"] = quiet - config, config_fp = get_config(config_fp=config_fp) + config, config_fp, config_type = get_config(config_fp=config_fp) ctx.obj["CONFIG"] = config ctx.obj["CONFIG_FP"] = config_fp + ctx.obj["CONFIG_TYPE"] = config_type if not quiet: click.secho("--- PYPACK-METAGER ---", fg="green") @@ -64,6 +85,7 @@ def increment(ctx, element, segment, increment_upstream, custom_version, force): update_config_version( config=ctx.obj["CONFIG"], config_fp=ctx.obj["CONFIG_FP"], + config_type=ctx.obj["CONFIG_TYPE"], element=element, segment=segment, increment_upstream=increment_upstream, @@ -78,14 +100,18 @@ def increment(ctx, element, segment, increment_upstream, custom_version, force): @click.option("-s", "--section", type=click.STRING, default="metadata") def echo_meta_elm(ctx, name, section): config = ctx.obj["CONFIG"] + config_fp = ctx.obj["CONFIG_FP"] + try: - _section = config[section] + _section = config + for s in section.split("."): + _section = _section[s] except KeyError: - raise KeyError(f"Section '{section}' does not exist in `setup.cfg`.") + raise KeyError(f"Section '{section}' does not exist in `{config_fp}`.") try: - sys.stdout.write(_section[name] + "\n") + sys.stdout.write(str(_section[name]) + "\n") except KeyError: - raise KeyError(f"Tag '{name}' does not exist in `setup.cfg`/'{section}'.") + raise KeyError(f"Tag '{name}' does not exist in `{config_fp}`/'{section}'.") if __name__ == "__main__": diff --git a/src/pymetager/version_manager.py b/src/pymetager/version_manager.py index 9a6d5d8..b138728 100644 --- a/src/pymetager/version_manager.py +++ b/src/pymetager/version_manager.py @@ -4,6 +4,8 @@ """ from packaging.version import Version +import toml + _main_version_components = ["major", "minor", "micro"] _segment_version_components = ["dev", "pre", "post"] @@ -170,10 +172,7 @@ def to_custom_version(version, custom_version, **kwargs): } -def update_config_version(config, config_fp, element, **kwargs): - print(f"Read config from {config_fp}") - version = Version(config["metadata"]["version"]) - +def _update(element, version, **kwargs): new_version = _version_ops[element](version, **kwargs) try: assert new_version > version @@ -182,10 +181,31 @@ def update_config_version(config, config_fp, element, **kwargs): print("WARNING: we are reverting the version!") else: raise e + return new_version + - print(f"Updating version `{version}` to `{new_version}`.") - config["metadata"]["version"] = str(new_version) +def update_config_version(config, config_fp, config_type, element, **kwargs): + print(f"Read config from {config_fp}") + + if config_type == ".cfg": + version = Version(config["metadata"]["version"]) + new_version = _update(element, version, **kwargs) + print(f"Updating version `{version}` to `{new_version}`.") + config["metadata"]["version"] = str(new_version) + print("Writing back config...") + with open(config_fp, "w") as configfile: + config.write(configfile) + + elif config_type == ".toml": + version = Version(config["tool"]["poetry"]["version"]) + new_version = _update(element, version, **kwargs) + print(f"Updating version `{version}` to `{new_version}`.") + config["tool"]["poetry"]["version"] = str(new_version) + print("Writing back config...") + with open(config_fp, "w") as configfile: + toml.dump(config, configfile) - print("Writing back config...") - with open(config_fp, "w") as configfile: - config.write(configfile) + else: + raise NotImplementedError( + "File extension not recognized. Shoud be `.toml` or `.cfg`." + )