kopia lustrzana https://github.com/biobootloader/wolverine
Added Proper Logging
all commands are logged in a log file with nicely formatted context and datetimepull/25/head
rodzic
270ac7af27
commit
fa84b1e7de
|
@ -1,2 +1,3 @@
|
||||||
venv
|
venv
|
||||||
openai_key.txt
|
openai_key.txt
|
||||||
|
wolverine.log
|
18
wolverine.py
18
wolverine.py
|
@ -6,6 +6,7 @@ import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import openai
|
import openai
|
||||||
|
import logging
|
||||||
from termcolor import cprint
|
from termcolor import cprint
|
||||||
|
|
||||||
# Load the OpenAI API key
|
# Load the OpenAI API key
|
||||||
|
@ -13,6 +14,15 @@ def load_openai_key():
|
||||||
with open("openai_key.txt") as f:
|
with open("openai_key.txt") as f:
|
||||||
return f.read().strip()
|
return f.read().strip()
|
||||||
|
|
||||||
|
# Set up logging
|
||||||
|
def configure_logging():
|
||||||
|
logging.basicConfig(
|
||||||
|
filename="wolverine.log",
|
||||||
|
format="%(asctime)s - %(levelname)s - %(message)s",
|
||||||
|
level=logging.INFO,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# Run the provided script and return the output and return code
|
# Run the provided script and return the output and return code
|
||||||
def run_script(script_name, script_args):
|
def run_script(script_name, script_args):
|
||||||
script_args = [str(arg) for arg in script_args]
|
script_args = [str(arg) for arg in script_args]
|
||||||
|
@ -134,8 +144,10 @@ def apply_change_interactive(file_path, change):
|
||||||
if decision == "y":
|
if decision == "y":
|
||||||
with open(file_path, "w") as f:
|
with open(file_path, "w") as f:
|
||||||
f.writelines(file_lines)
|
f.writelines(file_lines)
|
||||||
|
logging.info(f"Applied change: {change}")
|
||||||
return True
|
return True
|
||||||
elif decision == "n":
|
elif decision == "n":
|
||||||
|
logging.info(f"Rejected change: {change}")
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
print("Invalid input. Please enter 'y' or 'n'.")
|
print("Invalid input. Please enter 'y' or 'n'.")
|
||||||
|
@ -172,9 +184,11 @@ def main(script_name, *script_args, revert=False, model="gpt-4", interactive=Fal
|
||||||
if returncode == 0:
|
if returncode == 0:
|
||||||
cprint("Script ran successfully.", "blue")
|
cprint("Script ran successfully.", "blue")
|
||||||
print("Output:", output)
|
print("Output:", output)
|
||||||
|
logging.info("Script ran successfully.")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
cprint("Script crashed. Trying to fix...", "blue")
|
cprint("Script crashed. Trying to fix...", "blue")
|
||||||
|
logging.error(f"Script crashed with return code {returncode}.")
|
||||||
print("Output:", output)
|
print("Output:", output)
|
||||||
|
|
||||||
json_response = send_error_to_gpt(
|
json_response = send_error_to_gpt(
|
||||||
|
@ -196,17 +210,21 @@ def main(script_name, *script_args, revert=False, model="gpt-4", interactive=Fal
|
||||||
else:
|
else:
|
||||||
cprint("Change rejected.", "red")
|
cprint("Change rejected.", "red")
|
||||||
cprint("Finished applying changes. Rerunning...", "blue")
|
cprint("Finished applying changes. Rerunning...", "blue")
|
||||||
|
logging.info("Finished applying changes in interactive mode.")
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
apply_changes(script_name, json_response)
|
apply_changes(script_name, json_response)
|
||||||
cprint("Changes applied. Rerunning...", "blue")
|
cprint("Changes applied. Rerunning...", "blue")
|
||||||
|
logging.info("Changes applied.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(f"Failed to fix the script: {str(e)}")
|
raise Exception(f"Failed to fix the script: {str(e)}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
configure_logging()
|
||||||
try:
|
try:
|
||||||
fire.Fire(main)
|
fire.Fire(main)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
|
logging.error(f"Error: {str(e)}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue