[go: nahoru, domu]

Python 3 support for Crashpad’s update.py, without compromising Python 2

It’s better to be prepared for the future than…to not be.

This is mostly the result of running 2to3, with some shims to maintain
compatibility with Python 2. Some attention was needed on
str/bytes/unicode types and character encodings to keep things
compatible with both Python 2 and 3.

The #! line hasn’t changed, so this script will still normally be
interpreted by Python 2.

Change-Id: I3a7978cab09fe679fccc7ddd52e0fbce251b7ae1
Reviewed-on: https://chromium-review.googlesource.com/797334
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Scott Graham <scottmg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#520253}
diff --git a/third_party/crashpad/update.py b/third_party/crashpad/update.py
index 7781e26..f99640f8 100755
--- a/third_party/crashpad/update.py
+++ b/third_party/crashpad/update.py
@@ -5,6 +5,8 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+from __future__ import print_function
+
 import argparse
 import os
 import pipes
@@ -14,6 +16,9 @@
 import tempfile
 import textwrap
 
+if sys.version_info[0] < 3:
+  input = raw_input
+
 
 IS_WINDOWS = sys.platform.startswith('win')
 
@@ -26,7 +31,7 @@
     """
     try:
         subprocess.check_call(args, shell=IS_WINDOWS)
-    except subprocess.CalledProcessError, e:
+    except subprocess.CalledProcessError as e:
         if e.returncode != 1:
             raise
         return False
@@ -86,7 +91,7 @@
     readme_path = (parsed.readme_path or
                    os.path.join(os.path.dirname(__file__ or '.'),
                                 'README.chromium'))
-    readme_content_old = open(readme_path).read()
+    readme_content_old = open(readme_path, 'rb').read().decode('utf-8')
 
     project_name_match = re.search(
         r'^Name:\s+(.*)$', readme_content_old, re.MULTILINE)
@@ -160,31 +165,32 @@
                                         '-x',
                                         filtered_update_range]):
             assisted_cherry_pick = True
-            print >>sys.stderr, ("""
+            print("""
 Please fix the errors above and run "git cherry-pick --continue".
 Press Enter when "git cherry-pick" completes.
 You may use a new shell for this, or ^Z if job control is available.
 Press ^C to abort.
-""")
-            raw_input()
+""", file=sys.stderr)
+            input()
     except:
         # ^C, signal, or something else.
-        print >>sys.stderr, 'Aborting...'
+        print('Aborting...', file=sys.stderr)
         subprocess.call(['git', 'cherry-pick', '--abort'], shell=IS_WINDOWS)
         raise
 
     # Get an abbreviated hash and subject line for each commit in the window,
     # sorted in chronological order. Use the unfiltered view so that the commit
     # hashes are recognizable.
-    log_lines = subprocess.check_output(['git',
-                                         '-c',
-                                         'core.abbrev=12',
-                                         'log',
-                                         '--abbrev-commit',
-                                         '--pretty=oneline',
-                                         '--reverse',
-                                         unfiltered_update_range],
-                                         shell=IS_WINDOWS).splitlines(False)
+    log_lines = subprocess.check_output(
+        ['git',
+         '-c',
+         'core.abbrev=12',
+         'log',
+         '--abbrev-commit',
+         '--pretty=oneline',
+         '--reverse',
+         unfiltered_update_range],
+         shell=IS_WINDOWS).decode('utf-8').splitlines(False)
 
     if assisted_cherry_pick:
         # If the user had to help, count the number of cherry-picked commits,
@@ -193,7 +199,7 @@
             ['git', 'rev-list', '--count', original_head + '..HEAD'],
             shell=IS_WINDOWS))
         if cherry_picked_commits != len(log_lines):
-            print >>sys.stderr, 'Something smells fishy, aborting anyway...'
+            print('Something smells fishy, aborting anyway...', file=sys.stderr)
             subprocess.call(['git', 'cherry-pick', '--abort'], shell=IS_WINDOWS)
             raise Exception('not all commits were cherry-picked',
                             len(log_lines),
@@ -201,8 +207,9 @@
 
     # Make a nice commit message. Start with the full commit hash.
     revision_new = subprocess.check_output(
-        ['git', 'rev-parse', parsed.update_to], shell=IS_WINDOWS).rstrip()
-    new_message = 'Update ' + project_name + ' to ' + revision_new + '\n\n'
+        ['git', 'rev-parse', parsed.update_to],
+        shell=IS_WINDOWS).decode('utf-8').rstrip()
+    new_message = u'Update ' + project_name + ' to ' + revision_new + '\n\n'
 
     # Wrap everything to 72 characters, with a hanging indent.
     wrapper = textwrap.TextWrapper(width=72, subsequent_indent = ' ' * 13)
@@ -264,16 +271,17 @@
                           shell=IS_WINDOWS)
 
     # Write the new README.
-    open(readme_path, 'wb').write(readme_content_new)
+    open(readme_path, 'wb').write(readme_content_new.encode('utf-8'))
 
     # Commit everything.
     subprocess.check_call(['git', 'add', readme_path], shell=IS_WINDOWS)
 
     try:
         commit_message_name = None
-        with tempfile.NamedTemporaryFile(delete=False) as commit_message_f:
+        with tempfile.NamedTemporaryFile(mode='wb',
+                                         delete=False) as commit_message_f:
             commit_message_name = commit_message_f.name
-            commit_message_f.write(new_message)
+            commit_message_f.write(new_message.encode('utf-8'))
         subprocess.check_call(['git',
                                'commit', '--file=' + commit_message_name],
                               shell=IS_WINDOWS)
@@ -282,9 +290,8 @@
             os.unlink(commit_message_name)
 
     if has_local_modifications:
-        print >>sys.stderr, (
-            'Remember to check the Local Modifications section in ' +
-            readme_path)
+        print('Remember to check the Local Modifications section in ' +
+              readme_path, file=sys.stderr)
 
     return 0