Skip to content

Commit

Permalink
Fixed bumpversion script (python3 support)
Browse files Browse the repository at this point in the history
  • Loading branch information
Salem Harrache authored and SalemHarrache committed Jul 20, 2016
1 parent bac20c0 commit 898d295
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions scripts/bumpversion.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# coding: utf-8
from __future__ import unicode_literals, print_function
import sys
import os
import re

Expand All @@ -11,6 +12,45 @@
from argparse import RawTextHelpFormatter, ArgumentParser, FileType


if (sys.version_info[0] == 3):
bytes = bytes
str = type(u"")
basestring = (str, bytes)

def is_bytes(x):
return isinstance(x, (bytes, memoryview, bytearray))

else:
bytes = str
str = type(u"")
basestring = basestring

def is_bytes(x):
return isinstance(x, (buffer, bytearray))


def to_unicode(obj, encoding='utf-8'):
""" Convert ``obj`` to unicode"""
# unicode support
if isinstance(obj, str):
return obj

# bytes support
if is_bytes(obj):
if hasattr(obj, 'tobytes'):
return str(obj.tobytes(), encoding)
return str(obj, encoding)

# string support
if isinstance(obj, basestring):
if hasattr(obj, 'decode'):
return obj.decode(encoding)
else:
return str(obj, encoding)

return str(obj)


def generate_changelog_title(version):
version_title = "Version %s" % version
return version_title + "\n" + "-" * len(version_title)
Expand Down Expand Up @@ -45,9 +85,9 @@ def bump_release_version(args):
# would be. Useful side effect: exits if the working directory is not
# clean.
changelog = args.changelog.name
bumpver = subprocess.check_output(
bumpver = to_unicode(subprocess.check_output(
['bumpversion', 'release', '--dry-run', '--verbose'],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT))
m = re.search(r'Parsing version \'(\d+\.\d+\.\d+)\.dev0\'', bumpver)
current_version = m.groups(0)[0] + ".dev0"
m = re.search(r'New version will be \'(\d+\.\d+\.\d+)\'', bumpver)
Expand Down Expand Up @@ -109,9 +149,9 @@ def bump_new_version(args):
# clean.
changelog = args.changelog.name
part = args.part
bumpver = subprocess.check_output(
bumpver = to_unicode(subprocess.check_output(
['bumpversion', part, '--dry-run', '--verbose'],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT))
m = re.search(r'Parsing version \'(\d+\.\d+\.\d+)\'', bumpver)
current_version = m.groups(0)[0]
m = re.search(r'New version will be \'(\d+\.\d+\.\d+)\.dev0\'', bumpver)
Expand Down

0 comments on commit 898d295

Please sign in to comment.