Skip to content

Commit

Permalink
Refactor project attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljoseph committed Oct 16, 2013
1 parent 67fe635 commit da2f4e6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 58 deletions.
45 changes: 45 additions & 0 deletions changes/attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import ast
import logging
import tempfile

from path import path

from changes import shell

log = logging.getLogger(__name__)


def extract_attribute(app_name, attribute_name):
"""Extract metatdata property from a module"""
with open('%s/__init__.py' % app_name) as input_file:
for line in input_file:
if line.startswith(attribute_name):
return ast.literal_eval(line.split('=')[1].strip())


def replace_attribute(app_name, attribute_name, new_value, dry_run=True):
init_file = '%s/__init__.py' % app_name
_, tmp_file = tempfile.mkstemp()

with open(init_file) as input_file:
with open(tmp_file, 'w') as output_file:
for line in input_file:
if line.startswith(attribute_name):
line = "%s = '%s'\n" % (attribute_name, new_value)

output_file.write(line)

if not dry_run:
path(tmp_file).move(init_file)
else:
log.debug(shell.execute(
'diff %s %s' % (tmp_file, init_file),
dry_run=False
))


def has_attribute(app_name, attribute_name):
init_file = '%s/__init__.py' % app_name
return any(
[attribute_name in init_line for init_line in open(init_file).readlines()]
)
41 changes: 4 additions & 37 deletions changes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,39 +105,6 @@ def get_new_version(app_name, current_version,
return new_version.strip()


def extract_attribute(app_name, attribute_name):
"""Extract metatdata property from a module"""
with open('%s/__init__.py' % app_name) as input_file:
for line in input_file:
if line.startswith(attribute_name):
return ast.literal_eval(line.split('=')[1].strip())


def replace_attribute(app_name, attribute_name, new_value, dry_run=True):
init_file = '%s/__init__.py' % app_name
_, tmp_file = tempfile.mkstemp()

with open(init_file) as input_file:
with open(tmp_file, 'w') as output_file:
for line in input_file:
if line.startswith(attribute_name):
line = "%s = '%s'\n" % (attribute_name, new_value)

output_file.write(line)

if not dry_run:
path(tmp_file).move(init_file)
else:
log.debug(execute('diff %s %s' % (tmp_file, init_file), dry_run=False))


def has_attribute(app_name, attribute_name):
init_file = '%s/__init__.py' % app_name
return any(
[attribute_name in init_line for init_line in open(init_file).readlines()]
)


def has_requirement(dependency, requirements_contents):
return any(
[dependency in requirement for requirement in requirements_contents]
Expand All @@ -163,7 +130,7 @@ def execute(command, dry_run=True):
def write_new_changelog(app_name, filename, content_lines, dry_run=True):
heading_and_newline = (
'# [Changelog](%s/releases)\n' %
extract_attribute(app_name, '__url__')
attributes.extract_attribute(app_name, '__url__')
)

with open(filename, 'r+') as f:
Expand Down Expand Up @@ -191,8 +158,8 @@ def changelog():

changelog_content = [
'\n## [%s](%s/compare/%s...%s)\n\n' % (
new_version, extract_attribute(app_name, '__url__'),
current_version(app_name), new_version,
new_version, attributes.extract_attribute(app_name, '__url__'),
)
]
git_log = 'git log --oneline --no-merges'
Expand All @@ -215,7 +182,7 @@ def changelog():
sha1,
'[%s](%s/commit/%s)' % (
sha1,
extract_attribute(app_name, '__url__'),
attributes.extract_attribute(app_name, '__url__'),
sha1
)
)
Expand All @@ -241,7 +208,7 @@ def changelog():
def version():
app_name, dry_run, new_version = common_arguments()

replace_attribute(
attributes.replace_attribute(
app_name,
'__version__',
new_version,
Expand Down
37 changes: 37 additions & 0 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from changes import attributes

from . import BaseTestCase


class AttributeTestCase(BaseTestCase):


def test_extract_attribute(self):
self.assertEquals(
'0.0.1',
attributes.extract_attribute('test_app', '__version__')
)

def test_replace_attribute(self):
attributes.replace_attribute(
'test_app',
'__version__',
'1.0.0',
dry_run=False
)

expected_content = list(self.initial_init_content)
expected_content[2] = "__version__ = '1.0.0'"
self.assertEquals(
'\n'.join(expected_content),
''.join(
open(self.tmp_file).readlines()
)
)

def test_has_attribute(self):
self.assertTrue(
attributes.has_attribute(
self.module_name,
'__version__'
))
21 changes: 0 additions & 21 deletions tests/test_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,7 @@ def test_extract(self):
)
)

def test_extract_attribute(self):
self.assertEquals(
'0.0.1',
changes.extract_attribute('test_app', '__version__')
)

def test_replace_attribute(self):
changes.replace_attribute(
'test_app',
'__version__',
'1.0.0',
dry_run=False
)

expected_content = list(self.initial_init_content)
expected_content[2] = "__version__ = '1.0.0'"
self.assertEquals(
'\n'.join(expected_content),
''.join(
open(self.tmp_file).readlines()
)
)

def test_increment(self):
self.assertEquals(
Expand Down

0 comments on commit da2f4e6

Please sign in to comment.