Display new Autofill UI Contents in Views
[chromium-blink-merge.git] / webkit / support / setup_third_party.py
blob50c7cc22b83ba31cde6f2fb7f74169557128eddd
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """A helper script for setting up forwarding headers."""
8 import errno
9 import os
10 import sys
13 def GetHeaderFilesInDir(dir_path):
14 """Return a list of all header files in dir_path."""
15 all_files = []
16 for root, dirs, files in os.walk(dir_path):
17 # Backslashes get shell escaped by gyp, so force forward slash for
18 # path separators.
19 all_files.extend([os.path.join(root, f).replace(os.sep, '/')
20 for f in files if f.endswith('.h')])
21 return all_files
24 def Inputs(args):
25 """List the files in the provided input dir.
27 args: A list with 1 value, the input dir.
28 Returns: 0 on success, other value on error."""
29 if len(args) != 1:
30 print "'inputs' expects only one input directory."
31 return -1
33 for filename in GetHeaderFilesInDir(args[0]):
34 print filename
35 return 0
38 def Outputs(args):
39 """Takes an input dir and an output dir and figures out new output files
40 based on copying from the input dir to the output dir.
42 args: A list with 2 values, the input dir and the output dir.
43 Returns: 0 on success, other value on error."""
44 if len(args) != 2:
45 print "'outputs' expects an input directory and an output directory."
46 return -1
48 base_input_dir = args[0]
49 output_dir = args[1]
50 input_files = GetHeaderFilesInDir(base_input_dir)
51 for filename in input_files:
52 rel_path = filename[len(base_input_dir) + 1:]
53 # Backslashes get shell escaped by gyp, so force forward slash for
54 # path separators.
55 print os.path.join(output_dir, rel_path).replace(os.sep, '/')
58 def SetupHeaders(args):
59 """Takes an input dir and an output dir and sets up forwarding headers
60 from output dir to files in input dir.
61 args: A list with 2 values, the input dir and the output dir.
62 Returns: 0 on success, other value on error."""
63 if len(args) != 2 and len(args) != 3:
64 print ("'setup_headers' expects an input directory, an output directory, ."
65 "and an optional directory to make includes relative to.")
66 return -1
68 base_input_dir = args[0]
69 output_dir = args[1]
70 relative_to_dir = None
71 if len(args) == 3:
72 relative_to_dir = args[2]
73 input_files = GetHeaderFilesInDir(base_input_dir)
74 for input_filename in input_files:
75 rel_path = input_filename[len(base_input_dir) + 1:]
76 out_filename = os.path.join(output_dir, rel_path)
77 TryToMakeDir(os.path.split(out_filename)[0])
78 WriteSetupFilename(input_filename, out_filename, relative_to_dir)
81 def TryToMakeDir(dir_name):
82 """Create the directory dir_name if it doesn't exist."""
83 try:
84 os.makedirs(dir_name)
85 except OSError, e:
86 if e.errno != errno.EEXIST:
87 raise e
90 def NormalizePath(path):
91 """Normalize path for use with os.path.commonprefix.
92 On windows, this makes sure that the drive letters are always in the
93 same case."""
94 abs_path = os.path.abspath(path)
95 drive, rest = os.path.splitdrive(abs_path)
96 return os.path.join(drive.lower(), rest)
99 def WriteSetupFilename(input_filename, out_filename, relative_to_dir):
100 """Create a forwarding header from out_filename to input_filename."""
101 # Figure out the relative path from out_filename to input_filename.
102 # We can't use os.path.relpath since that's a python2.6 feature and we
103 # support python 2.5.
104 input_filename = NormalizePath(input_filename)
105 out_filename = NormalizePath(out_filename)
106 ancestor = os.path.commonprefix([input_filename, out_filename])
108 assert os.path.isdir(ancestor)
109 num_parent_dirs = 0
110 out_dir = os.path.split(out_filename)[0]
111 while os.path.normpath(ancestor) != os.path.normpath(out_dir):
112 num_parent_dirs += 1
113 out_dir = os.path.split(out_dir)[0]
115 if sys.platform == 'win32':
116 # Windows has a file path limit of 260 characters that we can hit when
117 # generating these forwarding headers. Instead of writing the full
118 # relative path, just write the path relative to the WebKit/chromium dir,
119 # which is in the include path.
120 rel_path = input_filename[len(ancestor):]
121 if relative_to_dir:
122 rel_path = os.path.join(relative_to_dir, rel_path)
123 else:
124 rel_path = os.path.join('/'.join(['..'] * num_parent_dirs),
125 input_filename[len(ancestor):])
127 out_file = open(out_filename, 'w')
128 out_file.write("""// This file is generated. Do not edit.
129 #include "%s"
130 """ % rel_path)
131 out_file.close()
134 def Main(argv):
135 commands = {
136 'inputs': Inputs,
137 'outputs': Outputs,
138 'setup_headers': SetupHeaders,
140 command = argv[1]
141 args = argv[2:]
142 return commands[command](args)
145 if __name__ == '__main__':
146 sys.exit(Main(sys.argv))