Skip to content

Commit

Permalink
Merge pull request #72 from michaeljoseph/rm-sh-add-fabric
Browse files Browse the repository at this point in the history
-sh + fabric everywhere
  • Loading branch information
michaeljoseph committed Jul 14, 2014
2 parents 127911b + 12d674b commit 2283919
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 79 deletions.
5 changes: 3 additions & 2 deletions changes/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import logging
import tempfile

from fabric.api import local, settings
from path import path

import sh

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -32,7 +32,8 @@ def replace_attribute(module_name, attribute_name, new_value, dry_run=True):
if not dry_run:
path(tmp_file).move(init_file)
else:
log.debug(sh.diff(tmp_file, init_file, _ok_code=1))
with settings(warn_only=True):
log.info(local('diff %s %s' % (tmp_file, init_file)))


def has_attribute(module_name, attribute_name):
Expand Down
22 changes: 7 additions & 15 deletions changes/changelog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
import re

import sh
from fabric.api import local

from changes import attributes, config, version

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -39,7 +40,7 @@ def replace_sha_with_commit_link(git_log_content):
'__url__'
)

for index, line in enumerate(git_log_content):
for index, line in enumerate(git_log_content.split('\n')):
# http://stackoverflow.com/a/468378/5549
sha1_re = re.match(r'^[0-9a-f]{5,40}\b', line)
if sha1_re:
Expand Down Expand Up @@ -67,23 +68,14 @@ def changelog():

git_log_content = None
try:
git_log_content = sh.git.log(
'--oneline',
'--no-merges',
'--no-color',
'%s..master' % version.current_version(module_name),
_tty_out=False
).split('\n')

git_log = 'git log --oneline --no-merges --no-color'
git_log_content = local('%s %s..master' % (git_log, version.current_version(module_name)))
log.debug('content: %s' % git_log_content)
except:
log.warn('Error diffing previous version, initial release')

git_log_content = sh.git.log(
'--oneline',
'--no-merges',
'--no-color',
_tty_out=False
).split('\n')
git_log_content = local(git_log)

git_log_content = replace_sha_with_commit_link(git_log_content)

Expand Down
1 change: 1 addition & 0 deletions changes/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CHANGELOG = 'CHANGELOG.md'
REQUIREMENTS = 'requirements.txt'
arguments = {}


Expand Down
13 changes: 6 additions & 7 deletions changes/packaging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging

from path import path
import sh

from changes import config, shell, util, venv, verification

Expand All @@ -10,9 +9,9 @@

def install():
module_name, dry_run, new_version = config.common_arguments()
commands = ['setup.py', 'clean', 'sdist', 'bdist_wheel']
build_package_command = 'python setup.py clean sdist' # bdist_wheel'
try:
if (shell.handle_dry_run(sh.python, tuple(commands))):
if (shell.dry_run(build_package_command)):
with util.mktmpdir() as tmp_dir:
venv.create_venv(tmp_dir=tmp_dir)
for distribution in path('dist').files():
Expand All @@ -29,11 +28,11 @@ def upload():
module_name, dry_run, new_version = config.common_arguments()
pypi = config.arguments['--pypi']

upload_args = 'setup.py clean sdist upload'.split(' ')
upload_args = 'python setup.py clean sdist upload'
if pypi:
upload_args.extend(['-r', pypi])
upload_args += ' -r %s' % pypi

upload_result = shell.handle_dry_run(sh.python, tuple(upload_args))
upload_result = shell.dry_run(upload_args)
if not upload_result:
raise Exception('Error uploading: %s' % upload_result)
else:
Expand All @@ -53,7 +52,7 @@ def pypi():
package_index = pypi

try:
result = shell.execute(install_cmd, dry_run=dry_run)
result = shell.dry_run(install_cmd)
if result:
log.info('Successfully installed %s from %s',
module_name, package_index)
Expand Down
8 changes: 3 additions & 5 deletions changes/probe.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import logging
from os.path import exists

import sh
from fabric.api import local

from changes import attributes, config

log = logging.getLogger(__name__)

REQUIREMENTS = 'requirements.txt'


def get_requirements():
requirements_file = config.arguments.get('--requirements') or REQUIREMENTS
requirements_file = config.arguments.get('--requirements') or config.REQUIREMENTS
has_requirements = exists(requirements_file)
requirements = None
if has_requirements:
Expand All @@ -33,7 +31,7 @@ def probe_project(module_name):
"""
log.info('Checking project for changes requirements.')
# on [github](https://github.com)
git_remotes = sh.git.remote('-v')
git_remotes = local('git remote -v', capture=True).split('\n')
on_github = any(['github.com' in remote for remote in git_remotes])
log.info('On Github? %s', on_github)

Expand Down
20 changes: 5 additions & 15 deletions changes/shell.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import logging

from fabric.api import local

from changes import config
from changes.compat import check_output, CalledProcessError

log = logging.getLogger(__name__)


def handle_dry_run(fn, *args):
def dry_run(command):
if not config.arguments.get('--dry-run', True):
return fn(*args)
else:
log.debug('dry run of %s %s, skipping' % (fn, args))
return True


def execute(command, dry_run=True):
if not dry_run:
try:
return check_output(command.split(' ')).split('\n')
except CalledProcessError as e:
log.error('return code: %s, output: %s', e.returncode, e.output)
return False
return local(command, capture=False)
else:
log.debug('dry run of %s, skipping' % command)
return True
return True
22 changes: 5 additions & 17 deletions changes/vcs.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import logging

import sh

from changes import config, shell

log = logging.getLogger(__name__)


def commit_version_change():
module_name, dry_run, new_version = config.common_arguments()

shell.handle_dry_run(
sh.git.commit,
('-m', new_version, '%s/__init__.py' % module_name, config.CHANGELOG)
)

shell.handle_dry_run(sh.git.push, ())
module_name, _, new_version = config.common_arguments()
shell.dry_run('git commit -m "%s" %s/__init__.py %s' % (new_version, module_name, config.CHANGELOG))
shell.dry_run('git push')


def tag():
_, dry_run, new_version = config.common_arguments()

shell.handle_dry_run(
sh.git.tag,
('-a', new_version, '-m', '"%s"' % new_version)
)

shell.handle_dry_run(sh.git.push, ('--tags'))
shell.dry_run('git tag -a %s -m "%s"' % (new_version, new_version))
shell.dry_run('git push --tags')
13 changes: 6 additions & 7 deletions changes/verification.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

import sh
from fabric.api import local

from changes import config, shell

Expand All @@ -9,16 +9,15 @@

def run_tests():
if config.arguments['--tox']:
sh.tox()
result = local('tox')
else:
sh.nosetests()

return True
result = local('nosetests')
return result.succeeded


def run_test_command():
if config.arguments['--test-command']:
test_command = config.arguments['--test-command']
result = shell.execute(sh, tuple(test_command.split(' ')))
result = local(config.arguments['--test-command'])
log.info('Test command "%s", returned %s', test_command, result)
return result.succeeded
return True
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ path.py < 5.0.0
pinocchio < 1.0.0
python-coveralls < 3.0.0
semantic_version < 3.0.0
sh < 2.0.0
sphinx-bootstrap-theme < 1.0.0
sphinxcontrib-httpdomain < 2.0.0
testtube < 1.0.0
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
'docopt < 1.0.0',
'path.py < 5.0.0',
'semantic_version < 3.0.0',
'sh < 2.0.0',
'wheel < 1.0.0',
],
entry_points={
Expand Down
11 changes: 2 additions & 9 deletions tests/test_shell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sh
from unittest2 import TestCase

from changes import config, shell
Expand All @@ -10,17 +9,11 @@ def test_handle_dry_run(self):
config.arguments['--dry-run'] = False
self.assertEquals(
'',
shell.handle_dry_run(
sh.diff,
('README.md', 'README.md')
)
shell.dry_run('diff README.md README.md')
)

def test_handle_dry_run_true(self):
config.arguments['--dry-run'] = True
self.assertTrue(
shell.handle_dry_run(
sh.diff,
('README.md', 'README.md')
)
shell.dry_run('diff README.md README.md')
)

0 comments on commit 2283919

Please sign in to comment.