-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67fe635
commit da2f4e6
Showing
4 changed files
with
86 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__' | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters