Home | History | Annotate | Download | only in build
      1 #!/usr/bin/env python
      2 # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3 #
      4 # Use of this source code is governed by a BSD-style license
      5 # that can be found in the LICENSE file in the root of the source
      6 # tree. An additional intellectual property rights grant can be found
      7 # in the file PATENTS.  All contributing project authors may
      8 # be found in the AUTHORS file in the root of the source tree.
      9 
     10 """ Adds extra patterns to the root .gitignore file.
     11 
     12 Reads the contents of the filename given as the first argument and appends
     13 them to the root .gitignore file. The new entires are intended to be additional
     14 ignoring patterns, or negating patterns to override existing entries (man
     15 gitignore for more details).
     16 """
     17 
     18 import os
     19 import sys
     20 
     21 MODIFY_STRING = '# The following added by %s\n'
     22 
     23 
     24 def main(argv):
     25   if not argv[1]:
     26     # Special case; do nothing.
     27     return 0
     28 
     29   modify_string = MODIFY_STRING % argv[0]
     30   gitignore_file = os.path.dirname(argv[0]) + '/../../.gitignore'
     31   lines = open(gitignore_file, 'r').readlines()
     32   for i, line in enumerate(lines):
     33     # Look for modify_string in the file to ensure we don't append the extra
     34     # patterns more than once.
     35     if line == modify_string:
     36       lines = lines[:i]
     37       break
     38   lines.append(modify_string)
     39 
     40   f = open(gitignore_file, 'w')
     41   f.write(''.join(lines))
     42   f.write(open(argv[1], 'r').read())
     43   f.close()
     44 
     45 if __name__ == '__main__':
     46   sys.exit(main(sys.argv))
     47