Skip to content
Merged
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
36 changes: 35 additions & 1 deletion weco/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,39 @@ def smooth_update(


# Other helper functions
DEFAULT_MAX_LINES = 50
DEFAULT_MAX_CHARS = 5000


def truncate_output(output: str, max_lines: int = DEFAULT_MAX_LINES, max_chars: int = DEFAULT_MAX_CHARS) -> str:
"""Truncate the output to a reasonable size."""
lines = output.splitlines()

# Determine what truncations are needed based on original output
lines_truncated = len(lines) > max_lines
chars_truncated = len(output) > max_chars

# Apply truncations to the original output
if lines_truncated:
output = "\n".join(lines[-max_lines:])

if chars_truncated:
output = output[-max_chars:]

# Add prefixes for truncations that were applied
prefixes = []
if lines_truncated:
prefixes.append(f"truncated to last {max_lines} lines")
if chars_truncated:
prefixes.append(f"truncated to last {max_chars} characters")

if prefixes:
prefix_text = ", ".join(prefixes)
output = f"... ({prefix_text})\n{output}"

return output


def run_evaluation(eval_command: str) -> str:
"""Run the evaluation command on the code and return the output."""

Expand All @@ -136,7 +169,8 @@ def run_evaluation(eval_command: str) -> str:
if len(output) > 0:
output += "\n"
output += result.stdout
return output

return truncate_output(output)


# Update Check Function
Expand Down