Skip to content

Commit

Permalink
Merge branch 'main' into tsmorph-step
Browse files Browse the repository at this point in the history
  • Loading branch information
codelion committed Aug 30, 2024
2 parents d1f214a + 458a6ba commit 46bb2dc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
61 changes: 36 additions & 25 deletions patchwork/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import importlib
import importlib.util
import json
import signal
import traceback
from collections import deque
from pathlib import Path
Expand Down Expand Up @@ -57,6 +58,39 @@ def list_option_callback(ctx: click.Context, param: click.Parameter, value: str
ctx.exit()


def find_patchflow(possible_module_paths: Iterable[str], patchflow: str) -> Any | None:
for module_path in possible_module_paths:
try:
spec = importlib.util.spec_from_file_location("custom_module", module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
logger.info(f'Patchflow `{patchflow}` loaded from "{module_path}"')
return getattr(module, patchflow)
except AttributeError:
logger.debug(f"Patchflow {patchflow} not found in {module_path}")
except Exception:
logger.debug(f"Patchflow {patchflow} not found as a file/directory in {module_path}")

try:
module = importlib.import_module(module_path)
logger.info(f"Patchflow {patchflow} loaded from {module_path}")
return getattr(module, patchflow)
except ModuleNotFoundError:
logger.debug(f"Patchflow {patchflow} not found as a module in {module_path}")
except AttributeError:
logger.debug(f"Patchflow {patchflow} not found in {module_path}")

return None


def setup_cli():
def sigint_handler(signum, frame):
logger.info("Received SIGINT, exiting")
exit(1)

signal.signal(signal.SIGINT, sigint_handler)


@click.command(
context_settings=dict(
ignore_unknown_options=True,
Expand Down Expand Up @@ -120,6 +154,8 @@ def cli(
patched_api_key: str | None,
disable_telemetry: bool,
):
setup_cli()

if "::" in patchflow:
module_path, _, patchflow_name = patchflow.partition("::")
else:
Expand Down Expand Up @@ -203,30 +239,5 @@ def cli(
file.write(serialize(inputs))


def find_patchflow(possible_module_paths: Iterable[str], patchflow: str) -> Any | None:
for module_path in possible_module_paths:
try:
spec = importlib.util.spec_from_file_location("custom_module", module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
logger.info(f'Patchflow `{patchflow}` loaded from "{module_path}"')
return getattr(module, patchflow)
except AttributeError:
logger.debug(f"Patchflow {patchflow} not found in {module_path}")
except Exception:
logger.debug(f"Patchflow {patchflow} not found as a file/directory in {module_path}")

try:
module = importlib.import_module(module_path)
logger.info(f"Patchflow {patchflow} loaded from {module_path}")
return getattr(module, patchflow)
except ModuleNotFoundError:
logger.debug(f"Patchflow {patchflow} not found as a module in {module_path}")
except AttributeError:
logger.debug(f"Patchflow {patchflow} not found in {module_path}")

return None


if __name__ == "__main__":
cli()
11 changes: 8 additions & 3 deletions patchwork/steps/CallCode2Prompt/CallCode2Prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import subprocess
from pathlib import Path

from patchwork.logger import logger
from patchwork.step import Step, StepStatus

FOLDER_PATH = "folder_path"
Expand All @@ -15,7 +16,9 @@ def __init__(self, inputs: dict):
if not all(key in inputs.keys() for key in self.required_keys):
raise ValueError(f'Missing required data: "{self.required_keys}"')

self.folder_path = inputs[FOLDER_PATH]
self.folder_path = Path(inputs[FOLDER_PATH])
if not self.folder_path.is_dir():
raise ValueError(f"Folder path does not exist: {self.folder_path}")
self.filter = inputs.get("filter", None)
self.suppress_comments = inputs.get("suppress_comments", False)
self.markdown_file_name = inputs.get("markdown_file_name", "README.md")
Expand Down Expand Up @@ -51,8 +54,10 @@ def run(self) -> dict:
with open(self.code_file_path, "r") as file:
file_content = file.read()
except FileNotFoundError:
self.set_status(StepStatus.FAILED, f"Unable to find file: {self.code_file_path}")
return dict(files_to_patch=[])
logger.info(f"Unable to find file: {self.code_file_path}")
return dict(files_to_patch=[
dict(uri=self.code_file_path, fullContent=prompt_content_md)
])

lines = file_content.splitlines(keepends=True)

Expand Down
2 changes: 1 addition & 1 deletion patchwork/steps/ScanSemgrep/typed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class ScanSemgrepInputs(TypedDict, total=False):
sarif_file_path: Annotated[str, StepTypeConfig(is_config=True, is_path=True)]
sarif_values: Dict
sarif_values: str
semgrep_extra_args: Annotated[str, StepTypeConfig(is_config=True)]


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "patchwork-cli"
version = "0.0.54"
version = "0.0.55"
description = ""
authors = ["patched.codes"]
license = "AGPL"
Expand Down

0 comments on commit 46bb2dc

Please sign in to comment.