redirect_stdout.py 929 B

1234567891011121314151617181920212223242526272829
  1. # Copyright 2016 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. from __future__ import print_function
  5. import os
  6. import subprocess
  7. import sys
  8. # This script executes a command and redirects the stdout to a file. This is
  9. # equivalent to |command... > output_file|.
  10. #
  11. # Usage: python redirect_stdout.py output_file command...
  12. if __name__ == '__main__':
  13. if len(sys.argv) < 2:
  14. print("Usage: %s output_file command..." % sys.argv[0], file=sys.stderr)
  15. sys.exit(1)
  16. # This script is designed to run binaries produced by the current build. We
  17. # may prefix it with "./" to avoid picking up system versions that might
  18. # also be on the path.
  19. path = sys.argv[2]
  20. if not os.path.isabs(path):
  21. path = './' + path
  22. with open(sys.argv[1], 'w') as fp:
  23. sys.exit(subprocess.check_call([path] + sys.argv[3:], stdout=fp))