import io
import logging
from os.path import exists

from plumbum.cmd import git

from changes import attributes, exceptions

log = logging.getLogger(__name__)


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__')
    )
    return report_and_raise(
        'Has module metadata',
        has_metadata,
        'Your %s/__init__.py must contain __version__ and __url__ attributes'
    )


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

@@ -1,14 +1,4 @@
-import io%0A
 impo
@@ -52,23 +52,66 @@
 mbum
-.cmd import git
+ import local%0Afrom plumbum.commands import CommandNotFound
 %0A%0Afr
@@ -184,24 +184,101 @@
 __name__)%0A%0A%0A
+TOOLS = %5B'git', 'diff', 'python'%5D%0ATEST_RUNNERS = %5B'pytest', 'nose', 'tox'%5D%0A%0A%0A
 def report_a
@@ -546,782 +546,380 @@
 e%0A%0A%0A
-def on_github():%0A    %22%22%22on %5Bgithub%5D(https://github.com)%22%22%22%0A    return report_and_raise(%0A        'On GitHub',%0A        any(%5B'github.com' in remote for remote in git('remote', '-v').split('%5Cn')%5D),%0A        'Your package needs to be a GitHub project'%0A    )%0A%0A%0Adef has_setup():%0A    %22%22%22%60setup.py%60%22%22%22%0A    return report_and_raise('Has a setup.py', exists('setup.py'), 'Your project needs a setup.py')%0A%0A# supports executing tests with %60py.test%60, %60nosetests%60 or %60tox%60%0ATEST_RUNNERS = %5B'pytest', 'nose', 'tox'%5D%0Adef has_requirements():%0A    %22%22%22%60requirements.txt%60 and required requirements%22%22%22%0A    has_requirements = report_and_raise('Has a requirements.txt', exists(context.requirements), 'Create a requirements.txt for your project')%0A%0A    requirements = io.open(context.requirements).read
+    return report_and_raise(%0A    )%0A%0A%0Adef has_setup():%0A    %22%22%22%60setup.py%60%22%22%22%0A    return report_and_raise('Has a setup.py', exists('setup.py'), 'Your project needs a setup.py')%0Adef has_binary(command):%0A    try:%0A        local.which(command)%0A        return True%0A    except CommandNotFound:%0A        log.info('%25s does not exist' %25 command)%0A        return False%0A%0A%0Adef has_tools
 ()
+:
 %0A   
@@ -930,202 +930,123 @@
 urn 
-report_and_raise(%0A        'Has a 
+any(%5Bhas_binary(tool) for tool in TOOLS%5D)%0A%0A%0Adef has_
 test
- 
+_
 runner
- (%25s)' %25 TEST_RUNNERS,%0A        any(%5Brunner in requirements for runner in TEST_RUNNERS%5D),%0A        'Please use one of the supported test runners (%25s)' %25
+():%0A    return any(%5Bhas_binary(runner) for runner in
  TES
@@ -1054,21 +1054,17 @@
 _RUNNERS
-%0A    
+%5D
 )%0A%0A%0Adef 
@@ -2047,43 +2047,16 @@
     
-if (on_github() and has_setup() and
+return (
 %0A   
@@ -2064,22 +2064,21 @@
     has_
-readme
+tools
 () and h
@@ -2080,25 +2080,21 @@
 and has_
-changelog
+setup
 () and h
@@ -2134,42 +2134,65 @@
 has_
-requirements):%0A        return True
+test_runner() and has_readme() and has_changelog()%0A    )%0A
 %0A   

Drop github and requirements.txt requirements