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    for input in inputs:
113        # Make input pathnames absolute so they can be accessed after changing
114        # directory. On Windows, convert \ to / for inputs to the perl script to
115        # work around the intermix of activepython + cygwin perl.
116        inputAbs = os.path.abspath(input)
117        inputAbsPosix = inputAbs.replace(os.path.sep, posixpath.sep)
118        inputBasename = os.path.basename(input)
119        if inputBasename == 'make_names.pl':
120            assert makeNamesInput == None
121            makeNamesInput = inputAbs
122        elif inputBasename.endswith('TagNames.in') \
123             or inputBasename.endswith('tags.in'):
124            assert tagInput == None
125            tagInput = inputAbsPosix
126        elif inputBasename.endswith('AttributeNames.in') \
127             or inputBasename.endswith('attrs.in'):
128            assert attrInput == None
129            attrInput = inputAbsPosix
130        else:
131            assert False
132
133    assert makeNamesInput != None
134    assert tagInput != None or attrInput != None
135
136    # scriptsPath is a Perl include directory, located relative to
137    # makeNamesInput.
138    scriptsPath = os.path.normpath(
139        os.path.join(os.path.dirname(makeNamesInput), os.pardir, 'bindings', 'scripts'))
140
141    # Change to the output directory because make_names.pl puts output in its
142    # working directory.
143    os.chdir(outputDir)
144
145    # Build up the command.
146    command = ['perl', '-I', scriptsPath, makeNamesInput]
147    if tagInput != None:
148        command.extend(['--tags', tagInput])
149    if attrInput != None:
150        command.extend(['--attrs', attrInput])
151    command.extend(options)
152
153    # Do it. check_call is new in 2.5, so simulate its behavior with call and
154    # assert.
155    returnCode = subprocess.call(command)
156    assert returnCode == 0
157
158    # Go through the outputs. Any output that belongs in a different directory
159    # is moved. Do a copy and delete instead of rename for maximum portability.
160    # Note that all paths used in this section are still absolute.
161    for output in outputs:
162        thisOutputDir = os.path.dirname(output)
163        if thisOutputDir != outputDir:
164            outputBasename = os.path.basename(output)
165            src = os.path.join(outputDir, outputBasename)
166            dst = os.path.join(thisOutputDir, outputBasename)
167            shutil.copyfile(src, dst)
168            os.unlink(src)
169
170    return returnCode
171
172
173if __name__ == '__main__':
174    sys.exit(main(sys.argv))
175