Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break out each check for better output granularity #86

Merged
merged 1 commit into from
Oct 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ProbeException(Exception):
pass
99 changes: 65 additions & 34 deletions changes/probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,80 @@

from plumbum.cmd import git

from changes import attributes
from changes import attributes, exceptions

log = logging.getLogger(__name__)


def probe_project(context):
"""
Check if the project meets `changes` requirements
"""
log.info('Checking project for changes requirements.')
# on [github](https://github.com)
git_remotes = git('remote', '-v').split('\n')
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(context.requirements)
if has_requirements:
# supports executing tests with `py.test`, `nosetests` or `tox`
requirements = io.open(context.requirements).read()
test_runners = ['pytest', 'nose', 'tox']
runs_tests = any([runner in requirements for runner in test_runners])
log.info('Runs tests? %s' % runs_tests)

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

# `<module_name>/__init__.py` with `__version__` and `__url__`
def report_and_raise(probe_name, probe_result, failure_msg):
"""Logs the probe result and raises on failure"""
log.info('%s? %s' % (probe_name, probe_result))
if not probe_result:
raise exceptions.ProbeException(failure_msg)
else:
return True


def on_github():
"""on [github](https://github.com)"""
return report_and_raise(
'On GitHub',
any(['github.com' in remote for remote in git('remote', '-v').split('\n')]),
'Your package needs to be a GitHub project'
)


def has_setup():
"""`setup.py`"""
return report_and_raise('Has a setup.py', exists('setup.py'), 'Your project needs a setup.py')

# supports executing tests with `py.test`, `nosetests` or `tox`
TEST_RUNNERS = ['pytest', 'nose', 'tox']
def has_requirements():
"""`requirements.txt` and required requirements"""
has_requirements = report_and_raise('Has a requirements.txt', exists(context.requirements), 'Create a requirements.txt for your project')

requirements = io.open(context.requirements).read()
return report_and_raise(
'Has a test runner (%s)' % TEST_RUNNERS,
any([runner in requirements for runner in TEST_RUNNERS]),
'Please use one of the supported test runners (%s)' % TEST_RUNNERS
)


def has_changelog():
"""CHANGELOG.md"""
return report_and_raise('CHANGELOG.md', exists('CHANGELOG.md'), 'Create a CHANGELOG.md file')


def has_readme():
"""README.md"""
return report_and_raise('README.md', exists('README.md'), 'Create a README.md file')


def has_metadata(context):
"""`<module_name>/__init__.py` with `__version__` and `__url__`"""
init_path = '%s/__init__.py' % context.module_name
has_metadata = (
exists(init_path) and
attributes.has_attribute(context.module_name, '__version__') and
attributes.has_attribute(context.module_name, '__url__')
)
log.info('Has module metadata? %s', has_metadata)
return report_and_raise(
'Has module metadata',
has_metadata,
'Your %s/__init__.py must contain __version__ and __url__ attributes'
)

if not (on_github and setup and has_changelog and has_metadata and
has_requirements and runs_tests):
raise Exception('Project does not meet `changes` requirements')
else:

def probe_project(context):
"""
Check if the project meets `changes` requirements.
Complain and exit otherwise.
"""
log.info('Checking project for changes requirements.')
if (on_github() and has_setup() and
has_readme() and has_changelog() and has_metadata(context) and
has_requirements):
return True
return False