1#!/usr/bin/python
2#
3# Copyright (C) 2009 Google Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30#
31# Copyright (c) 2009 The Chromium Authors. All rights reserved.
32# Use of this source code is governed by a BSD-style license that can be
33# found in the LICENSE file.
34
35# action_makenames.py is a harness script to connect actions sections of
36# gyp-based builds to make_names.pl.
37#
38# usage: action_makenames.py OUTPUTS -- INPUTS [-- OPTIONS]
39#
40# Multiple OUTPUTS, INPUTS, and OPTIONS may be listed. The sections are
41# separated by -- arguments.
42#
43# The directory name of the first output is chosen as the directory in which
44# make_names will run. If the directory name for any subsequent output is
45# different, those files will be moved to the desired directory.
46#
47# Multiple INPUTS may be listed. An input with a basename matching
48# "make_names.pl" is taken as the path to that script. Inputs with names
49# ending in TagNames.in or tags.in are taken as tag inputs. Inputs with names
50# ending in AttributeNames.in or attrs.in are taken as attribute inputs. There
51# may be at most one tag input and one attribute input. A make_names.pl input
52# is required and at least one tag or attribute input must be present.
53#
54# OPTIONS is a list of additional options to pass to make_names.pl. This
55# section need not be present.
56
57
58import os
59import posixpath
60import shutil
61import subprocess
62import sys
63
64
65def SplitArgsIntoSections(args):
66    sections = []
67    while len(args) > 0:
68        if not '--' in args:
69            # If there is no '--' left, everything remaining is an entire section.
70            dashes = len(args)
71        else:
72            dashes = args.index('--')
73
74        sections.append(args[:dashes])
75
76        # Next time through the loop, look at everything after this '--'.
77        if dashes + 1 == len(args):
78            # If the '--' is at the end of the list, we won't come back through the
79            # loop again. Add an empty section now corresponding to the nothingness
80            # following the final '--'.
81            args = []
82            sections.append(args)
83        else:
84            args = args[dashes + 1:]
85
86    return sections
87
88
89def main(args):
90    sections = SplitArgsIntoSections(args[1:])
91    assert len(sections) == 2 or len(sections) == 3
92    (outputs, inputs) = sections[:2]
93    if len(sections) == 3:
94        options = sections[2]
95    else:
96        options = []
97
98    # Make all output pathnames absolute so that they can be accessed after
99    # changing directory.
100    for index in xrange(0, len(outputs)):
101        outputs[index] = os.path.abspath(outputs[index])
102
103    outputDir = os.path.dirname(outputs[0])
104
105    # Look at the inputs and figure out which ones are make_names.pl, tags, and
106    # attributes. There can be at most one of each, and those are the only
107    # input types supported. make_names.pl is required and at least one of tags
108    # and attributes is required.
109    makeNamesInput = None
110    tagInput = None
111    attrInput = None
112    eventsInput = None
113    for input in inputs:
114        # Make input pathnames absolute so they can be accessed after changing
115        # directory. On Windows, convert \ to / for inputs to the perl script to
116        # work around the intermix of activepython + cygwin perl.
117        inputAbs = os.path.abspath(input)
118        inputAbsPosix = inputAbs.replace(os.path.sep, posixpath.sep)
119        inputBasename = os.path.basename(input)
120        if inputBasename in ('make_names.pl', 'make_event_factory.pl', 'make_dom_exceptions.pl', 'make_settings.pl'):
121            assert makeNamesInput == None
122            makeNamesInput = inputAbs
123        elif inputBasename.endswith('TagNames.in') or inputBasename.endswith('tags.in'):
124            assert tagInput == None
125            tagInput = inputAbsPosix
126        elif inputBasename.endswith('AttributeNames.in') or inputBasename.endswith('attrs.in'):
127            assert attrInput == None
128            attrInput = inputAbsPosix
129        elif (inputBasename.endswith('EventTargetFactory.in') or inputBasename.endswith('EventNames.in')
130            or inputBasename.endswith('DOMExceptions.in') or inputBasename.endswith('Settings.in')):
131            eventsInput = inputAbsPosix
132        elif inputBasename.endswith('Names.in'):
133            options.append(inputAbsPosix)
134        elif inputBasename.endswith('.pm'):
135            continue
136        else:
137            assert False
138
139    assert makeNamesInput != None
140    assert tagInput != None or attrInput != None or eventsInput != None or ('--fonts' in options) or ('--resourceTypes' in options)
141
142    # scriptsPath is a Perl include directory, located relative to
143    # makeNamesInput.
144    scriptsPath = os.path.normpath(
145        os.path.join(os.path.dirname(makeNamesInput), os.pardir, 'scripts'))
146
147    # Change to the output directory because make_names.pl puts output in its
148    # working directory.
149    os.chdir(outputDir)
150
151    # Build up the command.
152    command = ['perl', '-I', scriptsPath, makeNamesInput]
153    if tagInput != None:
154        command.extend(['--tags', tagInput])
155    if attrInput != None:
156        command.extend(['--attrs', attrInput])
157    if eventsInput != None:
158        command.extend(['--input', eventsInput])
159    command.extend(options)
160
161    # Do it. check_call is new in 2.5, so simulate its behavior with call and
162    # assert.
163    returnCode = subprocess.call(command)
164    assert returnCode == 0
165
166    # Go through the outputs. Any output that belongs in a different directory
167    # is moved. Do a copy and delete instead of rename for maximum portability.
168    # Note that all paths used in this section are still absolute.
169    for output in outputs:
170        thisOutputDir = os.path.dirname(output)
171        if thisOutputDir != outputDir:
172            outputBasename = os.path.basename(output)
173            src = os.path.join(outputDir, outputBasename)
174            dst = os.path.join(thisOutputDir, outputBasename)
175            shutil.copyfile(src, dst)
176            os.unlink(src)
177
178    return returnCode
179
180
181if __name__ == '__main__':
182    sys.exit(main(sys.argv))
183