1 #===----------------------------------------------------------------------===## 2 # 3 # The LLVM Compiler Infrastructure 4 # 5 # This file is dual licensed under the MIT and the University of Illinois Open 6 # Source Licenses. See LICENSE.TXT for details. 7 # 8 #===----------------------------------------------------------------------===## 9 10 from contextlib import contextmanager 11 import os 12 import tempfile 13 14 15 def cleanFile(filename): 16 try: 17 os.remove(filename) 18 except OSError: 19 pass 20 21 22 @contextmanager 23 def guardedTempFilename(suffix='', prefix='', dir=None): 24 # Creates and yeilds a temporary filename within a with statement. The file 25 # is removed upon scope exit. 26 handle, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir) 27 os.close(handle) 28 yield name 29 cleanFile(name) 30 31 32 @contextmanager 33 def guardedFilename(name): 34 # yeilds a filename within a with statement. The file is removed upon scope 35 # exit. 36 yield name 37 cleanFile(name) 38 39 40 @contextmanager 41 def nullContext(value): 42 # yeilds a variable within a with statement. No action is taken upon scope 43 # exit. 44 yield value 45 46 47 def makeReport(cmd, out, err, rc): 48 report = "Command: %s\n" % cmd 49 report += "Exit Code: %d\n" % rc 50 if out: 51 report += "Standard Output:\n--\n%s--\n" % out 52 if err: 53 report += "Standard Error:\n--\n%s--\n" % err 54 report += '\n' 55 return report 56