Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,41 @@

from __future__ import annotations

import logging
import os
import shlex
import subprocess

from airflow.utils.process_utils import execute_in_subprocess

def _execute_in_subprocess(cmd: list[str], cwd: str | None = None, env: dict[str, str] | None = None) -> None:
"""
Execute a process and stream output to logger.

:param cmd: command and arguments to run
:param cwd: Current working directory passed to the Popen constructor
:param env: Additional environment variables to set for the subprocess.
"""
log = logging.getLogger(__name__)

log.info("Executing cmd: %s", " ".join(shlex.quote(c) for c in cmd))
with subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=0,
close_fds=True,
cwd=cwd,
env=env,
) as proc:
log.info("Output:")
if proc.stdout:
with proc.stdout:
for line in iter(proc.stdout.readline, b""):
log.info("%s", line.decode().rstrip())

exit_code = proc.wait()
if exit_code != 0:
raise subprocess.CalledProcessError(exit_code, cmd)


def init_module(go_module_name: str, go_module_path: str) -> None:
Expand All @@ -36,7 +68,7 @@ def init_module(go_module_name: str, go_module_path: str) -> None:
if os.path.isfile(os.path.join(go_module_path, "go.mod")):
return
go_mod_init_cmd = ["go", "mod", "init", go_module_name]
execute_in_subprocess(go_mod_init_cmd, cwd=go_module_path)
_execute_in_subprocess(go_mod_init_cmd, cwd=go_module_path)


def install_dependencies(go_module_path: str) -> None:
Expand All @@ -46,4 +78,4 @@ def install_dependencies(go_module_path: str) -> None:
:param go_module_path: The path to the directory containing the Go module.
"""
go_mod_tidy = ["go", "mod", "tidy"]
execute_in_subprocess(go_mod_tidy, cwd=go_module_path)
_execute_in_subprocess(go_mod_tidy, cwd=go_module_path)
4 changes: 2 additions & 2 deletions providers/google/tests/unit/google/test_go_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@


class TestGoModule:
@mock.patch("airflow.providers.google.go_module_utils.execute_in_subprocess")
@mock.patch("airflow.providers.google.go_module_utils._execute_in_subprocess")
def test_should_init_go_module(self, mock_execute_in_subprocess):
init_module(go_module_name="example.com/main", go_module_path="/home/example/go")
mock_execute_in_subprocess.assert_called_once_with(
["go", "mod", "init", "example.com/main"], cwd="/home/example/go"
)

@mock.patch("airflow.providers.google.go_module_utils.execute_in_subprocess")
@mock.patch("airflow.providers.google.go_module_utils._execute_in_subprocess")
def test_should_install_module_dependencies(self, mock_execute_in_subprocess):
install_dependencies(go_module_path="/home/example/go")
mock_execute_in_subprocess.assert_called_once_with(["go", "mod", "tidy"], cwd="/home/example/go")