#!/usr/bin/env python

import os, sys, errno

if len(sys.argv) < 3:
    print >> sys.stderr, 'Usage: %s TEMP_PATH_ID EXECUTABLE [ARGUMENTS]' % os.path.basename(sys.argv[0])
    print >> sys.stderr, 'Creates temporary directory temp_path_id, chdirs to it, and runs EXECUTABLE with optional ARGUMENTS.'
    sys.exit(2)

temp_path_id = sys.argv[1]
executable = sys.argv[2]
new_argv = sys.argv[2:] # Includes executable as new_argv[0].

try: os.makedirs(temp_path_id)
except OSError, e:
    if e.errno == errno.EEXIST:
        print >> sys.stderr, "Warning: `%s' exists." % temp_path_id
    else: raise

os.chdir(temp_path_id)
os.execvp(executable, new_argv)
