3 """CaptureCmd - A generic tool for capturing information about the
4 invocations of another program.
8 1. Move the original tool to a safe known location.
10 2. Link CaptureCmd to the original tool's location.
12 3. Define CAPTURE_CMD_PROGRAM to the known location of the original
13 tool; this must be an absolute path.
15 4. Define CAPTURE_CMD_DIR to a directory to write invocation
24 def saveCaptureData(prefix
, dir, object):
25 string
= repr(object) + '\n'
26 key
= hashlib
.sha1(string
).hexdigest()
27 path
= os
.path
.join(dir,
29 if not os
.path
.exists(path
):
36 program
= os
.getenv('CAPTURE_CMD_PROGRAM')
37 dir = os
.getenv('CAPTURE_CMD_DIR')
38 fallback
= os
.getenv('CAPTURE_CMD_FALLBACK')
40 raise ValueError('CAPTURE_CMD_PROGRAM is not defined!')
42 raise ValueError('CAPTURE_CMD_DIR is not defined!')
44 # Make the output directory if it doesn't already exist.
45 if not os
.path
.exists(dir):
48 # Get keys for various data.
49 env
= os
.environ
.items()
51 envKey
= saveCaptureData('env-', dir, env
)
52 cwdKey
= saveCaptureData('cwd-', dir, os
.getcwd())
53 argvKey
= saveCaptureData('argv-', dir, sys
.argv
)
54 entry
= (time
.time(), envKey
, cwdKey
, argvKey
)
55 saveCaptureData('cmd-', dir, entry
)
60 os
.execv(program
, sys
.argv
)
63 res
= os
.waitpid(pid
, 0)
65 os
.execv(fallback
, sys
.argv
)
69 os
.execv(program
, sys
.argv
)
72 if __name__
== '__main__':