asyncioify run_command_in_foreground

This commit is contained in:
Michael Hudson-Doyle 2019-12-13 12:21:47 +13:00
parent 614ebc415e
commit a92b88ed9d
3 changed files with 13 additions and 10 deletions

View File

@ -164,7 +164,7 @@ class Subiquity(Application):
print(DEBUG_SHELL_INTRO)
self.run_command_in_foreground(
"bash", before_hook=_before, cwd='/')
["bash"], before_hook=_before, cwd='/')
def note_file_for_apport(self, key, path):
self._apport_files.append((key, path))

View File

@ -19,13 +19,13 @@ import json
import logging
import os
import struct
import subprocess
import sys
import tty
import urwid
import yaml
from subiquitycore.async_helpers import schedule_task
from subiquitycore.controller import (
RepeatedController,
Skip,
@ -33,6 +33,7 @@ from subiquitycore.controller import (
from subiquitycore.signals import Signal
from subiquitycore.prober import Prober
from subiquitycore.ui.frame import SubiquityCoreUI
from subiquitycore.utils import arun_command
log = logging.getLogger('subiquitycore.core')
@ -415,10 +416,9 @@ class Application:
# there the symptom is that we are running in the foreground but not
# listening to stdin! The fix is the same.
def run():
subprocess.run(cmd, **kw)
def restore(fut):
async def _run():
await arun_command(
cmd, stdin=None, stdout=None, stderr=None)
screen.start()
urwid.emit_signal(
screen, urwid.display_common.INPUT_DESCRIPTORS_CHANGED)
@ -431,7 +431,7 @@ class Application:
screen, urwid.display_common.INPUT_DESCRIPTORS_CHANGED)
if before_hook is not None:
before_hook()
self.run_in_bg(run, restore)
schedule_task(_run())
def _connect_base_signals(self):
"""Connect signals used in the core controller."""

View File

@ -66,6 +66,7 @@ async def arun_command(cmd, *, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
encoding='utf-8', input=None, errors='replace',
env=None, check=False, **kw):
if input is None:
if 'stdin' not in kw:
kw['stdin'] = subprocess.DEVNULL
else:
kw['stdin'] = subprocess.PIPE
@ -75,7 +76,9 @@ async def arun_command(cmd, *, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
*cmd, stdout=stdout, stderr=stderr, env=_clean_env(env), **kw)
stdout, stderr = await proc.communicate(input=input)
if encoding:
if stdout is not None:
stdout = stdout.decode(encoding)
if stderr is not None:
stderr = stderr.decode(encoding)
log.debug("arun_command %s exited with code %s", cmd, proc.returncode)
if check and proc.returncode != 0: