Skip to content

Commit

Permalink
Move run_with_logs into utils. (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohinb2 authored Jul 4, 2024
1 parent 4185ee1 commit 64fde37
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 53 deletions.
7 changes: 2 additions & 5 deletions runhouse/resources/envs/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
from runhouse.globals import obj_store

from runhouse.logger import logger
from runhouse.resources.envs.utils import (
_process_env_vars,
run_setup_command,
run_with_logs,
)
from runhouse.resources.envs.utils import _process_env_vars, run_setup_command
from runhouse.resources.folders import Folder
from runhouse.resources.hardware import _get_cluster_from, Cluster
from runhouse.resources.packages import Package
from runhouse.resources.resource import Resource
from runhouse.utils import run_with_logs


class Env(Resource):
Expand Down
49 changes: 1 addition & 48 deletions runhouse/resources/envs/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import subprocess
import sys

from pathlib import Path
from typing import Dict, List
Expand All @@ -10,7 +9,7 @@
from runhouse.constants import CONDA_INSTALL_CMDS, EMPTY_DEFAULT_ENV_NAME
from runhouse.globals import rns_client
from runhouse.resources.resource import Resource
from runhouse.utils import locate_working_dir
from runhouse.utils import locate_working_dir, run_with_logs


def _process_reqs(reqs):
Expand Down Expand Up @@ -124,52 +123,6 @@ def _env_vars_from_file(env_file):


# ------- Installation helpers -------


def run_with_logs(cmd: str, **kwargs):
"""Runs a command and prints the output to sys.stdout.
We can't just pipe to sys.stdout, and when in a `call` method
we overwrite sys.stdout with a multi-logger to a file and stdout.
Args:
cmd: The command to run.
kwargs: Keyword arguments to pass to subprocess.Popen.
Returns:
The returncode of the command.
"""
require_outputs = kwargs.pop("require_outputs", False)
stream_logs = kwargs.pop("stream_logs", True)

p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
shell=True,
**kwargs,
)

out = ""
if stream_logs:
while True:
line = p.stdout.readline()
if line == "" and p.poll() is not None:
break
sys.stdout.write(line)
sys.stdout.flush()
if require_outputs:
out += line

stdout, stderr = p.communicate()

if require_outputs:
stdout = stdout or out
return p.returncode, stdout, stderr

return p.returncode


def run_setup_command(
cmd: str,
cluster: "Cluster" = None,
Expand Down
48 changes: 48 additions & 0 deletions runhouse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import inspect
import logging
import os
import subprocess
import sys
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
Expand All @@ -13,6 +14,53 @@

logger = logging.getLogger(__name__)

####################################################################################################
# Simple running utility
####################################################################################################
def run_with_logs(cmd: str, **kwargs):
"""Runs a command and prints the output to sys.stdout.
We can't just pipe to sys.stdout, and when in a `call` method
we overwrite sys.stdout with a multi-logger to a file and stdout.
Args:
cmd: The command to run.
kwargs: Keyword arguments to pass to subprocess.Popen.
Returns:
The returncode of the command.
"""
require_outputs = kwargs.pop("require_outputs", False)
stream_logs = kwargs.pop("stream_logs", True)

p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
shell=True,
**kwargs,
)

out = ""
if stream_logs:
while True:
line = p.stdout.readline()
if line == "" and p.poll() is not None:
break
sys.stdout.write(line)
sys.stdout.flush()
if require_outputs:
out += line

stdout, stderr = p.communicate()

if require_outputs:
stdout = stdout or out
return p.returncode, stdout, stderr

return p.returncode


####################################################################################################
# Module discovery and import logic
####################################################################################################
Expand Down

0 comments on commit 64fde37

Please sign in to comment.