Skip to content

Commit

Permalink
Merge pull request #46 from michaeljoseph/replace-iterpipes-with-sh
Browse files Browse the repository at this point in the history
Replace iterpipes with sh
  • Loading branch information
michaeljoseph committed Oct 24, 2013
2 parents 94f3415 + bb935fa commit 5ff67f5
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 104 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Lint the project with:

Generate the documentation with:

cd docs && PYTHONPATH=.. make singlehtml
(cd docs && make singlehtml)

To monitor changes to Python files and execute flake8 and nosetests
automatically, execute the following from the root project directory:
Expand Down
7 changes: 2 additions & 5 deletions changes/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from path import path

from changes import shell
import sh

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -32,10 +32,7 @@ def replace_attribute(app_name, attribute_name, new_value, dry_run=True):
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
))
log.debug(sh.diff(tmp_file, init_file, _ok_code=1))


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

from changes import attributes, config, shell, version
import sh
from changes import attributes, config, version

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -64,21 +65,21 @@ def changelog():
)
]

## vcs (todo: config templatise all these commands)
git_log = 'git log --oneline --no-merges'
version_difference = '%s..master' % version.current_version(app_name)

git_log_content = shell.execute(
'%s %s' % (git_log, version_difference),
dry_run=False
)
git_log_content = sh.git.log(
'--oneline',
'--no-merges',
'%s..master' % version.current_version(app_name),
_tty_out=False
).split('\n')
log.debug('content: %s' % git_log_content)

if not git_log_content:
log.debug('sniffing initial release, drop tags: %s', git_log)
git_log_content = shell.execute(git_log, dry_run=False)

## /vcs
log.debug('sniffing initial release, drop tags')
git_log_content = sh.git.log(
'--oneline',
'--no-merges',
_tty_out=False
).split('\n')

git_log_content = replace_sha_with_commit_link(git_log_content)

Expand All @@ -87,10 +88,9 @@ def changelog():
# makes change log entries into bullet points
if git_log_content:
[
changelog_content.append('* %s' % line)
changelog_content.append('* %s\n' % line)
if line else line
for line in git_log_content[:-1]
# for all except the last line?
]

write_new_changelog(
Expand Down
10 changes: 6 additions & 4 deletions changes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
from docopt import docopt

import changes
from changes import probe
from changes import config, probe, util, version
from changes.config import arguments
from changes.changelog import changelog
from changes.packaging import install, upload, pypi
from changes.testing import run_tests
from changes.verification import run_tests
from changes.version import bump_version
from changes.vcs import tag, commit_version_change

Expand All @@ -61,7 +61,7 @@ def release():
if not arguments['--skip-changelog']:
changelog()
bump_version()
test()
run_tests()
commit_version_change()
install()
upload()
Expand All @@ -83,9 +83,11 @@ def initialise():
def main():
arguments = initialise()

version_arguments = ['--major', '--minor', '--patch']
commands = ['release', 'changelog', 'run_tests', 'bump_version', 'tag',
'upload', 'install', 'pypi']
suppress_version_prompt_for = ['run_tests', 'upload']

if arguments['--new-version']:
arguments['new_version'] = arguments['--new-version']

Expand All @@ -100,6 +102,6 @@ def main():
arguments['new_version'] = version.get_new_version(
app_name,
version.current_version(app_name),
**version.extract_version_arguments(arguments)
**util.extract_arguments(arguments, version_arguments)
)
globals()[command]()
23 changes: 23 additions & 0 deletions changes/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from subprocess import Popen, PIPE, CalledProcessError


def check_output(*popenargs, **kwargs):
"""
Run command with arguments and return its output as a byte string.
Backported from Python 2.7 as it's implemented as pure python on stdlib.
>>> check_output(['/usr/bin/python', '--version'])
Python 2.6.2
"""
process = Popen(stdout=PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
40 changes: 24 additions & 16 deletions changes/packaging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import virtualenv
import logging
import tempfile

from path import path
import sh
import virtualenv

from changes import config, shell, testing
from changes import config, shell, verification

log = logging.getLogger(__name__)


def make_virtualenv():
Expand All @@ -13,19 +19,19 @@ def make_virtualenv():
def install():
app_name, dry_run, new_version = config.common_arguments()

result = shell.execute('python setup.py clean sdist', dry_run=dry_run)
result = shell.handle_dry_run(sh.python, ('setup.py', 'clean', 'sdist'))
if result:
tmp_dir = make_virtualenv()
try:
virtualenv.install_sdist(
arguments['<app_name>'],
config.arguments['<app_name>'],
'dist/%s-%s.tar.gz' % (app_name, new_version),
'%s/bin/python' % tmp_dir
)
log.info('Successfully installed %s sdist', app_name)
if testing.run_test_command():
if verification.run_test_command():
log.info('Successfully ran test command: %s',
arguments['--test-command'])
config.arguments['--test-command'])
except:
raise Exception('Error installing %s sdist', app_name)

Expand All @@ -34,41 +40,43 @@ def install():

def upload():
app_name, dry_run, new_version = config.common_arguments()
pypi = arguments['--pypi']
pypi = config.arguments['--pypi']

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

if not shell.execute(upload, dry_run=dry_run):
upload_result = shell.handle_dry_run(sh.python, tuple(upload_args))
if not upload_result:
raise Exception('Error uploading')
else:
log.info('Succesfully uploaded %s %s', app_name, new_version)


def pypi():
app_name, dry_run, _ = config.common_arguments()
pypi = arguments['--pypi']
package_index = 'pypi'

tmp_dir = make_virtualenv()
install = '%s/bin/pip install %s' % (tmp_dir, app_name)
install_cmd = '%s/bin/pip install %s' % (tmp_dir, app_name)

package_index = 'pypi'
pypi = config.arguments['--pypi']
if pypi:
install = install + '-i %s' % pypi
install_cmd += '-i %s' % pypi
package_index = pypi

try:
result = shell.execute(install, dry_run=dry_run)
result = shell.execute(install_cmd, dry_run=dry_run)
if result:
log.info('Successfully installed %s from %s',
app_name, package_index)
else:
log.error('Failed to install %s from %s',
app_name, package_index)

testing.run_test_command()
verification.run_test_command()
except:
log.exception('error installing %s from %s', app_name, package_index)
raise Exception('Error installing %s from %s', app_name, package_index)

path(tmp_dir).rmtree(path(tmp_dir))
7 changes: 4 additions & 3 deletions changes/probe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import logging
from os.path import exists

from changes import attributes, shell
import sh

from changes import attributes

log = logging.getLogger(__name__)

Expand All @@ -18,7 +20,7 @@ def probe_project(app_name):
"""
log.info('Checking project for changes requirements.')
# on [github](https://github.com)
git_remotes = shell.execute('git remote -v', dry_run=False)
git_remotes = sh.git.remote('-v')
on_github = any(['github.com' in remote for remote in git_remotes])
log.info('On Github? %s', on_github)

Expand All @@ -45,7 +47,6 @@ def probe_project(app_name):
log.info('Has module metadata? %s', has_metadata)

# supports executing tests with `nosetests` or `tox`
log.debug(requirements_contents)
runs_tests = (
has_requirement('nose', requirements_contents) or
has_requirement('tox', requirements_contents)
Expand Down
17 changes: 13 additions & 4 deletions changes/shell.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import logging

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

log = logging.getLogger(__name__)


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


def execute(command, dry_run=True):
log.debug('executing %s', command)
if not dry_run:
try:
return [result for result in iterpipes.linecmd(command)(None)]
except iterpipes.CalledProcessError, e:
return check_output(command.split(' ')).split('\n')
except CalledProcessError as e:
log.debug('return code: %s, output: %s', e.returncode, e.output)
return False
else:
log.debug('dry run of %s, skipping' % command)
return True
24 changes: 0 additions & 24 deletions changes/testing.py

This file was deleted.

31 changes: 22 additions & 9 deletions changes/vcs.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import logging

import sh

from changes import config, shell

log = logging.getLogger(__name__)


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

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

if not (shell.execute(command, dry_run=dry_run) and
shell.execute('git push', dry_run=dry_run)):
raise Exception('Version change commit failed')
if commit_result:
push_result = shell.handle_dry_run(sh.git.push, ())
if not push_result:
raise Exception('Version change commit failed')


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

shell.execute(
'git tag -a %s -m "%s"' % (new_version, new_version),
dry_run=dry_run
tag_result = shell.handle_dry_run(
sh.git.tag,
('-a', new_version, '-m', '"%s"' % new_version)
)
shell.execute('git push --tags', dry_run=dry_run)

if tag_result:
push_tags_result = shell.handle_dry_run(sh.git.push, ('--tags'))
if not push_tags_result:
raise Exception('Tagging failed')
Loading

0 comments on commit 5ff67f5

Please sign in to comment.