##############################################################################
#
# Copyright (c) 2013 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Support Utilities
"""
import subprocess
import sys

def do(cmd, cwd=None, print_stdout=True, print_cmd=True, ignore_exit=False):
    if print_cmd:
        print(' '.join(cmd))
    p = subprocess.Popen(
        cmd,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        cwd=cwd)
    out, _ = p.communicate()
    if out and print_stdout:
        print(out.decode())
    if p.returncode != 0:
        print('Failed command:')
        print(' '.join(cmd))
        if out:
            print(out.decode())
        if not ignore_exit:
            sys.exit(p.returncode)
