1#!/usr/bin/env python
2# Copyright (c) 2011 Code Aurora Forum. All rights reserved.
3# Copyright (c) 2010 Google Inc. All rights reserved.
4# Copyright (c) 2009 Apple Inc. All rights reserved.
5# Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met:
10#
11#     * Redistributions of source code must retain the above copyright
12# notice, this list of conditions and the following disclaimer.
13#     * Redistributions in binary form must reproduce the above
14# copyright notice, this list of conditions and the following disclaimer
15# in the documentation and/or other materials provided with the
16# distribution.
17#     * Neither the name of Google Inc. nor the names of its
18# contributors may be used to endorse or promote products derived from
19# this software without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32#
33# A tool for automating dealing with bugzilla, posting patches, committing patches, etc.
34
35import logging
36import os
37import signal
38import sys
39import codecs
40
41import webkitpy.common.version_check
42
43from webkitpy.common.system.logutils import configure_logging
44from webkitpy.tool.main import WebKitPatch
45
46# A StreamWriter will by default try to encode all objects passed
47# to write(), so when passed a raw string already encoded as utf8,
48# it will blow up with an UnicodeDecodeError. This does not match
49# the default behaviour of writing to sys.stdout, so we intercept
50# the case of writing raw strings and make sure StreamWriter gets
51# input that it can handle.
52class ForgivingUTF8Writer(codecs.lookup('utf-8')[-1]):
53    def write(self, object):
54        if isinstance(object, str):
55            # Assume raw strings are utf-8 encoded. If this line
56            # fails with an UnicodeDecodeError, our assumption was
57            # wrong, and the stacktrace should show you where we
58            # write non-Unicode/UTF-8 data (which we shouldn't).
59            object = object.decode('utf-8')
60        return codecs.StreamWriter.write(self, object)
61
62# By default, sys.stdout assumes ascii encoding.  Since our messages can
63# contain unicode strings (as with some peoples' names) we need to apply
64# the utf-8 codec to prevent throwing and exception.
65# Not having this was the cause of https://bugs.webkit.org/show_bug.cgi?id=63452.
66sys.stdout = ForgivingUTF8Writer(sys.stdout)
67
68_log = logging.getLogger("webkit-patch")
69
70def main():
71    # This is a hack to let us enable DEBUG logging as early as possible.
72    # Note this can't be ternary as versioning.check_version()
73    # hasn't run yet and this python might be older than 2.5.
74    if set(["-v", "--verbose"]).intersection(set(sys.argv)):
75        logging_level = logging.DEBUG
76    else:
77        logging_level = logging.INFO
78    configure_logging(logging_level=logging_level)
79    WebKitPatch(os.path.abspath(__file__)).main()
80
81
82if __name__ == "__main__":
83    try:
84        main()
85    except KeyboardInterrupt:
86        sys.exit(signal.SIGINT + 128)
87