Skip to content

Commit

Permalink
Refactor version
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljoseph committed Oct 16, 2013
1 parent da2f4e6 commit daeda05
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 46 deletions.
51 changes: 5 additions & 46 deletions changes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import virtualenv

import changes
from changes.util import extract, increment
from changes import attributes, probe, shell, util, version


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -84,47 +84,6 @@ def common_arguments():
)


def get_new_version(app_name, current_version,
major=False, minor=False, patch=True):

guess_new_version = increment(
current_version,
major=major,
minor=minor,
patch=patch
)

new_version = raw_input(
'What is the release version for "%s" '
'[Default: %s]: ' % (
app_name, guess_new_version
)
)
if not new_version:
new_version = guess_new_version
return new_version.strip()


def has_requirement(dependency, requirements_contents):
return any(
[dependency in requirement for requirement in requirements_contents]
)


def current_version(app_name):
return extract_attribute(app_name, '__version__')


def execute(command, dry_run=True):
log.debug('executing %s', commands)
if not dry_run:
try:
return subprocess.check_output(command.split(' ')).split('\n')
except subprocess.CalledProcessError, e:
log.debug('return code: %s, output: %s', e.returncode, e.output)
return False
else:
return True


def write_new_changelog(app_name, filename, content_lines, dry_run=True):
Expand Down Expand Up @@ -158,12 +117,12 @@ def changelog():

changelog_content = [
'\n## [%s](%s/compare/%s...%s)\n\n' % (
current_version(app_name), new_version,
new_version, attributes.extract_attribute(app_name, '__url__'),
version.current_version(app_name), new_version,
)
]
git_log = 'git log --oneline --no-merges'
version_difference = '%s..master' % current_version(app_name)
version_difference = '%s..master' % version.current_version(app_name)

git_log_content = execute('%s %s' % (git_log, version_difference),
dry_run=False)
Expand Down Expand Up @@ -406,9 +365,9 @@ def main():
for command in commands:
if arguments[command]:
if command not in suppress_version_prompt_for:
arguments['new_version'] = get_new_version(
arguments['new_version'] = version.get_new_version(
app_name,
current_version(app_name),
version.current_version(app_name),
**extract_version_arguments()
)
globals()[command]()
67 changes: 67 additions & 0 deletions changes/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging

import semantic_version

from changes import util, attributes


log = logging.getLogger(__name__)


def strip_long_arguments(arguments, long_keys):
long_arguments = util.extract(arguments, long_keys)
return dict([
(key[2:], value) for key, value in long_arguments.items()
])


def extract_version_arguments():
return strip_long_arguments(['--major', '--minor', '--patch'])

def increment(version, major=False, minor=False, patch=True):
"""
Increment a semantic version
:param version: str of the version to increment
:param major: bool specifying major level version increment
:param minor: bool specifying minor level version increment
:param patch: bool specifying patch level version increment
:return: str of the incremented version
"""
version = semantic_version.Version(version)
if major:
version.major += 1
version.minor = 0
version.patch = 0
elif minor:
version.minor += 1
version.patch = 0
elif patch:
version.patch += 1

return str(version)


def get_new_version(app_name, current_version,
major=False, minor=False, patch=True):

guess_new_version = increment(
current_version,
major=major,
minor=minor,
patch=patch
)

new_version = raw_input(
'What is the release version for "%s" '
'[Default: %s]: ' % (
app_name, guess_new_version
)
)
if not new_version:
new_version = guess_new_version
return new_version.strip()


def current_version(app_name):
return attributes.extract_attribute(app_name, '__version__')
47 changes: 47 additions & 0 deletions tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from unittest2 import TestCase

from changes import version
from . import BaseTestCase

class VersionTestCase(TestCase):

def test_increment(self):
self.assertEquals(
'1.0.0',
version.increment('0.0.1', major=True)
)

self.assertEquals(
'0.1.0',
version.increment('0.0.1', minor=True)
)

self.assertEquals(
'1.0.1',
version.increment('1.0.0', patch=True)
)

def test_strip_long_arguments(self):
arguments = {
'--major': True,
'--minor': False,
'--patch': False,
}
long_keys = ['--major', '--minor', '--patch']
expected = {
'major': True,
'minor': False,
'patch': False,
}
self.assertEquals(
expected,
version.strip_long_arguments(arguments, long_keys)
)

class CurrentVersionTestCase(BaseTestCase):

def test_current_version(self):
self.assertEquals(
'0.0.1',
version.current_version(self.module_name)
)

0 comments on commit daeda05

Please sign in to comment.