|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +from pathlib import Path |
| 4 | +from typing import Callable, List |
| 5 | + |
| 6 | +from fsspec.implementations.local import LocalFileSystem |
| 7 | + |
| 8 | +from lightning.app.storage.copier import _copy_files |
| 9 | +from lightning.app.storage.path import _filesystem, _shared_storage_path |
| 10 | + |
| 11 | + |
| 12 | +def _get_files(fs, src: Path, dst: Path, overwrite: bool = True): |
| 13 | + dst = dst.resolve() |
| 14 | + if fs.isdir(src): |
| 15 | + if isinstance(fs, LocalFileSystem): |
| 16 | + dst = dst.resolve() |
| 17 | + if fs.exists(dst): |
| 18 | + if overwrite: |
| 19 | + fs.rm(str(dst), recursive=True) |
| 20 | + else: |
| 21 | + raise FileExistsError(f"The file {dst} was found. Add get(..., overwrite=True) to replace it.") |
| 22 | + |
| 23 | + shutil.copytree(src, dst) |
| 24 | + else: |
| 25 | + glob = f"{str(src)}/**" |
| 26 | + fs.get(glob, str(dst), recursive=False) |
| 27 | + else: |
| 28 | + fs.get(str(src), str(dst), recursive=False) |
| 29 | + |
| 30 | + |
| 31 | +class FileSystem: |
| 32 | + |
| 33 | + """This filesystem enables to easily move files from and to the shared storage.""" |
| 34 | + |
| 35 | + def __init__(self) -> None: |
| 36 | + self._fs = _filesystem() |
| 37 | + self._root = str(_shared_storage_path()) |
| 38 | + |
| 39 | + def put(self, src_path: str, dst_path: str, put_fn: Callable = _copy_files) -> None: |
| 40 | + """This method enables to put a file to the shared storage in a blocking fashion. |
| 41 | +
|
| 42 | + Arguments: |
| 43 | + src_path: The path to your files locally |
| 44 | + dst_path: The path to your files transfered in the shared storage. |
| 45 | + put_fn: The method to use to put files in the shared storage. |
| 46 | + """ |
| 47 | + if not os.path.exists(Path(src_path).resolve()): |
| 48 | + raise FileExistsError(f"The provided path {src_path} doesn't exist") |
| 49 | + |
| 50 | + if not dst_path.startswith("/"): |
| 51 | + raise Exception(f"The provided destination {dst_path} needs to start with `/`.") |
| 52 | + |
| 53 | + src = Path(src_path).resolve() |
| 54 | + dst = Path(os.path.join(self._root, dst_path[1:])).resolve() |
| 55 | + |
| 56 | + return put_fn(src, dst, fs=self._fs) |
| 57 | + |
| 58 | + def get(self, src_path: str, dst_path: str, overwrite: bool = True, get_fn: Callable = _get_files) -> None: |
| 59 | + """This method enables to get files from the shared storage in a blocking fashion. |
| 60 | +
|
| 61 | + Arguments: |
| 62 | + src_path: The path to your files in the shared storage |
| 63 | + dst_path: The path to your files transfered locally |
| 64 | + get_fn: The method to use to put files in the shared storage. |
| 65 | + """ |
| 66 | + if not src_path.startswith("/"): |
| 67 | + raise Exception(f"The provided destination {src_path} needs to start with `/`.") |
| 68 | + |
| 69 | + src = Path(os.path.join(self._root, src_path[1:])).resolve() |
| 70 | + dst = Path(dst_path).resolve() |
| 71 | + |
| 72 | + return get_fn(fs=self._fs, src=src, dst=dst, overwrite=overwrite) |
| 73 | + |
| 74 | + def listdir(self, path: str) -> List[str]: |
| 75 | + """This method enables to list files from the shared storage in a blocking fashion. |
| 76 | +
|
| 77 | + Arguments: |
| 78 | + path: The path to files to list. |
| 79 | + """ |
| 80 | + if not path.startswith("/"): |
| 81 | + raise Exception(f"The provided destination {path} needs to start with `/`.") |
| 82 | + |
| 83 | + shared_path = Path(os.path.join(self._root, path[1:])).resolve() |
| 84 | + |
| 85 | + if not self._fs.exists(shared_path): |
| 86 | + raise RuntimeError(f"The provided path {shared_path} doesn't exist.") |
| 87 | + |
| 88 | + # Invalidate cache before running ls in case new directories have been added |
| 89 | + # TODO: Re-evaluate this - may lead to performance issues |
| 90 | + self._fs.invalidate_cache() |
| 91 | + |
| 92 | + paths = self._fs.ls(shared_path) |
| 93 | + if not paths: |
| 94 | + return paths |
| 95 | + |
| 96 | + return sorted([path.replace(self._root + os.sep, "") for path in paths if not path.endswith("info.txt")]) |
| 97 | + |
| 98 | + def walk(self, path: str) -> List[str]: |
| 99 | + """This method enables to list files from the shared storage in a blocking fashion. |
| 100 | +
|
| 101 | + Arguments: |
| 102 | + path: The path to files to list. |
| 103 | + """ |
| 104 | + if not path.startswith("/"): |
| 105 | + raise Exception(f"The provided destination {path} needs to start with `/`.") |
| 106 | + |
| 107 | + shared_path = Path(os.path.join(self._root, path[1:])).resolve() |
| 108 | + |
| 109 | + if not self._fs.exists(shared_path): |
| 110 | + raise RuntimeError(f"The provided path {shared_path} doesn't exist.") |
| 111 | + |
| 112 | + # Invalidate cache before running ls in case new directories have been added |
| 113 | + # TODO: Re-evaluate this - may lead to performance issues |
| 114 | + self._fs.invalidate_cache() |
| 115 | + |
| 116 | + paths = self._fs.ls(shared_path) |
| 117 | + if not paths: |
| 118 | + return paths |
| 119 | + |
| 120 | + out = [] |
| 121 | + |
| 122 | + for shared_path in paths: |
| 123 | + path = str(shared_path).replace(self._root, "") |
| 124 | + if self._fs.isdir(shared_path): |
| 125 | + out.extend(self.walk(path)) |
| 126 | + else: |
| 127 | + if path.endswith("info.txt"): |
| 128 | + continue |
| 129 | + out.append(path[1:]) |
| 130 | + return sorted(out) |
| 131 | + |
| 132 | + def rm(self, path) -> None: |
| 133 | + if not path.startswith("/"): |
| 134 | + raise Exception(f"The provided destination {path} needs to start with `/`.") |
| 135 | + |
| 136 | + delete_path = Path(os.path.join(self._root, path[1:])).resolve() |
| 137 | + |
| 138 | + if self._fs.exists(str(delete_path)): |
| 139 | + if self._fs.isdir(str(delete_path)): |
| 140 | + self._fs.rmdir(str(delete_path)) |
| 141 | + else: |
| 142 | + self._fs.rm(str(delete_path)) |
| 143 | + else: |
| 144 | + raise Exception(f"The file path {path} doesn't exist.") |
| 145 | + |
| 146 | + def isfile(self, path: str) -> bool: |
| 147 | + if not path.startswith("/"): |
| 148 | + raise Exception(f"The provided destination {path} needs to start with `/`.") |
| 149 | + |
| 150 | + path = Path(os.path.join(self._root, path[1:])).resolve() |
| 151 | + return self._fs.isfile(path) |
| 152 | + |
| 153 | + def isdir(self, path: str) -> bool: |
| 154 | + if not path.startswith("/"): |
| 155 | + raise Exception(f"The provided destination {path} needs to start with `/`.") |
| 156 | + |
| 157 | + path = Path(os.path.join(self._root, path[1:])).resolve() |
| 158 | + return self._fs.isdir(path) |
0 commit comments