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) print(DEBUG_SHELL_INTRO)
self.run_command_in_foreground( self.run_command_in_foreground(
"bash", before_hook=_before, cwd='/') ["bash"], before_hook=_before, cwd='/')
def note_file_for_apport(self, key, path): def note_file_for_apport(self, key, path):
self._apport_files.append((key, path)) self._apport_files.append((key, path))

View File

@ -19,13 +19,13 @@ import json
import logging import logging
import os import os
import struct import struct
import subprocess
import sys import sys
import tty import tty
import urwid import urwid
import yaml import yaml
from subiquitycore.async_helpers import schedule_task
from subiquitycore.controller import ( from subiquitycore.controller import (
RepeatedController, RepeatedController,
Skip, Skip,
@ -33,6 +33,7 @@ from subiquitycore.controller import (
from subiquitycore.signals import Signal from subiquitycore.signals import Signal
from subiquitycore.prober import Prober from subiquitycore.prober import Prober
from subiquitycore.ui.frame import SubiquityCoreUI from subiquitycore.ui.frame import SubiquityCoreUI
from subiquitycore.utils import arun_command
log = logging.getLogger('subiquitycore.core') log = logging.getLogger('subiquitycore.core')
@ -415,10 +416,9 @@ class Application:
# there the symptom is that we are running in the foreground but not # there the symptom is that we are running in the foreground but not
# listening to stdin! The fix is the same. # listening to stdin! The fix is the same.
def run(): async def _run():
subprocess.run(cmd, **kw) await arun_command(
cmd, stdin=None, stdout=None, stderr=None)
def restore(fut):
screen.start() screen.start()
urwid.emit_signal( urwid.emit_signal(
screen, urwid.display_common.INPUT_DESCRIPTORS_CHANGED) screen, urwid.display_common.INPUT_DESCRIPTORS_CHANGED)
@ -431,7 +431,7 @@ class Application:
screen, urwid.display_common.INPUT_DESCRIPTORS_CHANGED) screen, urwid.display_common.INPUT_DESCRIPTORS_CHANGED)
if before_hook is not None: if before_hook is not None:
before_hook() before_hook()
self.run_in_bg(run, restore) schedule_task(_run())
def _connect_base_signals(self): def _connect_base_signals(self):
"""Connect signals used in the core controller.""" """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', encoding='utf-8', input=None, errors='replace',
env=None, check=False, **kw): env=None, check=False, **kw):
if input is None: if input is None:
if 'stdin' not in kw:
kw['stdin'] = subprocess.DEVNULL kw['stdin'] = subprocess.DEVNULL
else: else:
kw['stdin'] = subprocess.PIPE 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) *cmd, stdout=stdout, stderr=stderr, env=_clean_env(env), **kw)
stdout, stderr = await proc.communicate(input=input) stdout, stderr = await proc.communicate(input=input)
if encoding: if encoding:
if stdout is not None:
stdout = stdout.decode(encoding) stdout = stdout.decode(encoding)
if stderr is not None:
stderr = stderr.decode(encoding) stderr = stderr.decode(encoding)
log.debug("arun_command %s exited with code %s", cmd, proc.returncode) log.debug("arun_command %s exited with code %s", cmd, proc.returncode)
if check and proc.returncode != 0: if check and proc.returncode != 0: