1# Copyright (c) 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import os.path
7import subprocess
8import sys
9
10if len(sys.argv) < 3:
11  print "Usage: %s OUTPUTFILE SCRIPTNAME ARGUMENTS" % sys.argv[0]
12  print "Re-execs the python interpreter against SCRIPTNAME with ARGS,"
13  print "redirecting output to OUTPUTFILE."
14  sys.exit(1)
15
16abs_outputfile = os.path.abspath(sys.argv[1])
17abs_outputdir = os.path.dirname(abs_outputfile)
18
19if not os.path.isdir(abs_outputdir):
20  os.makedirs(abs_outputdir)
21
22ret = 0
23
24with open(abs_outputfile, "w") as f:
25  ret = subprocess.Popen([sys.executable] + sys.argv[2:], stdout=f).wait()
26
27if ret:
28  os.remove(abs_outputfile)
29  sys.exit(ret)
30