Skip to content

Commit

Permalink
IO updates (#5)
Browse files Browse the repository at this point in the history
* Always log to stderr. Valid uses of the CLI will be watching for stdout and we don't want to pollute it with logs.
* Allow overriding stdout and stderr in shell functions. Sometimes we don't care about the output at all.
* Defer some imports to better enable individual tool use.
  • Loading branch information
shreve authored Jun 12, 2024
1 parent d9e4c90 commit bcf5ce5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
26 changes: 16 additions & 10 deletions src/mads/build/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,33 @@ def _stream_process(cmd, **kwargs):
cmd,
encoding="utf-8",
stdin=subprocess.PIPE if "input" in kwargs else subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=kwargs.get("stdout", subprocess.PIPE),
stderr=kwargs.get("stderr", subprocess.PIPE),
shell=True,
cwd=kwargs.get("cwd"),
) as process:
stdout = ""
stderr = ""

if kwargs.get("input"):
process.stdin.write(kwargs["input"])
process.stdin.close()
# Configure the IO/streams
if process.stdin:
if kwargs.get("input"):
process.stdin.write(kwargs["input"])
process.stdin.close()

# Don't block on stdout/stderr
os.set_blocking(process.stdout.fileno(), False)
os.set_blocking(process.stderr.fileno(), False)
if process.stdout:
os.set_blocking(process.stdout.fileno(), False)
if process.stderr:
os.set_blocking(process.stderr.fileno(), False)

# Collect the output streams
stdout = ""
stderr = ""

def communicate(proc):
"""Read stdin and stdout from a process as streams."""
nonlocal stdout, stderr

outputs = [proc.stdout, proc.stderr]
outputs = list(filter(None, [proc.stdout, proc.stderr]))

# Select will return a list of only files ready to read from
ready, _, _ = select.select(outputs, [], [], 0.0001)
Expand Down
3 changes: 2 additions & 1 deletion src/mads/environ/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

from pydantic_settings import BaseSettings

from mads.build.shell import shell

NOT_GIT = {"message": "[This does not appear to be a git project]"}


@cache
def git_config_source() -> dict[str, str]:
from mads.build.shell import shell

cwd = Path(".git")

# If the git cache is populated, .git is actually a file pointing to the
Expand Down
4 changes: 2 additions & 2 deletions src/mads/environ/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from pydantic import Field
from pydantic_settings import BaseSettings

from mads.build import log, shell


class Resources(BaseSettings):
"""Get information about the current system we're running in."""
Expand Down Expand Up @@ -39,6 +37,8 @@ class Resources(BaseSettings):
def enable_swap(self):
"""Enable swap memory."""

from mads.build import log, shell

if self.swap_memory > 0:
log.info("Swap memory is already enabled.")
return True
Expand Down

0 comments on commit bcf5ce5

Please sign in to comment.