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 # 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: if success_results is None:
success_results = [0] success_results = [0]
opt_cwd: Optional[str] = cwd
if isWindows() and prefs.getBool(bash_pref): if isWindows() and prefs.getBool(bash_pref):
# launch the command from a bash shell is requested # launch the command from a bash shell is requested
cmd = [ cmd = [
prefs.convertToNativePath('/bin/bash.exe'), prefs.convertToNativePath('/bin/bash.exe'),
'-l', '-l',
'-c', '-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 # use subprocess.Popen to retrieve the file contents
if isWindows(): if isWindows():
info = subprocess.STARTUPINFO() info = subprocess.STARTUPINFO() # type: ignore
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW info.dwFlags |= subprocess.STARTF_USESHOWWINDOW # type: ignore
info.wShowWindow = subprocess.SW_HIDE info.wShowWindow = subprocess.SW_HIDE # type: ignore
else: else:
info = None info = None
if _use_flatpak(): if _use_flatpak():
cmd = ['flatpak-spawn', '--host'] + cmd cmd = ['flatpak-spawn', '--host'] + cmd
with subprocess.Popen( with subprocess.Popen(
cmd, cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd=opt_cwd,
cwd=dn,
startupinfo=info) as proc: startupinfo=info) as proc:
proc.stdin.close() output: bytes
proc.stderr.close() if proc.stdout is not None:
fd = proc.stdout # read the command's output
# read the command's output output = proc.stdout.read()
s = fd.read() proc.stdout.close()
fd.close()
if proc.wait() not in success_results: if proc.wait() not in success_results:
raise IOError('Command failed.') raise IOError('Command failed.')
return s return output
def len_minus_line_ending(s: str) -> int: 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 # 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( 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]: def readconfiglines(fd: TextIO) -> List[str]:

View File

@ -19,7 +19,7 @@
import os import os
from typing import List, Optional from typing import List
from diffuse import utils from diffuse import utils
from diffuse.preferences import Preferences from diffuse.preferences import Preferences
@ -139,7 +139,7 @@ class Darcs(VcsInterface):
return self._getCommitTemplate(prefs, names, None) return self._getCommitTemplate(prefs, names, None)
def getRevision(self, prefs: Preferences, name: str, rev: str) -> bytes: 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: try:
args.extend(['-n', str(int(rev))]) args.extend(['-n', str(int(rev))])
except ValueError: except ValueError: