Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poetry integration #9

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
poetry.lock

# EDITORS
.vscode
.idea
Expand Down
45 changes: 45 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
48 changes: 0 additions & 48 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -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 = [email protected]
license = BSD 4-Clause License
license_file = LICENSE
maintainer = BuildNN Team
maintainer_email = [email protected]
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 =
Expand Down
3 changes: 0 additions & 3 deletions setup.py

This file was deleted.

46 changes: 36 additions & 10 deletions src/pymetager/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import click
import os
import configparser
import toml

from .version_manager import (
_version_ops,
Expand All @@ -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.",
Expand All @@ -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")
Expand Down Expand Up @@ -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,
Expand All @@ -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__":
Expand Down
38 changes: 29 additions & 9 deletions src/pymetager/version_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"""

from packaging.version import Version
import toml


_main_version_components = ["major", "minor", "micro"]
_segment_version_components = ["dev", "pre", "post"]
Expand Down Expand Up @@ -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
Expand All @@ -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`."
)