# Path: bts_tools/core.py
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
#     r = _run(cmd, capture_io=capture_io, verbose=verbose,
#              run_dir=run_dir, forward_signals=forward_signals, pidfile=pidfile, shell=shell)
#     if r.status != 0:
#         if log_on_fail:
#             log.warning('Failed running: %s' % cmd)
#             if capture_io:
#                 log.warning('\nSTDOUT:\n{}\nSTDERR:\n:{}'.format(r.stdout, r.stderr))
#         raise RuntimeError('Failed running: %s' % cmd)
#     return r


from bts_tools.core import run
from jinja2 import Template
import sys
import re
    """Build the Sphinx documentation and open it in a web browser"""
    # FIXME: add step to call gen_config_yaml_doc and integrate it
    return {'actions': ['cd docs; make html',
                        open_file('docs/_build/html/index.html')]}


def task_pypi_doc():
    """Build the main page that will be uploaded to PyPI and open it in a web browser"""
    return {'actions': ['python setup.py --long-description | rst2html.py > /tmp/bts_tools_pypi_doc.html',
                        open_file('/tmp/bts_tools_pypi_doc.html')]}


def task_gen_config_yaml_doc():
    """Build the config.yaml documentation from the comments in the provided default config.yaml file"""

    def read_file(filename):
        lines = open(filename).readlines()
        # shift it to the right so it fits in a code block
        return ''.join(['    {}'.format(l) for l in lines])

    # defined as inner function so as not to run it during task definition (ie: every time we call "doit")
    def update_config_yaml_doc():
        config_yaml = read_file('bts_tools/config.yaml')
        config_feeds_yaml = read_file('bts_tools/config_feeds.yaml')
        t = Template(open('docs/config_yaml.rst.jinja').read()).render(config_yaml=config_yaml,
                                                                       config_feeds_yaml=config_feeds_yaml)
        with open('docs/config_yaml.rst', 'w') as out:
            out.write(t)

    def update_cmdline_help():

# Path: bts_tools/core.py
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
#     r = _run(cmd, capture_io=capture_io, verbose=verbose,
#              run_dir=run_dir, forward_signals=forward_signals, pidfile=pidfile, shell=shell)
#     if r.status != 0:
#         if log_on_fail:
#             log.warning('Failed running: %s' % cmd)
#             if capture_io:
#                 log.warning('\nSTDOUT:\n{}\nSTDERR:\n:{}'.format(r.stdout, r.stderr))
#         raise RuntimeError('Failed running: %s' % cmd)
#     return r


# Path: dodo.py
from bts_tools.core import run
from jinja2 import Template
import sys
import re

    """Build the Sphinx documentation and open it in a web browser"""
    # FIXME: add step to call gen_config_yaml_doc and integrate it
    return {'actions': ['cd docs; make html',
                        open_file('docs/_build/html/index.html')]}


def task_pypi_doc():
    """Build the main page that will be uploaded to PyPI and open it in a web browser"""
    return {'actions': ['python setup.py --long-description | rst2html.py > /tmp/bts_tools_pypi_doc.html',
                        open_file('/tmp/bts_tools_pypi_doc.html')]}


def task_gen_config_yaml_doc():
    """Build the config.yaml documentation from the comments in the provided default config.yaml file"""

    def read_file(filename):
        lines = open(filename).readlines()
        # shift it to the right so it fits in a code block
        return ''.join(['    {}'.format(l) for l in lines])

    # defined as inner function so as not to run it during task definition (ie: every time we call "doit")
    def update_config_yaml_doc():
        config_yaml = read_file('bts_tools/config.yaml')
        config_feeds_yaml = read_file('bts_tools/config_feeds.yaml')
        t = Template(open('docs/config_yaml.rst.jinja').read()).render(config_yaml=config_yaml,
                                                                       config_feeds_yaml=config_feeds_yaml)
        with open('docs/config_yaml.rst', 'w') as out:
            out.write(t)

    def update_cmdline_help():

        lines = run('bts -h', capture_io=True).stdout.split('\n')