#!/usr/bin/env python

"""
stdcrit = stderr + critbot
https://github.com/denis-ryzhkov/critbot

@author Alexander Dolgushin <d-os@list.ru>
@author Denis Ryzhkov <denisr@denisr.com>
"""

usage = '''Usage:
    stdcrit /path/to/critbot_config.py /path/to/script.py arg...
'''

### import

from critbot import crit
import imp
import subprocess
import sys

### main

def main():

    if len(sys.argv) < 3:
        exit(usage)
    imp.load_source('critbot_config', sys.argv[1])

    command = sys.argv[2:]

    try:
        p = subprocess.Popen(command, stdin=sys.stdin, stdout=sys.stdout, stderr=subprocess.PIPE)
        out, err = p.communicate()
        if err:
            crit('{command} wrote to stderr:\n{err}'.format(command=command, err=err))

    except Exception:
        crit(also=command)

if __name__ == '__main__':
    main()
