|
| 1 | +"""Attachment-backed workspace hydration.""" |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import os |
| 5 | +from datetime import datetime, timezone |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any |
| 8 | +from uuid import UUID |
| 9 | + |
| 10 | +from pydantic import BaseModel |
| 11 | +from uipath.core.workspace import AttachmentsProtocol, JobsProtocol |
| 12 | + |
| 13 | + |
| 14 | +class AttachmentRegistryEntry(BaseModel): |
| 15 | + """Registry entry for one workspace file attachment.""" |
| 16 | + |
| 17 | + attachment_key: str |
| 18 | + sha256: str |
| 19 | + size: int |
| 20 | + uploaded_at: str |
| 21 | + attachment_name: str | None = None |
| 22 | + |
| 23 | + |
| 24 | +class WorkspaceHydrator: |
| 25 | + """Hydrates/dehydrates a workspace directory through job attachments.""" |
| 26 | + |
| 27 | + DEFAULT_ATTACHMENT_PREFIX = ".uipath-workspace/" |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + *, |
| 32 | + workspace_path: str | Path, |
| 33 | + attachments: AttachmentsProtocol, |
| 34 | + jobs: JobsProtocol | None = None, |
| 35 | + current_job_key: str | None = None, |
| 36 | + folder_key: str | None = None, |
| 37 | + folder_path: str | None = None, |
| 38 | + attachment_prefix: str = DEFAULT_ATTACHMENT_PREFIX, |
| 39 | + ): |
| 40 | + """Initialize the hydrator.""" |
| 41 | + self.workspace_path = Path(workspace_path) |
| 42 | + self.attachments = attachments |
| 43 | + self.jobs = jobs |
| 44 | + self.current_job_key = current_job_key |
| 45 | + self.folder_key = folder_key |
| 46 | + self.folder_path = folder_path |
| 47 | + self.attachment_prefix = attachment_prefix.strip("/") |
| 48 | + |
| 49 | + async def hydrate( |
| 50 | + self, |
| 51 | + registry: dict[str, dict[str, Any]], |
| 52 | + ) -> dict[str, dict[str, Any]]: |
| 53 | + """Download registry files into the workspace. |
| 54 | +
|
| 55 | + Files with matching SHA-256 are left untouched. |
| 56 | + """ |
| 57 | + normalized = self._normalize_registry(registry) |
| 58 | + for virtual_path, entry in normalized.items(): |
| 59 | + target = self._resolve_workspace_path(virtual_path) |
| 60 | + if target.exists() and self._sha256(target) == entry.sha256: |
| 61 | + continue |
| 62 | + target.parent.mkdir(parents=True, exist_ok=True) |
| 63 | + await self.attachments.download_async( |
| 64 | + key=UUID(entry.attachment_key), |
| 65 | + destination_path=str(target), |
| 66 | + folder_key=self.folder_key, |
| 67 | + folder_path=self.folder_path, |
| 68 | + ) |
| 69 | + return self._dump_registry(normalized) |
| 70 | + |
| 71 | + async def dehydrate( |
| 72 | + self, |
| 73 | + registry: dict[str, dict[str, Any]], |
| 74 | + ) -> dict[str, dict[str, Any]]: |
| 75 | + """Upload new/changed workspace files and return the merged registry. |
| 76 | +
|
| 77 | + The result is rebuilt from the files currently present, so entries for |
| 78 | + files deleted locally are dropped and not restored on the next hydrate. |
| 79 | + """ |
| 80 | + previous = self._normalize_registry(registry) |
| 81 | + current: dict[str, AttachmentRegistryEntry] = {} |
| 82 | + for file_path in self._iter_files(): |
| 83 | + virtual_path = self._virtual_path(file_path) |
| 84 | + digest = self._sha256(file_path) |
| 85 | + size = file_path.stat().st_size |
| 86 | + existing = previous.get(virtual_path) |
| 87 | + |
| 88 | + if existing and existing.sha256 == digest and existing.size == size: |
| 89 | + current[virtual_path] = existing |
| 90 | + if self.current_job_key: |
| 91 | + await self.link_attachment(existing.attachment_key) |
| 92 | + continue |
| 93 | + |
| 94 | + attachment_name = self._attachment_name_for_virtual_path(virtual_path) |
| 95 | + attachment_key = await self.attachments.upload_async( |
| 96 | + name=attachment_name, |
| 97 | + source_path=str(file_path), |
| 98 | + folder_key=self.folder_key, |
| 99 | + folder_path=self.folder_path, |
| 100 | + ) |
| 101 | + entry = AttachmentRegistryEntry( |
| 102 | + attachment_key=str(attachment_key), |
| 103 | + sha256=digest, |
| 104 | + size=size, |
| 105 | + uploaded_at=datetime.now(timezone.utc).isoformat(), |
| 106 | + attachment_name=attachment_name, |
| 107 | + ) |
| 108 | + current[virtual_path] = entry |
| 109 | + |
| 110 | + if self.current_job_key: |
| 111 | + await self.link_attachment(entry.attachment_key) |
| 112 | + |
| 113 | + return self._dump_registry(current) |
| 114 | + |
| 115 | + async def link_attachment(self, attachment_key: str) -> None: |
| 116 | + """Link an already uploaded attachment to the current job.""" |
| 117 | + if self.jobs is None or not self.current_job_key: |
| 118 | + return |
| 119 | + |
| 120 | + await self.jobs.link_attachment_async( |
| 121 | + job_key=UUID(self.current_job_key), |
| 122 | + attachment_key=UUID(attachment_key), |
| 123 | + folder_key=self.folder_key, |
| 124 | + folder_path=self.folder_path, |
| 125 | + ) |
| 126 | + |
| 127 | + def _iter_files(self) -> list[Path]: |
| 128 | + if not self.workspace_path.exists(): |
| 129 | + return [] |
| 130 | + |
| 131 | + files: list[Path] = [] |
| 132 | + for root, _, names in os.walk(self.workspace_path): |
| 133 | + for name in names: |
| 134 | + path = Path(root) / name |
| 135 | + if path.is_file(): |
| 136 | + files.append(path) |
| 137 | + return sorted(files) |
| 138 | + |
| 139 | + def _resolve_workspace_path(self, virtual_path: str) -> Path: |
| 140 | + path = (self.workspace_path / virtual_path).resolve() |
| 141 | + workspace = self.workspace_path.resolve() |
| 142 | + if workspace != path and workspace not in path.parents: |
| 143 | + raise ValueError(f"Workspace path escapes root: {virtual_path}") |
| 144 | + return path |
| 145 | + |
| 146 | + def _virtual_path(self, path: Path) -> str: |
| 147 | + return path.relative_to(self.workspace_path).as_posix() |
| 148 | + |
| 149 | + @staticmethod |
| 150 | + def _escape(value: str) -> str: |
| 151 | + # json-pointer escaping; escape ~ before / so they can't collide |
| 152 | + return value.replace("~", "~0").replace("/", "~1") |
| 153 | + |
| 154 | + @staticmethod |
| 155 | + def _unescape(value: str) -> str: |
| 156 | + return value.replace("~1", "/").replace("~0", "~") |
| 157 | + |
| 158 | + def _attachment_name_for_virtual_path(self, virtual_path: str) -> str: |
| 159 | + # the platform mishandles "/" in attachment names, so keep it slash-free |
| 160 | + return self._escape(f"{self.attachment_prefix}/{virtual_path}") |
| 161 | + |
| 162 | + def _virtual_path_from_attachment_name(self, name: str) -> str | None: |
| 163 | + decoded = self._unescape(name) |
| 164 | + prefix = f"{self.attachment_prefix}/" |
| 165 | + if not decoded.startswith(prefix): |
| 166 | + return None |
| 167 | + virtual_path = decoded[len(prefix) :] |
| 168 | + self._resolve_workspace_path(virtual_path) |
| 169 | + return virtual_path |
| 170 | + |
| 171 | + def _normalize_registry( |
| 172 | + self, registry: dict[str, dict[str, Any]] |
| 173 | + ) -> dict[str, AttachmentRegistryEntry]: |
| 174 | + normalized: dict[str, AttachmentRegistryEntry] = {} |
| 175 | + for virtual_path, entry in registry.items(): |
| 176 | + self._resolve_workspace_path(virtual_path) |
| 177 | + normalized[virtual_path] = AttachmentRegistryEntry.model_validate(entry) |
| 178 | + return normalized |
| 179 | + |
| 180 | + def _dump_registry( |
| 181 | + self, registry: dict[str, AttachmentRegistryEntry] |
| 182 | + ) -> dict[str, dict[str, Any]]: |
| 183 | + return {path: entry.model_dump() for path, entry in registry.items()} |
| 184 | + |
| 185 | + def _sha256(self, path: Path) -> str: |
| 186 | + digest = hashlib.sha256() |
| 187 | + with path.open("rb") as file: |
| 188 | + for chunk in iter(lambda: file.read(1024 * 1024), b""): |
| 189 | + digest.update(chunk) |
| 190 | + return digest.hexdigest() |
0 commit comments