From: Joel Holdsworth <jholdsworth@nvidia.com>
To: git@vger.kernel.org
Cc: Tzadik Vanderhoof <tzadik.vanderhoof@gmail.com>,
Dorgon Chang <dorgonman@hotmail.com>,
Joachim Kuebart <joachim.kuebart@gmail.com>,
Daniel Levin <dendy.ua@gmail.com>,
Johannes Schindelin <johannes.schindelin@gmx.de>,
Luke Diamand <luke@diamand.org>, Ben Keene <seraphire@gmail.com>,
Andrew Oakley <andrew@adoakley.name>,
Joel Holdsworth <jholdsworth@nvidia.com>
Subject: [PATCH v2 2/3] git-p4: eliminate decode_stream and encode_stream
Date: Fri, 10 Dec 2021 15:31:00 +0000 [thread overview]
Message-ID: <20211210153101.35433-3-jholdsworth@nvidia.com> (raw)
In-Reply-To: <20211210153101.35433-1-jholdsworth@nvidia.com>
The decode_stream and encode_stream functions previously abstracted away
the differences in string encode/decode behaviour between Python 2 and
Python 3.
Given that Python 2 is no longer supported, and the code paths for it
have been removed, the abstraction is no longer necessary, and the
script can therefore be simplified by eliminating these functions.
Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
---
git-p4.py | 49 +++++++++++++++++++------------------------------
1 file changed, 19 insertions(+), 30 deletions(-)
diff --git a/git-p4.py b/git-p4.py
index e3fe86e4f2..5568d44c72 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -167,11 +167,6 @@ def prompt(prompt_text):
if response in choices:
return response
-def decode_text_stream(s):
- return s.decode() if isinstance(s, bytes) else s
-def encode_text_stream(s):
- return s.encode() if isinstance(s, str) else s
-
def decode_path(path):
"""Decode a given string (bytes or otherwise) using configured path encoding options
"""
@@ -276,7 +271,7 @@ def read_pipe_full(c):
expand = not isinstance(c, list)
p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
(out, err) = p.communicate()
- return (p.returncode, out, decode_text_stream(err))
+ return (p.returncode, out, err.decode())
def read_pipe(c, ignore_error=False, raw=False):
""" Read output from command. Returns the output text on
@@ -288,22 +283,17 @@ def read_pipe(c, ignore_error=False, raw=False):
(retcode, out, err) = read_pipe_full(c)
if retcode != 0:
if ignore_error:
- out = ""
+ out = b""
else:
die('Command failed: %s\nError: %s' % (str(c), err))
- if not raw:
- out = decode_text_stream(out)
- return out
+ return out if raw else out.decode()
def read_pipe_text(c):
""" Read output from a command with trailing whitespace stripped.
On error, returns None.
"""
(retcode, out, err) = read_pipe_full(c)
- if retcode != 0:
- return None
- else:
- return decode_text_stream(out).rstrip()
+ return out.decode().rstrip() if retcode == 0 else None
def p4_read_pipe(c, ignore_error=False, raw=False):
real_cmd = p4_build_cmd(c)
@@ -316,7 +306,7 @@ def read_pipe_lines(c):
expand = not isinstance(c, list)
p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
pipe = p.stdout
- val = [decode_text_stream(line) for line in pipe.readlines()]
+ val = [line.decode() for line in pipe.readlines()]
if pipe.close() or p.wait():
die('Command failed: %s' % str(c))
return val
@@ -346,7 +336,7 @@ def p4_has_move_command():
cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = p.communicate()
- err = decode_text_stream(err)
+ err = err.decode()
# return code will be 1 in either case
if err.find("Invalid option") >= 0:
return False
@@ -721,7 +711,7 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
stdin_file.write(stdin)
else:
for i in stdin:
- stdin_file.write(encode_text_stream(i))
+ stdin_file.write(i.encode() if isinstance(i, str) else i)
stdin_file.write(b'\n')
stdin_file.flush()
stdin_file.seek(0)
@@ -963,8 +953,7 @@ def branch_exists(branch):
cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- out, _ = p.communicate()
- out = decode_text_stream(out)
+ out = p.communicate()[0].decode()
if p.returncode:
return False
# expect exactly one line of output: the branch name
@@ -1349,7 +1338,7 @@ def generatePointer(self, contentFile):
['git', 'lfs', 'pointer', '--file=' + contentFile],
stdout=subprocess.PIPE
)
- pointerFile = decode_text_stream(pointerProcess.stdout.read())
+ pointerFile = pointerProcess.stdout.read().decode()
if pointerProcess.wait():
os.remove(contentFile)
die('git-lfs pointer command failed. Did you install the extension?')
@@ -2148,7 +2137,7 @@ def applyCommit(self, id):
tmpFile = os.fdopen(handle, "w+b")
if self.isWindows:
submitTemplate = submitTemplate.replace("\n", "\r\n")
- tmpFile.write(encode_text_stream(submitTemplate))
+ tmpFile.write(submitTemplate.encode())
tmpFile.close()
submitted = False
@@ -2204,8 +2193,8 @@ def applyCommit(self, id):
return False
# read the edited message and submit
- tmpFile = open(fileName, "rb")
- message = decode_text_stream(tmpFile.read())
+ with open(fileName, "r") as tmpFile:
+ message = tmpFile.read()
tmpFile.close()
if self.isWindows:
message = message.replace("\r\n", "\n")
@@ -2905,7 +2894,7 @@ def splitFilesIntoBranches(self, commit):
return branches
def writeToGitStream(self, gitMode, relPath, contents):
- self.gitStream.write(encode_text_stream(u'M {} inline {}\n'.format(gitMode, relPath)))
+ self.gitStream.write('M {} inline {}\n'.format(gitMode, relPath))
self.gitStream.write('data %d\n' % sum(len(d) for d in contents))
for d in contents:
self.gitStream.write(d)
@@ -2947,7 +2936,7 @@ def streamOneP4File(self, file, contents):
git_mode = "120000"
# p4 print on a symlink sometimes contains "target\n";
# if it does, remove the newline
- data = ''.join(decode_text_stream(c) for c in contents)
+ data = ''.join(c.decode() for c in contents)
if not data:
# Some version of p4 allowed creating a symlink that pointed
# to nothing. This causes p4 errors when checking out such
@@ -3001,9 +2990,9 @@ def streamOneP4File(self, file, contents):
pattern = p4_keywords_regexp_for_type(type_base, type_mods)
if pattern:
regexp = re.compile(pattern, re.VERBOSE)
- text = ''.join(decode_text_stream(c) for c in contents)
+ text = ''.join(c.decode() for c in contents)
text = regexp.sub(r'$\1$', text)
- contents = [ encode_text_stream(text) ]
+ contents = [text.encode()]
if self.largeFileSystem:
(git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)
@@ -3015,7 +3004,7 @@ def streamOneP4Deletion(self, file):
if verbose:
sys.stdout.write("delete %s\n" % relPath)
sys.stdout.flush()
- self.gitStream.write(encode_text_stream(u'D {}\n'.format(relPath)))
+ self.gitStream.write('D {}\n'.format(relPath))
if self.largeFileSystem and self.largeFileSystem.isLargeFile(relPath):
self.largeFileSystem.removeLargeFile(relPath)
@@ -3115,9 +3104,9 @@ def streamP4FilesCbSelf(entry):
if 'shelved_cl' in f:
# Handle shelved CLs using the "p4 print file@=N" syntax to print
# the contents
- fileArg = f['path'] + encode_text_stream('@={}'.format(f['shelved_cl']))
+ fileArg = f['path'] + '@={}'.format(f['shelved_cl']).encode()
else:
- fileArg = f['path'] + encode_text_stream('#{}'.format(f['rev']))
+ fileArg = f['path'] + '#{}'.format(f['rev']).encode()
fileArgs.append(fileArg)
--
2.33.0
next prev parent reply other threads:[~2021-12-10 15:31 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-10 15:30 [PATCH v2 0/3] Transition git-p4.py to support Python 3 only Joel Holdsworth
2021-12-10 15:30 ` [PATCH v2 1/3] git-p4: remove support for Python 2 Joel Holdsworth
2021-12-12 17:50 ` Andrew Oakley
2021-12-12 22:01 ` Ævar Arnfjörð Bjarmason
2021-12-10 15:31 ` Joel Holdsworth [this message]
2021-12-10 15:31 ` [PATCH v2 3/3] git-p4: add "Nvidia Corporation" to copyright header Joel Holdsworth
2021-12-10 18:57 ` Luke Diamand
2021-12-11 21:19 ` Elijah Newren
2021-12-13 0:11 ` brian m. carlson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211210153101.35433-3-jholdsworth@nvidia.com \
--to=jholdsworth@nvidia.com \
--cc=andrew@adoakley.name \
--cc=dendy.ua@gmail.com \
--cc=dorgonman@hotmail.com \
--cc=git@vger.kernel.org \
--cc=joachim.kuebart@gmail.com \
--cc=johannes.schindelin@gmx.de \
--cc=luke@diamand.org \
--cc=seraphire@gmail.com \
--cc=tzadik.vanderhoof@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).