Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds std pipe iterator for stdin and stderr #1321

Merged
merged 3 commits into from
May 6, 2024

Conversation

rudolfix
Copy link
Collaborator

@rudolfix rudolfix commented May 5, 2024

Description

An iterator that reads both stdout and stderr line by line from a process run via Venv

Copy link

netlify bot commented May 5, 2024

Deploy Preview for dlt-hub-docs canceled.

Name Link
🔨 Latest commit bb4119c
🔍 Latest deploy log https://app.netlify.com/sites/dlt-hub-docs/deploys/6638c635f2c2d50008c2ce05

@rudolfix rudolfix self-assigned this May 6, 2024
@rudolfix rudolfix added the support This issue is monitored by Solution Engineer label May 6, 2024
@rudolfix rudolfix requested a review from sultaniman May 6, 2024 11:03
@@ -1,3 +1,4 @@
import sys
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this import added by accident?

@@ -24,6 +25,45 @@ def exec_to_stdout(f: AnyFun) -> Iterator[Any]:
print(encode_obj(rv), flush=True)


def iter_std(venv: Venv, command: str, *script_args: Any) -> Iterator[Tuple[int, str]]:
"""Starts a process `command` with `script_args` in environment `venv` and returns iterator
of (filno, line) tuples where `fileno` is 1 for stdout and 2 for stderr. `line` is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should create a simple enum to denote 1 for stdout and 2 as sdterr.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will create a Literal with those codes, they correspond to unix file numbers for std so IMO they are fine

def _r_q(std_: int) -> None:
stream_ = process.stderr if std_ == 2 else process.stdout
for line in iter(stream_.readline, ""):
q_.put((std_, line[:-1] if line[-1] == "\n" else line))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think line.strip() is simpler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can line in theory be None?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think None stops the iterator so no. I could not find it in my tests

t_out.start()
t_err = Thread(target=_r_q, args=(2,), daemon=True)
t_err.start()
while line := q_.get():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have some hard timeout after it that we shut down or do we just let users use Ctrl+C?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not now I think. adding timeout is trivial if we need it

@@ -24,6 +25,45 @@ def exec_to_stdout(f: AnyFun) -> Iterator[Any]:
print(encode_obj(rv), flush=True)


def iter_std(venv: Venv, command: str, *script_args: Any) -> Iterator[Tuple[int, str]]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that shlex is not windows friendly, I am just wondering if we should sanitize script arguments, wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what I should do here? @sultaniman

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just a security related concern that something might get injected via arguments which will lead RCE or LCE

@rudolfix rudolfix requested a review from sultaniman May 6, 2024 11:59
Copy link
Contributor

@sultaniman sultaniman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@rudolfix rudolfix merged commit b4a736c into devel May 6, 2024
49 of 50 checks passed
@rudolfix rudolfix deleted the rfix/improves-std-iterator branch May 6, 2024 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support This issue is monitored by Solution Engineer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants