Skip to content

Commit

Permalink
Refactor probe
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljoseph committed Oct 16, 2013
1 parent 9606c27 commit 57e1db6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 44 deletions.
45 changes: 1 addition & 44 deletions changes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,48 +283,6 @@ def release():
log.exception('Error releasing')


def probe_project():
"""
Check if the project meets `changes` requirements
"""
app_name = arguments['<app_name>']

log.info('Checking project for changes requirements..self.')
# on [github](https://github.com)
git_remotes = execute('git remote -v', dry_run=False)
on_github = any(['github.com' in remote for remote in git_remotes])
log.info('On Github? %s', on_github)

# `setup.py`
setup = exists('setup.py')
log.info('setup.py? %s', setup)

# * `requirements.txt`
has_requirements = exists('requirements.txt')
log.info('requirements.txt? %s', setup)
requirements_contents = open('requirements.txt').readlines()

# * `CHANGELOG.md`
has_changelog = exists('CHANGELOG.md')
log.info('CHANGELOG.md? %s', has_changelog)

# * `<app_name>/__init__.py` with `__version__` and `__url__`
init_path = '%s/__init__.py' % app_name
init_contents = open(init_path).readlines()
has_metadata = (exists(init_path) and has_attribute(app_name, '__version__')
and has_attribute(app_name, '__url__'))
log.info('Has module metadata? %s', has_metadata)

# * supports executing tests with [`nosetests`][2] or [`tox`][3]
log.debug(requirements_contents)
runs_tests = (has_requirement('nose', requirements_contents) or
has_requirement('tox', requirements_contents))
log.info('Runs tests? %s' % runs_tests)

return (on_github and setup and has_changelog and has_metadata and
has_requirements and runs_tests)


def initialise():
global arguments
arguments = docopt(__doc__, version=changes.__version__)
Expand All @@ -345,10 +303,9 @@ def main():

app_name = arguments['<app_name>']

if not probe_project():
if not probe.probe_project(app_name):
raise Exception('Project does not meet `changes` requirements')


for command in commands:
if arguments[command]:
if command not in suppress_version_prompt_for:
Expand Down
56 changes: 56 additions & 0 deletions changes/probe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import logging
from os.path import exists

from changes import attributes, shell

log = logging.getLogger(__name__)


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


def probe_project(app_name):
"""
Check if the project meets `changes` requirements
"""
log.info('Checking project for changes requirements..self.')
# on [github](https://github.com)
git_remotes = shell.execute('git remote -v', dry_run=False)
on_github = any(['github.com' in remote for remote in git_remotes])
log.info('On Github? %s', on_github)

# `setup.py`
setup = exists('setup.py')
log.info('setup.py? %s', setup)

# `requirements.txt`
has_requirements = exists('requirements.txt')
log.info('requirements.txt? %s', setup)
requirements_contents = open('requirements.txt').readlines()

# `CHANGELOG.md`
has_changelog = exists('CHANGELOG.md')
log.info('CHANGELOG.md? %s', has_changelog)

# `<app_name>/__init__.py` with `__version__` and `__url__`
init_path = '%s/__init__.py' % app_name
has_metadata = (
exists(init_path) and
attributes.has_attribute(app_name, '__version__') and
attributes.has_attribute(app_name, '__url__')
)
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)
)
log.info('Runs tests? %s' % runs_tests)

return (on_github and setup and has_changelog and has_metadata and
has_requirements and runs_tests)
8 changes: 8 additions & 0 deletions tests/test_probe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from changes import probe
from . import BaseTestCase


class ProbeTestCase(BaseTestCase):

def test_probe_project(self):
self.assertTrue(probe.probe_project(self.module_name))

0 comments on commit 57e1db6

Please sign in to comment.