1########################################################################## 2# Copyright (c) 2009-2011, ETH Zurich. 3# All rights reserved. 4# 5# This file is distributed under the terms in the attached LICENSE file. 6# If you do not find this file, copies can be found by writing to: 7# ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group. 8########################################################################## 9 10import subprocess, os, sys 11 12QUIET = 0 13NORMAL = 1 14VERBOSE = 2 15DEBUG = 3 16 17current_level = NORMAL 18 19def message(level, message): 20 if level <= current_level: 21 sys.stdout.write(message + '\n') 22 sys.stdout.flush() 23 24def warning(s): 25 message(QUIET, 'Warning: ' + s) 26 27def error(s): 28 message(QUIET, 'Error: ' + s) 29 30def log(s): 31 message(NORMAL, s) 32 33def verbose(s): 34 message(VERBOSE, s) 35 36def debug(s): 37 message(DEBUG, s) 38 39def checkcmd(*args, **kwargs): 40 """Run a command as with subprocess.check_call, but either discard or 41 display the output, depending on the current debug level.""" 42 43 # display verbose message saying what we do 44 verbose('executing ' + ' '.join(args[0])) 45 46 # if non-debug mode, discard stdout 47 # if non-verbose mode, discard stderr as well (hides noise from cmake etc.) 48 devnull = None 49 if current_level < VERBOSE: 50 devnull = open(os.devnull, 'w') 51 kwargs['stderr'] = devnull 52 if current_level < DEBUG: 53 kwargs['stdout'] = devnull 54 55 subprocess.check_call(*args, **kwargs) 56 57 if devnull: 58 devnull.close() 59 60def addopts(p, dest): 61 p.add_option('-q', '--quiet', action='store_const', dest=dest, const=QUIET, 62 help='quiet output, only error messages and warnings') 63 p.add_option('-v', '--verbose', action='store_const', dest=dest, 64 const=VERBOSE, help='more verbose output') 65 p.add_option('--debug', action='store_const', dest=dest, const=DEBUG, 66 help='debug output, results of all commands') 67