OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 Code Aurora Forum. All rights reserved. | 2 # Copyright (c) 2011 Code Aurora Forum. All rights reserved. |
3 # Copyright (c) 2010 Google Inc. All rights reserved. | 3 # Copyright (c) 2010 Google Inc. All rights reserved. |
4 # Copyright (c) 2009 Apple Inc. All rights reserved. | 4 # Copyright (c) 2009 Apple Inc. All rights reserved. |
5 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) | 5 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) |
6 # | 6 # |
7 # Redistribution and use in source and binary forms, with or without | 7 # Redistribution and use in source and binary forms, with or without |
8 # modification, are permitted provided that the following conditions are | 8 # modification, are permitted provided that the following conditions are |
9 # met: | 9 # met: |
10 # | 10 # |
(...skipping 14 matching lines...) Expand all Loading... |
25 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 25 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 # | 32 # |
33 # A tool for automating dealing with bugzilla, posting patches, committing patch
es, etc. | 33 # A tool for automating dealing with bugzilla, posting patches, committing patch
es, etc. |
34 | 34 |
35 import logging | 35 import time |
36 import os | 36 import urllib |
37 import signal | 37 import webbrowser |
38 import sys | |
39 import codecs | |
40 | 38 |
41 import webkitpy.common.version_check | 39 from webkitpy.common.prettypatch import PrettyPatch |
| 40 from webkitpy.common.system import logutils |
| 41 from webkitpy.common.system.executive import Executive |
| 42 from webkitpy.common.system.executive import ScriptError |
| 43 from webkitpy.common.system.filesystem import FileSystem |
| 44 from webkitpy.common.checkout.scm.detection import detect_scm_system |
42 | 45 |
43 from webkitpy.common.system.logutils import configure_logging | |
44 from webkitpy.tool.main import WebKitPatch | |
45 | 46 |
46 # A StreamWriter will by default try to encode all objects passed | 47 _log = logutils.get_logger(__file__) |
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. | |
52 class 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 | 48 |
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
. | |
66 sys.stdout = ForgivingUTF8Writer(sys.stdout) | |
67 | 49 |
68 _log = logging.getLogger("webkit-patch") | 50 def can_open_url(): |
| 51 try: |
| 52 webbrowser.get() |
| 53 return True |
| 54 except webbrowser.Error, e: |
| 55 return False |
| 56 |
| 57 def open_url(url): |
| 58 if not can_open_url(): |
| 59 _log.warn("Failed to open %s" % url) |
| 60 webbrowser.open(url) |
| 61 |
| 62 def show_pretty_diff(): |
| 63 if not can_open_url(): |
| 64 return None |
| 65 |
| 66 try: |
| 67 pretty_patch = PrettyPatch(Executive()) |
| 68 pretty_diff_file = pretty_patch.pretty_diff_file(diff()) |
| 69 url = "file://%s" % urllib.quote(pretty_diff_file.name) |
| 70 open_url(url) |
| 71 # We return the pretty_diff_file here because we need to keep the |
| 72 # file alive until the user has had a chance to confirm the diff. |
| 73 return pretty_diff_file |
| 74 except ScriptError, e: |
| 75 _log.warning("PrettyPatch failed. :(") |
| 76 except OSError, e: |
| 77 _log.warning("PrettyPatch unavailable.") |
| 78 |
| 79 def diff(): |
| 80 scm = detect_scm_system(FileSystem().getcwd()) |
| 81 changed_files = scm.changed_files() |
| 82 return scm.create_patch(None, changed_files=changed_files) |
69 | 83 |
70 def main(): | 84 def main(): |
71 # This is a hack to let us enable DEBUG logging as early as possible. | 85 pretty_diff_file = show_pretty_diff() |
72 # Note this can't be ternary as versioning.check_version() | 86 # This is a terrible hack. Try to give enough time for the |
73 # hasn't run yet and this python might be older than 2.5. | 87 # browser to actually load the file. |
74 if set(["-v", "--verbose"]).intersection(set(sys.argv)): | 88 time.sleep(1) |
75 logging_level = logging.DEBUG | 89 if pretty_diff_file: |
76 else: | 90 pretty_diff_file.close() |
77 logging_level = logging.INFO | |
78 configure_logging(logging_level=logging_level) | |
79 WebKitPatch(os.path.abspath(__file__)).main() | |
80 | |
81 | 91 |
82 if __name__ == "__main__": | 92 if __name__ == "__main__": |
83 try: | 93 try: |
84 main() | 94 main() |
85 except KeyboardInterrupt: | 95 except KeyboardInterrupt: |
86 sys.exit(signal.SIGINT + 128) | 96 sys.exit(signal.SIGINT + 128) |
OLD | NEW |