summaryrefslogtreecommitdiffstats
path: root/chrome/tools/build/win/scan_server_dlls.py
blob: 084728da883f9de0d5b48509da8c5a0051387006 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python
# Copyright (c) 2006-2008 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.

"""Script used to scan for server DLLs at build time and build a header
   included by setup.exe. This header contains an array of the names of
   the DLLs that need registering at install time.

"""

import ConfigParser
import glob
import optparse
import os
import sys

CHROME_DIR = "Chrome-bin"
SERVERS_DIR = "servers"
GENERATED_DLL_INCLUDE_FILE_NAME = "registered_dlls.h"
GENERATED_DLL_INCLUDE_FILE_CONTENTS = """
// This file is automatically generated by scan_server_dlls.py.
// It contains the list of COM server dlls that need registering at
// install time.
#include "base/basictypes.h"

namespace {
const wchar_t* kDllsToRegister[] = { %s };
const int kNumDllsToRegister = %d;
}
"""


def Readconfig(output_dir, input_file):
  """Reads config information from input file after setting default value of
  global variabes.
  """
  variables = {}
  variables['ChromeDir'] = CHROME_DIR
  # Use a bogus version number, we don't really care what it is, we just
  # want to find the files that would get picked up from chrome.release,
  # and don't care where the installer archive task ends up putting them.
  variables['VersionDir'] = os.path.join(variables['ChromeDir'],
                                         '0.0.0.0')
  config = ConfigParser.SafeConfigParser(variables)

  print "Reading input_file: " + input_file

  config.read(input_file)
  return config


def CreateRegisteredDllIncludeFile(registered_dll_list, header_output_dir):
  """ Outputs the header file included by the setup project that
  contains the names of the DLLs to be registered at installation
  time.
  """
  output_file = os.path.join(header_output_dir, GENERATED_DLL_INCLUDE_FILE_NAME)

  dll_array_string = ""
  for dll in registered_dll_list:
    dll.replace("\\", "\\\\")
    if dll_array_string:
      dll_array_string += ', '
    dll_array_string += "L\"%s\"" % dll

  f = open(output_file, 'w')
  try:
    if len(registered_dll_list) == 0:
      f.write(GENERATED_DLL_INCLUDE_FILE_CONTENTS % ("L\"\"", 0))
    else:
      f.write(GENERATED_DLL_INCLUDE_FILE_CONTENTS % (dll_array_string,
                                                     len(registered_dll_list)))
  finally:
    f.close()


def ScanServerDlls(config, distribution, output_dir):
  """Scans for DLLs in the specified section of config that are in the
  subdirectory of output_dir named SERVERS_DIR. Returns a list of only the
  filename components of the paths to all matching DLLs.
  """

  registered_dll_list = []
  ScanDllsInSection(config, 'GENERAL', output_dir, registered_dll_list)
  if distribution:
    if len(distribution) > 1 and distribution[0] == '_':
      distribution = distribution[1:]
    ScanDllsInSection(config, distribution.upper(), output_dir,
                      registered_dll_list)

  return registered_dll_list


def ScanDllsInSection(config, section, output_dir, registered_dll_list):
  """Scans for DLLs in the specified section of config that are in the
  subdirectory of output_dir named SERVERS_DIR. Appends the file name of all
  matching dlls to registered_dll_list.
  """
  for option in config.options(section):
    if option.endswith('dir'):
      continue

    dst = config.get(section, option)
    (x, src_folder) = os.path.split(dst)

    for file in glob.glob(os.path.join(output_dir, option)):
      if option.startswith(SERVERS_DIR):
        (x, file_name) = os.path.split(file)
        print "Found server DLL file: " + file_name
        registered_dll_list.append(file_name)


def RunSystemCommand(cmd):
  if (os.system(cmd) != 0):
    raise "Error while running cmd: %s" % cmd


def main(options):
  """Main method that reads input file, scans <build_output>\servers for
     matches to files described in the input file. A header file for the
     setup project is then generated.
  """
  config = Readconfig(options.output_dir, options.input_file)
  registered_dll_list = ScanServerDlls(config, options.distribution,
                                       options.output_dir)
  CreateRegisteredDllIncludeFile(registered_dll_list,
                                 options.header_output_dir)


if '__main__' == __name__:
  option_parser = optparse.OptionParser()
  option_parser.add_option('-o', '--output_dir', help='Build Output directory')
  option_parser.add_option('-x', '--header_output_dir',
      help='Location where the generated header file will be placed.')
  option_parser.add_option('-i', '--input_file', help='Input file')
  option_parser.add_option('-d', '--distribution',
      help='Name of Chromium Distribution. Optional.')

  options, args = option_parser.parse_args()
  sys.exit(main(options))