llvm-project
73 строки · 1.9 Кб
1#!/usr/bin/env python
2
3"""CaptureCmd - A generic tool for capturing information about the
4invocations of another program.
5
6Usage
7--
81. Move the original tool to a safe known location.
9
102. Link CaptureCmd to the original tool's location.
11
123. Define CAPTURE_CMD_PROGRAM to the known location of the original
13tool; this must be an absolute path.
14
154. Define CAPTURE_CMD_DIR to a directory to write invocation
16information to.
17"""
18
19import hashlib20import os21import sys22import time23
24def saveCaptureData(prefix, dir, object):25string = repr(object) + '\n'26key = hashlib.sha1(string).hexdigest()27path = os.path.join(dir,28prefix + key)29if not os.path.exists(path):30f = open(path, 'wb')31f.write(string)32f.close()33return prefix + key34
35def main():36program = os.getenv('CAPTURE_CMD_PROGRAM')37dir = os.getenv('CAPTURE_CMD_DIR')38fallback = os.getenv('CAPTURE_CMD_FALLBACK')39if not program:40raise ValueError('CAPTURE_CMD_PROGRAM is not defined!')41if not dir:42raise ValueError('CAPTURE_CMD_DIR is not defined!')43
44# Make the output directory if it doesn't already exist.45if not os.path.exists(dir):46os.mkdir(dir, 0700)47
48# Get keys for various data.49env = os.environ.items()50env.sort()51envKey = saveCaptureData('env-', dir, env)52cwdKey = saveCaptureData('cwd-', dir, os.getcwd())53argvKey = saveCaptureData('argv-', dir, sys.argv)54entry = (time.time(), envKey, cwdKey, argvKey)55saveCaptureData('cmd-', dir, entry)56
57if fallback:58pid = os.fork()59if not pid:60os.execv(program, sys.argv)61os._exit(1)62else:63res = os.waitpid(pid, 0)64if not res:65os.execv(fallback, sys.argv)66os._exit(1)67os._exit(res)68else:69os.execv(program, sys.argv)70os._exit(1)71
72if __name__ == '__main__':73main()74