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

Add pypi support #18

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ the pre-release identifier or local version segment::
--pre TEXT Set the pre-release identifier
--local TEXT Set the local version segment
--canonicalize Canonicalize the new version
--pypi Get latest information about version from pypi.org
--help Show this message and exit.

The `--reset` option should be used alongside with minor or major bump.
Expand Down
57 changes: 52 additions & 5 deletions bump.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
import sys

import requests

try:
import configparser
except ImportError: # 2.7
Expand All @@ -10,7 +12,8 @@
from first import first
from packaging.utils import canonicalize_version

pattern = re.compile(r"((?:__)?version(?:__)? ?= ?[\"'])(.+?)([\"'])")
version_pattern = re.compile(r"((?:__)?version(?:__)? ?= ?[\"'])(.+?)([\"'])")
name_pattern = re.compile(r"((?:__)?name(?:__)? ?= ?[\"'])(.+?)([\"'])")


class SemVer(object):
Expand Down Expand Up @@ -87,13 +90,41 @@ class NoVersionFound(Exception):
pass


class NoNameFound(Exception):
pass


def find_version(input_string):
match = first(pattern.findall(input_string))
match = first(version_pattern.findall(input_string))
if match is None:
raise NoVersionFound
return match[1]


def find_name(input_string):
match = first(name_pattern.findall(input_string))
if match is None:
raise NoNameFound
return match[1]


def get_latest_version(name):
response = requests.get("https://pypi.org/pypi/{}/json".format(name)).json()
return response["info"]["version"]


def compare_version(local_version, pypi_version):
from packaging import version

if local_version == pypi_version:
return local_version, False
return (
(local_version, True)
if version.parse(local_version) > version.parse(pypi_version)
else (pypi_version, False)
)


@click.command()
@click.option(
"--major",
Expand Down Expand Up @@ -132,10 +163,12 @@ def find_version(input_string):
@click.option(
"--canonicalize", flag_value=True, default=None, help="Canonicalize the new version"
)
@click.option(
"--pypi", flag_value=True, default=None, help="Get latest version from pypi.org"
)
@click.argument("input", type=click.File("rb"), default=None, required=False)
@click.argument("output", type=click.File("wb"), default=None, required=False)
def main(input, output, major, minor, patch, reset, pre, local, canonicalize):

def main(input, output, major, minor, patch, reset, pre, local, canonicalize, pypi):
config = configparser.RawConfigParser()
config.read([".bump", "setup.cfg"])

Expand All @@ -148,8 +181,22 @@ def main(input, output, major, minor, patch, reset, pre, local, canonicalize):
canonicalize = canonicalize or config.get("bump", "canonicalize", fallback=False)

contents = input.read().decode("utf-8")
lock = False
try:
version_string = find_version(contents)
if pypi:
pypi_version = get_latest_version(find_name(contents))
version_string, lock = compare_version(
version_string, get_latest_version(find_name(contents))
)
if lock:
click.echo(
"Local version is higher: {} > {}.\nLooks like bump was done manually.".format(
version_string, pypi_version
)
)
sys.exit(0)

except NoVersionFound:
click.echo("No version found in ./{}.".format(input.name))
sys.exit(1)
Expand All @@ -159,7 +206,7 @@ def main(input, output, major, minor, patch, reset, pre, local, canonicalize):
version_string = str(version)
if canonicalize:
version_string = canonicalize_version(version_string)
new = pattern.sub("\g<1>{}\g<3>".format(version_string), contents)
new = version_pattern.sub("\g<1>{}\g<3>".format(version_string), contents)
output.write(new.encode())
click.echo(version_string)

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="bump",
version="1.3.0",
version="1.4.0",
description="Bumps package version numbers",
long_description=open("README.rst").read(),
license="MIT",
Expand All @@ -28,6 +28,7 @@
"configparser ; python_version<'3'",
"first",
"packaging>=17.1",
"requests",
],
entry_points={"console_scripts": ["bump = bump:main"]},
)