summaryrefslogtreecommitdiffstats
path: root/webkit/support/setup_third_party.py
blob: f3cf719c5b8590eec9cc83a7bc2baad9201f0185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python
# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""A helper script for setting up forwarding headers."""

import errno
import os
import sys


def GetHeaderFilesInDir(dir_path):
  """Return a list of all header files in dir_path."""
  all_files = []
  for root, dirs, files in os.walk(dir_path):
    all_files.extend([os.path.join(root, f) for f in files if f.endswith('.h')])
  return all_files


def Inputs(args):
  """List the files in the provided input dir.

  args: A list with 1 value, the input dir.
  Returns: 0 on success, other value on error."""
  if len(args) != 1:
    print "'inputs' expects only one input directory."
    return -1

  for filename in GetHeaderFilesInDir(args[0]):
    print filename
  return 0


def Outputs(args):
  """Takes an input dir and an output dir and figures out new output files
  based on copying from the input dir to the output dir.

  args: A list with 2 values, the input dir and the output dir.
  Returns: 0 on success, other value on error."""
  if len(args) != 2:
    print "'outputs' expects an input directory and an output directory."
    return -1

  base_input_dir = args[0]
  output_dir = args[1]
  input_files = GetHeaderFilesInDir(base_input_dir)
  for filename in input_files:
    rel_path = filename[len(base_input_dir) + 1:]
    print os.path.join(output_dir, rel_path)


def SetupHeaders(args):
  """Takes an input dir and an output dir and sets up forwarding headers
  from output dir to files in input dir.
  args: A list with 2 values, the input dir and the output dir.
  Returns: 0 on success, other value on error."""
  if len(args) != 2:
    print "'setup_headers' expects an input directory and an output directory."
    return -1

  base_input_dir = args[0]
  output_dir = args[1]
  input_files = GetHeaderFilesInDir(base_input_dir)
  for input_filename in input_files:
    rel_path = input_filename[len(base_input_dir) + 1:]
    out_filename = os.path.join(output_dir, rel_path)
    TryToMakeDir(os.path.split(out_filename)[0])
    WriteSetupFilename(input_filename, out_filename)


def TryToMakeDir(dir_name):
  """Create the directory dir_name if it doesn't exist."""
  try:
    os.makedirs(dir_name)
  except OSError, e:
    if e.errno != errno.EEXIST:
      raise e


def NormalizePath(path):
  """Normalize path for use with os.path.commonprefix.
  On windows, this makes sure that the drive letters are always in the
  same case."""
  abs_path = os.path.abspath(path)
  drive, rest = os.path.splitdrive(abs_path)
  return os.path.join(drive.lower(), rest)


def WriteSetupFilename(input_filename, out_filename):
  """Create a forwarding header from out_filename to input_filename."""
  # Figure out the relative path from out_filename to input_filename.
  # We can't use os.path.relpath since that's a python2.6 feature and we
  # support python 2.5.
  input_filename = NormalizePath(input_filename)
  out_filename = NormalizePath(out_filename)
  ancestor = os.path.commonprefix([input_filename, out_filename])

  assert os.path.isdir(ancestor)
  num_parent_dirs = 0
  out_dir = os.path.split(out_filename)[0]
  while os.path.normpath(ancestor) != os.path.normpath(out_dir):
    num_parent_dirs += 1
    out_dir = os.path.split(out_dir)[0]

  rel_path = os.path.join('/'.join(['..'] * num_parent_dirs),
                          input_filename[len(ancestor):])

  out_file = open(out_filename, 'w')
  out_file.write("""// This file is generated.  Do not edit.
#include "%s"
""" % rel_path)
  out_file.close()


def Main(argv):
  commands = {
    'inputs': Inputs,
    'outputs': Outputs,
    'setup_headers': SetupHeaders,
  }
  command = argv[1]
  args = argv[2:]
  return commands[command](args)


if __name__ == '__main__':
  sys.exit(Main(sys.argv))