|
| 1 | +from pathlib import Path |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | + |
| 5 | +def find_dot_dir( |
| 6 | + dir: str, dot_dir: str, root_dir: Optional[str] = None |
| 7 | +) -> Optional[str]: |
| 8 | + """ |
| 9 | + Find the nearest dot directory by traversing up from the given directory. |
| 10 | +
|
| 11 | + Args: |
| 12 | + dir (str): The starting directory path |
| 13 | + dot_dir (str): The dot directory name to search for (e.g., '.jupyter', '.git') |
| 14 | + root_dir (Optional[str]): The root directory to stop searching at. |
| 15 | + If None, searches to filesystem root. |
| 16 | +
|
| 17 | + Returns: |
| 18 | + str: The absolute path to the dot directory if found, None otherwise |
| 19 | +
|
| 20 | + Raises: |
| 21 | + ValueError: If dir is not a directory |
| 22 | + """ |
| 23 | + current_path = Path(dir).resolve() |
| 24 | + |
| 25 | + # Validate that the starting path is a directory |
| 26 | + if not current_path.is_dir(): |
| 27 | + raise ValueError(f"'{dir}' is not a directory") |
| 28 | + |
| 29 | + # Convert root_dir to resolved path if provided |
| 30 | + root_path = Path(root_dir).resolve() if root_dir is not None else None |
| 31 | + |
| 32 | + while current_path != current_path.parent: # Stop at filesystem root |
| 33 | + target_dir = current_path / dot_dir |
| 34 | + try: |
| 35 | + if target_dir.is_dir(): |
| 36 | + return str(target_dir) |
| 37 | + except PermissionError: |
| 38 | + # Stop searching if we don't have permission to access this directory |
| 39 | + break |
| 40 | + |
| 41 | + # Stop if we've reached the specified root directory |
| 42 | + if root_path is not None and current_path == root_path: |
| 43 | + break |
| 44 | + |
| 45 | + current_path = current_path.parent |
| 46 | + |
| 47 | + return None |
| 48 | + |
| 49 | + |
| 50 | +def find_workspace_dir(dir: str, root_dir: Optional[str] = None) -> str: |
| 51 | + """ |
| 52 | + Find the workspace directory using the following algorithm: |
| 53 | +
|
| 54 | + 1. First finds the nearest .jupyter directory |
| 55 | + 2. If the .jupyter directory has a parent that is the root_dir, |
| 56 | + go to step 3, otherwise return the .jupyter directory |
| 57 | + 3. Try to find a .git directory, if found return that |
| 58 | + 4. Simply return the directory the chat file is in. |
| 59 | +
|
| 60 | + Args: |
| 61 | + dir (str): The starting directory path |
| 62 | + root_dir (Optional[str]): The root directory to stop searching at. If None, searches to filesystem root. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + str: The absolute path to the workspace directory |
| 66 | +
|
| 67 | + Raises: |
| 68 | + ValueError: If dir is not a directory |
| 69 | + """ |
| 70 | + # Validate that the starting path is a directory |
| 71 | + dir_path = Path(dir).resolve() |
| 72 | + if not dir_path.is_dir(): |
| 73 | + raise ValueError(f"'{dir}' is not a directory") |
| 74 | + |
| 75 | + # Step 1: Find nearest .jupyter directory |
| 76 | + jupyter_dir = find_dot_dir(dir, ".jupyter", root_dir) |
| 77 | + |
| 78 | + if jupyter_dir is not None: |
| 79 | + jupyter_path = Path(jupyter_dir) |
| 80 | + done = True |
| 81 | + # Step 2: Check if .jupyter directory's parent is the root_dir, if so, go to step 3 |
| 82 | + if root_dir is not None: |
| 83 | + root_path = Path(root_dir).resolve() |
| 84 | + if jupyter_path.parent == root_path: |
| 85 | + done = False |
| 86 | + if done: |
| 87 | + return str(jupyter_path.parent) |
| 88 | + |
| 89 | + # Step 3: Try to find .git directory (if no .jupyter found) |
| 90 | + git_dir = find_dot_dir(dir, ".git", root_dir) |
| 91 | + if git_dir is not None: |
| 92 | + return str(Path(git_dir).parent) |
| 93 | + |
| 94 | + # Step 4: Return chat file directory |
| 95 | + return str(dir_path) |
0 commit comments