"""
send_output.py <site_name> <command>
Sends output of a command to the site admin.
"""
from qp.lib.site import Site
from qp.lib.util import get_output
from qp.mail.send import Email
from socket import getfqdn
import sys

def main(site_name, *args):
    publisher = Site(site_name).get_publisher()
    command = " ".join(args)
    output = get_output(command, include_stderr=True)
    if output:
        email = Email()
        email.set_subject(
            "%s %s %s" % (getfqdn(), site_name, command))
        email.set_body(output)
        if not email.send_as_is_without_conditions([publisher.get_webmaster_address()]):
            print("send failed")

if __name__ == '__main__':
    main(*sys.argv[1:])
