Add static typing for utils.popenRead

This commit is contained in:
Romain Failliot 2022-01-09 15:10:31 -05:00
parent dd57b466ef
commit 3b17dababd
2 changed files with 26 additions and 20 deletions

View File

@ -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]:

View File

@ -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: