1#!/usr/bin/env python
2#
3# Copyright 2014 the V8 project authors. All rights reserved.
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9#       notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11#       copyright notice, this list of conditions and the following
12#       disclaimer in the documentation and/or other materials provided
13#       with the distribution.
14#     * Neither the name of Google Inc. nor the names of its
15#       contributors may be used to endorse or promote products derived
16#       from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30# This utility concatenates several files into one. On Unix-like systems
31# it is equivalent to:
32#   cat file1 file2 file3 ...files... > target
33#
34# The reason for writing a seperate utility is that 'cat' is not available
35# on all supported build platforms, but Python is, and hence this provides
36# us with an easy and uniform way of doing this on all platforms.
37
38import optparse
39
40
41def Concatenate(filenames):
42  """Concatenate files.
43
44  Args:
45    files: Array of file names.
46           The last name is the target; all earlier ones are sources.
47
48  Returns:
49    True, if the operation was successful.
50  """
51  if len(filenames) < 2:
52    print "An error occured generating %s:\nNothing to do." % filenames[-1]
53    return False
54
55  try:
56    with open(filenames[-1], "wb") as target:
57      for filename in filenames[:-1]:
58        with open(filename, "rb") as current:
59          target.write(current.read())
60    return True
61  except IOError as e:
62    print "An error occured when writing %s:\n%s" % (filenames[-1], e)
63    return False
64
65
66def main():
67  parser = optparse.OptionParser()
68  parser.set_usage("""Concatenate several files into one.
69      Equivalent to: cat file1 ... > target.""")
70  (options, args) = parser.parse_args()
71  exit(0 if Concatenate(args) else 1)
72
73
74if __name__ == "__main__":
75  main()
76