diff --git a/pilot/.env.example b/pilot/.env.example index 3da7e032..b406f48a 100644 --- a/pilot/.env.example +++ b/pilot/.env.example @@ -28,3 +28,6 @@ DB_USER= DB_PASSWORD= USE_GPTPILOT_FOLDER=true + +# Load database imported from another location/system - EXPERIMENTAL +# AUTOFIX_FILE_PATHS=false diff --git a/pilot/database/models/files.py b/pilot/database/models/files.py index f956347b..301e151e 100644 --- a/pilot/database/models/files.py +++ b/pilot/database/models/files.py @@ -1,3 +1,5 @@ +from pathlib import Path +from os.path import commonprefix, join, sep from peewee import AutoField, CharField, TextField, ForeignKeyField from database.models.components.base_models import BaseModel @@ -16,3 +18,36 @@ class File(BaseModel): indexes = ( (('app', 'name', 'path'), True), ) + + @staticmethod + def update_paths(): + workspace_dir = Path(__file__).parent.parent.parent.parent / "workspace" + if not workspace_dir.exists(): + # This should only happen on first run + return + + paths = [file.full_path for file in File.select(File.full_path).distinct()] + if not paths: + # No paths in the database, so nothing to fix + return + + common_prefix = commonprefix(paths) + if commonprefix([common_prefix, str(workspace_dir)]) == str(workspace_dir): + # Paths are up to date, nothing to fix + return + + common_sep = "\\" if ":\\" in common_prefix else "/" + common_parts = common_prefix.split(common_sep) + try: + workspace_index = common_parts.index("workspace") + except ValueError: + # There's something strange going on, better not touch anything + return + old_prefix = common_sep.join(common_parts[:workspace_index + 1]) + + print(f"Updating file paths from {old_prefix} to {workspace_dir}") + for file in File.select().where(File.full_path.startswith(old_prefix)): + parts = file.full_path.split(common_sep) + new_path = str(workspace_dir) + sep + sep.join(parts[workspace_index + 1:]) + file.full_path = new_path + file.save() diff --git a/pilot/helpers/Project.py b/pilot/helpers/Project.py index 894ace97..125f748f 100644 --- a/pilot/helpers/Project.py +++ b/pilot/helpers/Project.py @@ -80,6 +80,9 @@ class Project: self.development_plan = development_plan self.dot_pilot_gpt = DotGptPilot(log_chat_completions=enable_dot_pilot_gpt) + if os.getenv("AUTOFIX_FILE_PATHS", "").lower() in ["true", "1", "yes"]: + File.update_paths() + def set_root_path(self, root_path: str): self.root_path = root_path self.dot_pilot_gpt.with_root_path(root_path)