Skip to content

Commit

Permalink
fix: Support running git-[im,ex]port-patches with Python3 too (#32410)
Browse files Browse the repository at this point in the history
* script: Python3 compatibility for utf8 conversion

The unicode() method has been renamed to str() in Python3,
add a wrapper around it to support running against both versions.

* script: don't require python2 for git-[import,export]-patches

The scripts work just fine with python3 too, so use the generic
python executable as the script interpreter.
Most setups don't even require or provide python 2 anymore,
so this saves one from having to install it just for the scripts.

Co-authored-by: Romain Pokrzywka <rpokrzywka@nvidia.com>
  • Loading branch information
trop[bot] and kromain committed Jan 11, 2022
1 parent 583421f commit 2449ef1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion script/git-export-patches
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python

import argparse
import sys
Expand Down
2 changes: 1 addition & 1 deletion script/git-import-patches
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python

import argparse
import sys
Expand Down
10 changes: 9 additions & 1 deletion script/lib/git.py
Expand Up @@ -229,6 +229,14 @@ def remove_patch_filename(patch):
force_keep_next_line = l.startswith('Subject: ')


def to_utf8(patch):
"""Python 2/3 compatibility: unicode has been renamed to str in Python3"""
if sys.version_info[0] >= 3:
return str(patch, "utf-8")
else:
return unicode(patch, "utf-8")


def export_patches(repo, out_dir, patch_range=None, dry_run=False):
if patch_range is None:
patch_range, num_patches = guess_base_commit(repo)
Expand All @@ -250,7 +258,7 @@ def export_patches(repo, out_dir, patch_range=None, dry_run=False):
for patch in patches:
filename = get_file_name(patch)
filepath = posixpath.join(out_dir, filename)
existing_patch = unicode(io.open(filepath, 'rb').read(), "utf-8")
existing_patch = to_utf8(io.open(filepath, 'rb').read())
formatted_patch = join_patch(patch)
if formatted_patch != existing_patch:
bad_patches.append(filename)
Expand Down

0 comments on commit 2449ef1

Please sign in to comment.