1 #!/usr/bin/env python 2 # 3 # Copyright 2014 The Chromium Authors. All rights reserved. 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 """ 8 Prepends a given file with a given line. This can be used to add a shebang line 9 to a generated file. 10 """ 11 12 import optparse 13 import os 14 import shutil 15 import sys 16 17 18 def main(): 19 parser = optparse.OptionParser() 20 parser.add_option('--input', help='The file to prepend the line to.') 21 parser.add_option('--line', help='The line to be prepended.') 22 parser.add_option('--output', help='The output file.') 23 24 options, _ = parser.parse_args() 25 input_path = options.input 26 output_path = options.output 27 line = options.line 28 29 # Warning - this reads all of the input file into memory. 30 with open(output_path, 'w') as output_file: 31 output_file.write(line + '\n') 32 with open(input_path, 'r') as input_file: 33 shutil.copyfileobj(input_file, output_file) 34 35 36 if __name__ == '__main__': 37 sys.exit(main()) 38