From 3b17dababd5a3c68c2be3e8b38f402b9697fc46e Mon Sep 17 00:00:00 2001 From: Romain Failliot Date: Sun, 9 Jan 2022 15:10:31 -0500 Subject: [PATCH] Add static typing for utils.popenRead --- src/diffuse/utils.py | 42 +++++++++++++++++++++++----------------- src/diffuse/vcs/darcs.py | 4 ++-- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/diffuse/utils.py b/src/diffuse/utils.py index 1702435..f566657 100644 --- a/src/diffuse/utils.py +++ b/src/diffuse/utils.py @@ -164,43 +164,49 @@ def _use_flatpak() -> bool: # use popen to read the output of a command -def popenRead(dn, cmd, prefs, bash_pref, success_results=None): +def popenRead( + cwd: str, + cmd: List[str], + prefs: Preferences, + bash_pref: str, + success_results: List[int] = None) -> bytes: if success_results is None: success_results = [0] + + opt_cwd: Optional[str] = cwd if isWindows() and prefs.getBool(bash_pref): # launch the command from a bash shell is requested cmd = [ prefs.convertToNativePath('/bin/bash.exe'), '-l', '-c', - f"cd {_bash_escape(dn)}; {' '.join([ _bash_escape(arg) for arg in cmd ])}" + f"cd {_bash_escape(cwd)}; {' '.join([ _bash_escape(arg) for arg in cmd ])}" ] - dn = None + opt_cwd = None + # use subprocess.Popen to retrieve the file contents if isWindows(): - info = subprocess.STARTUPINFO() - info.dwFlags |= subprocess.STARTF_USESHOWWINDOW - info.wShowWindow = subprocess.SW_HIDE + info = subprocess.STARTUPINFO() # type: ignore + info.dwFlags |= subprocess.STARTF_USESHOWWINDOW # type: ignore + info.wShowWindow = subprocess.SW_HIDE # type: ignore else: info = None + if _use_flatpak(): cmd = ['flatpak-spawn', '--host'] + cmd with subprocess.Popen( cmd, - stdin=subprocess.PIPE, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - cwd=dn, + cwd=opt_cwd, startupinfo=info) as proc: - proc.stdin.close() - proc.stderr.close() - fd = proc.stdout - # read the command's output - s = fd.read() - fd.close() + output: bytes + if proc.stdout is not None: + # read the command's output + output = proc.stdout.read() + proc.stdout.close() if proc.wait() not in success_results: raise IOError('Command failed.') - return s + return output def len_minus_line_ending(s: str) -> int: @@ -228,9 +234,9 @@ def _strip_eols(ss: List[str]) -> List[str]: # use popen to read the output of a command -def popenReadLines(dn, cmd, prefs, bash_pref, success_results=None): +def popenReadLines(cwd, cmd, prefs, bash_pref, success_results=None): return _strip_eols(splitlines(popenRead( - dn, cmd, prefs, bash_pref, success_results).decode('utf-8', errors='ignore'))) + cwd, cmd, prefs, bash_pref, success_results).decode('utf-8', errors='ignore'))) def readconfiglines(fd: TextIO) -> List[str]: diff --git a/src/diffuse/vcs/darcs.py b/src/diffuse/vcs/darcs.py index dac7bdd..9dd25bf 100644 --- a/src/diffuse/vcs/darcs.py +++ b/src/diffuse/vcs/darcs.py @@ -19,7 +19,7 @@ import os -from typing import List, Optional +from typing import List from diffuse import utils from diffuse.preferences import Preferences @@ -139,7 +139,7 @@ class Darcs(VcsInterface): return self._getCommitTemplate(prefs, names, None) def getRevision(self, prefs: Preferences, name: str, rev: str) -> bytes: - args: List[Optional[str]] = [prefs.getString('darcs_bin'), 'show', 'contents'] + args: List[str] = [prefs.getString('darcs_bin'), 'show', 'contents'] try: args.extend(['-n', str(int(rev))]) except ValueError: