-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add new steps * update init file * change command to shell * bugfixes and changes * lock update * update callsql * make username optional since some dbs take username in connect args instead * fix test and filter url kwargs by none * auto parse dict inputs * bump version * lint and update deps
- Loading branch information
Showing
18 changed files
with
1,041 additions
and
614 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
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,57 @@ | ||
from __future__ import annotations | ||
|
||
from sqlalchemy import URL, create_engine, exc, text | ||
|
||
from patchwork.common.utils.input_parsing import parse_to_dict | ||
from patchwork.common.utils.utils import mustache_render | ||
from patchwork.logger import logger | ||
from patchwork.step import Step, StepStatus | ||
from patchwork.steps.CallSQL.typed import CallSQLInputs, CallSQLOutputs | ||
|
||
|
||
class CallSQL(Step, input_class=CallSQLInputs, output_class=CallSQLOutputs): | ||
def __init__(self, inputs: dict): | ||
super().__init__(inputs) | ||
query_template_data = inputs.get("db_query_template_values", {}) | ||
self.query = mustache_render(inputs["db_query"], query_template_data) | ||
self.__build_engine(inputs) | ||
|
||
def __build_engine(self, inputs: dict): | ||
dialect = inputs["db_dialect"] | ||
driver = inputs.get("db_driver") | ||
dialect_plus_driver = f"{dialect}+{driver}" if driver is not None else dialect | ||
kwargs = dict( | ||
username=inputs.get("db_username"), | ||
host=inputs.get("db_host", "localhost"), | ||
port=inputs.get("db_port", 5432), | ||
password=inputs.get("db_password"), | ||
database=inputs.get("db_database"), | ||
query=parse_to_dict(inputs.get("db_params")), | ||
) | ||
connection_url = URL.create( | ||
dialect_plus_driver, | ||
**{k: v for k, v in kwargs.items() if v is not None}, | ||
) | ||
|
||
connect_args = None | ||
if inputs.get("db_driver_args") is not None: | ||
connect_args = parse_to_dict(inputs.get("db_driver_args")) | ||
|
||
self.engine = create_engine(connection_url, connect_args=connect_args) | ||
with self.engine.connect() as conn: | ||
conn.execute(text("SELECT 1")) | ||
return self.engine | ||
|
||
def run(self) -> dict: | ||
try: | ||
rv = [] | ||
with self.engine.begin() as conn: | ||
cursor = conn.execute(text(self.query)) | ||
for row in cursor: | ||
result = row._asdict() | ||
rv.append(result) | ||
logger.info(f"Retrieved {len(rv)} rows!") | ||
return dict(results=rv) | ||
except exc.InvalidRequestError as e: | ||
self.set_status(StepStatus.FAILED, f"`{self.query}` failed with message:\n{e}") | ||
return dict(results=[]) |
Empty file.
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,24 @@ | ||
from __future__ import annotations | ||
|
||
from typing_extensions import Any, TypedDict | ||
|
||
|
||
class __RequiredCallSQLInputs(TypedDict): | ||
db_dialect: str | ||
db_query: str | ||
|
||
|
||
class CallSQLInputs(__RequiredCallSQLInputs, total=False): | ||
db_driver: str | ||
db_username: str | ||
db_password: str | ||
db_host: str | ||
db_port: int | ||
db_name: str | ||
db_params: dict[str, Any] | ||
db_driver_args: dict[str, Any] | ||
db_query_template_values: dict[str, Any] | ||
|
||
|
||
class CallSQLOutputs(TypedDict): | ||
results: list[dict[str, Any]] |
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,58 @@ | ||
from __future__ import annotations | ||
|
||
import shlex | ||
import subprocess | ||
from pathlib import Path | ||
|
||
from patchwork.common.utils.utils import mustache_render | ||
from patchwork.logger import logger | ||
from patchwork.step import Step, StepStatus | ||
from patchwork.steps.CallShell.typed import CallShellInputs, CallShellOutputs | ||
|
||
|
||
class CallShell(Step, input_class=CallShellInputs, output_class=CallShellOutputs): | ||
def __init__(self, inputs: dict): | ||
super().__init__(inputs) | ||
script_template_values = inputs.get("script_template_values", {}) | ||
self.script = mustache_render(inputs["script"], script_template_values) | ||
self.working_dir = inputs.get("working_dir", Path.cwd()) | ||
self.env = self.__parse_env_text(inputs.get("env", "")) | ||
|
||
@staticmethod | ||
def __parse_env_text(env_text: str) -> dict[str, str]: | ||
env_spliter = shlex.shlex(env_text, posix=True) | ||
env_spliter.whitespace_split = True | ||
env_spliter.whitespace += ";" | ||
|
||
env: dict[str, str] = dict() | ||
for env_assign in env_spliter: | ||
env_assign_spliter = shlex.shlex(env_assign, posix=True) | ||
env_assign_spliter.whitespace_split = True | ||
env_assign_spliter.whitespace += "=" | ||
env_parts = list(env_assign_spliter) | ||
if len(env_parts) < 1: | ||
continue | ||
|
||
env_assign_target = env_parts[0] | ||
if len(env_parts) < 2: | ||
logger.error(f"{env_assign_target} is not assigned anything, skipping...") | ||
continue | ||
if len(env_parts) > 2: | ||
logger.error(f"{env_assign_target} has more than 1 assignment, skipping...") | ||
continue | ||
env[env_assign_target] = env_parts[1] | ||
|
||
return env | ||
|
||
def run(self) -> dict: | ||
p = subprocess.run(self.script, shell=True, capture_output=True, text=True, cwd=self.working_dir, env=self.env) | ||
try: | ||
p.check_returncode() | ||
except subprocess.CalledProcessError as e: | ||
self.set_status( | ||
StepStatus.FAILED, | ||
f"Script failed.", | ||
) | ||
logger.info(f"stdout: \n{p.stdout}") | ||
logger.info(f"stderr:\n{p.stderr}") | ||
return dict(stdout_output=p.stdout, stderr_output=p.stderr) |
Empty file.
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 __future__ import annotations | ||
|
||
from typing_extensions import Annotated, Any, TypedDict | ||
|
||
from patchwork.common.utils.step_typing import StepTypeConfig | ||
|
||
|
||
class __RequiredCallShellInputs(TypedDict): | ||
script: str | ||
|
||
|
||
class CallShellInputs(__RequiredCallShellInputs, total=False): | ||
working_dir: Annotated[str, StepTypeConfig(is_path=True)] | ||
env: str | ||
script_template_values: dict[str, Any] | ||
|
||
|
||
class CallShellOutputs(TypedDict): | ||
stdout_output: str |
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.