-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from scale-vector/rfix/moves-runners
implements venv management and singer tap source
- Loading branch information
Showing
35 changed files
with
2,025 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import pool_runner | ||
from .pool_runner import TRunArgs, TRunMetrics, initialize_runner, run_pool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from subprocess import PIPE, CalledProcessError | ||
from typing import Any, Iterator | ||
|
||
from dlt.common.runners.venv import Venv | ||
|
||
|
||
def iter_stdout(venv: Venv, command: str, *script_args: Any) -> Iterator[str]: | ||
# start a process in virtual environment, assume that text comes from stdout | ||
process = venv.start_command(command, *script_args, stdout=PIPE, stderr=PIPE, text=True) | ||
exit_code: int = None | ||
|
||
# read all the lines until empty marker is returned | ||
for line in iter(process.stdout.readline, ''): | ||
yield line[:-1] | ||
|
||
# we fail iterator if exit code is not 0 | ||
exit_code = process.wait() | ||
if exit_code != 0: | ||
raise CalledProcessError(exit_code, command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import os | ||
import shutil | ||
import venv | ||
import types | ||
import subprocess | ||
from typing import Any, List, Type | ||
|
||
from dlt.common.exceptions import CannotInstallDependency, VenvNotFound | ||
|
||
|
||
class DLTEnvBuilder(venv.EnvBuilder): | ||
def __init__(self) -> None: | ||
super().__init__(with_pip=True, clear=True) | ||
|
||
def post_setup(self, context: types.SimpleNamespace) -> None: | ||
self.context = context | ||
|
||
|
||
class Venv(): | ||
def __init__(self, context: types.SimpleNamespace) -> None: | ||
self.context = context | ||
|
||
@classmethod | ||
def create(cls, venv_dir: str, dependencies: List[str] = None) -> "Venv": | ||
b = DLTEnvBuilder() | ||
try: | ||
b.create(os.path.abspath(venv_dir)) | ||
if dependencies: | ||
Venv._install_deps(b.context, dependencies) | ||
except: | ||
if os.path.isdir(venv_dir): | ||
shutil.rmtree(venv_dir) | ||
raise | ||
return cls(b.context) | ||
|
||
@classmethod | ||
def restore(cls, venv_dir: str) -> "Venv": | ||
if not os.path.isdir(venv_dir): | ||
raise VenvNotFound(venv_dir) | ||
b = venv.EnvBuilder(clear=False, upgrade=False) | ||
c = b.ensure_directories(os.path.abspath(venv_dir)) | ||
if not os.path.isfile(c.env_exe): | ||
raise VenvNotFound(c.env_exe) | ||
return cls(c) | ||
|
||
def __enter__(self) -> "Venv": | ||
return self | ||
|
||
def __exit__(self, exc_type: Type[BaseException], exc_val: BaseException, exc_tb: types.TracebackType) -> None: | ||
# delete venv | ||
if self.context.env_dir and os.path.isdir(self.context.env_dir): | ||
shutil.rmtree(self.context.env_dir) | ||
|
||
def start_command(self, entry_point: str, *script_args: Any, **popen_kwargs: Any) -> "subprocess.Popen[str]": | ||
command = os.path.join(self.context.bin_path, entry_point) | ||
cmd = [command, *script_args] | ||
return subprocess.Popen(cmd, **popen_kwargs) | ||
|
||
def run_command(self, entry_point: str, *script_args: Any) -> str: | ||
# runs one of installed entry points typically CLIS coming with packages and installed into PATH | ||
command = os.path.join(self.context.bin_path, entry_point) | ||
cmd = [command, *script_args] | ||
return subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode("utf-8") | ||
|
||
def run_script(self, script_path: str, *script_args: Any) -> str: | ||
# os.environ is passed to executed process | ||
cmd = [self.context.env_exe, "-I", os.path.abspath(script_path), *script_args] | ||
try: | ||
return subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode("utf-8") | ||
except subprocess.CalledProcessError as cpe: | ||
if cpe.returncode == 2: | ||
raise FileNotFoundError(script_path) | ||
else: | ||
raise | ||
|
||
def run_module(self, module: str, *module_args: Any) -> str: | ||
cmd = [self.context.env_exe, "-Im", module, *module_args] | ||
return subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode("utf-8") | ||
|
||
def add_dependencies(self, dependencies: List[str] = None) -> None: | ||
Venv._install_deps(self.context, dependencies) | ||
|
||
@staticmethod | ||
def _install_deps(context: types.SimpleNamespace, dependencies: List[str]) -> None: | ||
for dep in dependencies: | ||
cmd = [context.env_exe, "-Im", "pip", "install", dep] | ||
try: | ||
subprocess.check_output(cmd, stderr=subprocess.STDOUT) | ||
except subprocess.CalledProcessError as exc: | ||
raise CannotInstallDependency(dep, context.env_exe, exc.output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.