1*f4a2713aSLionel Sambuc#!/usr/bin/env python 2*f4a2713aSLionel Sambuc 3*f4a2713aSLionel Sambuc"""CaptureCmd - A generic tool for capturing information about the 4*f4a2713aSLionel Sambucinvocations of another program. 5*f4a2713aSLionel Sambuc 6*f4a2713aSLionel SambucUsage 7*f4a2713aSLionel Sambuc-- 8*f4a2713aSLionel Sambuc1. Move the original tool to a safe known location. 9*f4a2713aSLionel Sambuc 10*f4a2713aSLionel Sambuc2. Link CaptureCmd to the original tool's location. 11*f4a2713aSLionel Sambuc 12*f4a2713aSLionel Sambuc3. Define CAPTURE_CMD_PROGRAM to the known location of the original 13*f4a2713aSLionel Sambuctool; this must be an absolute path. 14*f4a2713aSLionel Sambuc 15*f4a2713aSLionel Sambuc4. Define CAPTURE_CMD_DIR to a directory to write invocation 16*f4a2713aSLionel Sambucinformation to. 17*f4a2713aSLionel Sambuc""" 18*f4a2713aSLionel Sambuc 19*f4a2713aSLionel Sambucimport hashlib 20*f4a2713aSLionel Sambucimport os 21*f4a2713aSLionel Sambucimport sys 22*f4a2713aSLionel Sambucimport time 23*f4a2713aSLionel Sambuc 24*f4a2713aSLionel Sambucdef saveCaptureData(prefix, dir, object): 25*f4a2713aSLionel Sambuc string = repr(object) + '\n' 26*f4a2713aSLionel Sambuc key = hashlib.sha1(string).hexdigest() 27*f4a2713aSLionel Sambuc path = os.path.join(dir, 28*f4a2713aSLionel Sambuc prefix + key) 29*f4a2713aSLionel Sambuc if not os.path.exists(path): 30*f4a2713aSLionel Sambuc f = open(path, 'wb') 31*f4a2713aSLionel Sambuc f.write(string) 32*f4a2713aSLionel Sambuc f.close() 33*f4a2713aSLionel Sambuc return prefix + key 34*f4a2713aSLionel Sambuc 35*f4a2713aSLionel Sambucdef main(): 36*f4a2713aSLionel Sambuc program = os.getenv('CAPTURE_CMD_PROGRAM') 37*f4a2713aSLionel Sambuc dir = os.getenv('CAPTURE_CMD_DIR') 38*f4a2713aSLionel Sambuc fallback = os.getenv('CAPTURE_CMD_FALLBACK') 39*f4a2713aSLionel Sambuc if not program: 40*f4a2713aSLionel Sambuc raise ValueError('CAPTURE_CMD_PROGRAM is not defined!') 41*f4a2713aSLionel Sambuc if not dir: 42*f4a2713aSLionel Sambuc raise ValueError('CAPTURE_CMD_DIR is not defined!') 43*f4a2713aSLionel Sambuc 44*f4a2713aSLionel Sambuc # Make the output directory if it doesn't already exist. 45*f4a2713aSLionel Sambuc if not os.path.exists(dir): 46*f4a2713aSLionel Sambuc os.mkdir(dir, 0700) 47*f4a2713aSLionel Sambuc 48*f4a2713aSLionel Sambuc # Get keys for various data. 49*f4a2713aSLionel Sambuc env = os.environ.items() 50*f4a2713aSLionel Sambuc env.sort() 51*f4a2713aSLionel Sambuc envKey = saveCaptureData('env-', dir, env) 52*f4a2713aSLionel Sambuc cwdKey = saveCaptureData('cwd-', dir, os.getcwd()) 53*f4a2713aSLionel Sambuc argvKey = saveCaptureData('argv-', dir, sys.argv) 54*f4a2713aSLionel Sambuc entry = (time.time(), envKey, cwdKey, argvKey) 55*f4a2713aSLionel Sambuc saveCaptureData('cmd-', dir, entry) 56*f4a2713aSLionel Sambuc 57*f4a2713aSLionel Sambuc if fallback: 58*f4a2713aSLionel Sambuc pid = os.fork() 59*f4a2713aSLionel Sambuc if not pid: 60*f4a2713aSLionel Sambuc os.execv(program, sys.argv) 61*f4a2713aSLionel Sambuc os._exit(1) 62*f4a2713aSLionel Sambuc else: 63*f4a2713aSLionel Sambuc res = os.waitpid(pid, 0) 64*f4a2713aSLionel Sambuc if not res: 65*f4a2713aSLionel Sambuc os.execv(fallback, sys.argv) 66*f4a2713aSLionel Sambuc os._exit(1) 67*f4a2713aSLionel Sambuc os._exit(res) 68*f4a2713aSLionel Sambuc else: 69*f4a2713aSLionel Sambuc os.execv(program, sys.argv) 70*f4a2713aSLionel Sambuc os._exit(1) 71*f4a2713aSLionel Sambuc 72*f4a2713aSLionel Sambucif __name__ == '__main__': 73*f4a2713aSLionel Sambuc main() 74