1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
import os
import shutil
import stat
from abc import abstractmethod
from typing import Iterable, List
from trashcli.compat import Protocol
class FileSize(Protocol):
@abstractmethod
def file_size(self, path):
raise NotImplementedError()
class MakeFileExecutable(Protocol):
@abstractmethod
def make_file_executable(self, path):
raise NotImplementedError()
class WriteFile(Protocol):
@abstractmethod
def write_file(self, name, contents):
raise NotImplementedError()
class ReadFile(Protocol):
@abstractmethod
def read_file(self, path):
raise NotImplementedError()
class Move(Protocol):
@abstractmethod
def move(self, path, dest):
raise NotImplementedError()
class RemoveFile2(Protocol):
@abstractmethod
def remove_file2(self, path):
raise NotImplementedError()
class RemoveFile(Protocol):
@abstractmethod
def remove_file(self, path):
raise NotImplementedError()
class RemoveFileIfExists(Protocol):
@abstractmethod
def remove_file_if_exists(self, path):
raise NotImplementedError()
class EntriesIfDirExists(Protocol):
@abstractmethod
def entries_if_dir_exists(self, path): # type: (str) -> List[str]
raise NotImplementedError()
class PathExists(Protocol):
@abstractmethod
def exists(self, path):
raise NotImplementedError()
class HasStickyBit(Protocol):
@abstractmethod
def has_sticky_bit(self, path): # type: (str) -> bool
raise NotImplementedError
class IsStickyDir(Protocol):
@abstractmethod
def is_sticky_dir(self, path): # type: (str) -> bool
raise NotImplementedError
class IsSymLink(Protocol):
@abstractmethod
def is_symlink(self, path): # type: (str) -> bool
raise NotImplementedError
class ContentsOf(Protocol):
@abstractmethod
def contents_of(self, path):
raise NotImplementedError()
class RealEntriesIfDirExists(EntriesIfDirExists):
def entries_if_dir_exists(self, path):
if os.path.exists(path):
for entry in os.listdir(path):
yield entry
class RealExists(PathExists):
def exists(self, path): # type: (str) -> bool
return os.path.exists(path)
class RealHasStickyBit(HasStickyBit):
def has_sticky_bit(self, path):
return (os.stat(path).st_mode & stat.S_ISVTX) == stat.S_ISVTX
class RealIsStickyDir(IsStickyDir, RealHasStickyBit):
def is_sticky_dir(self, path): # type: (str) -> bool
return os.path.isdir(path) and self.has_sticky_bit(path)
class RealIsSymLink(IsSymLink):
def is_symlink(self, path): # type: (str) -> bool
return os.path.islink(path)
class RealContentsOf(ContentsOf):
def contents_of(self, path):
return _read_file(path)
class RealRemoveFile(RemoveFile):
def remove_file(self, path):
if os.path.lexists(path):
try:
os.remove(path)
except:
return shutil.rmtree(path)
class RealRemoveFile2(RemoveFile2):
def remove_file2(self, path):
try:
os.remove(path)
except OSError:
shutil.rmtree(path)
class RealRemoveFileIfExists(RemoveFileIfExists, RemoveFile2):
def remove_file_if_exists(self, path):
if os.path.lexists(path): self.remove_file2(path)
class RealMove(Move):
def move(self, path, dest):
return shutil.move(path, str(dest))
class ListFilesInDir(Protocol):
@abstractmethod
def list_files_in_dir(self, path): # type: (str) -> Iterable[str]
raise NotImplementedError()
class RealListFilesInDir(ListFilesInDir):
def list_files_in_dir(self, path): # type: (str) -> Iterable[str]
for entry in os.listdir(path):
result = os.path.join(path, entry)
yield result
class MkDirs(Protocol):
@abstractmethod
def mkdirs(self, path):
raise NotImplementedError()
class RealMkDirs(MkDirs):
def mkdirs(self, path):
if os.path.isdir(path):
return
os.makedirs(path)
class AtomicWrite(Protocol):
@abstractmethod
def atomic_write(self, path, content):
raise NotImplementedError()
@abstractmethod
def open_for_write_in_exclusive_and_create_mode(self, path):
raise NotImplementedError()
class RealAtomicWrite(AtomicWrite):
def atomic_write(self, path, content):
file_handle = self.open_for_write_in_exclusive_and_create_mode(path)
os.write(file_handle, content)
os.close(file_handle)
def open_for_write_in_exclusive_and_create_mode(self, path):
return os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
class RealReadFile(ReadFile):
def read_file(self, path):
return _read_file(path)
class RealWriteFile(WriteFile):
def write_file(self, name, contents):
with open(name, 'w') as f:
f.write(contents)
class RealMakeFileExecutable(MakeFileExecutable):
def make_file_executable(self, path):
os.chmod(path, os.stat(path).st_mode | stat.S_IXUSR)
class RealFileSize(FileSize):
def file_size(self, path):
return os.stat(path).st_size
class FsMethods(
RealEntriesIfDirExists,
RealExists,
RealIsStickyDir,
RealIsSymLink,
RealContentsOf,
RealRemoveFile,
RealRemoveFile2,
RealRemoveFileIfExists,
RealMove,
RealListFilesInDir,
RealMkDirs,
RealAtomicWrite,
RealReadFile,
RealWriteFile,
RealMakeFileExecutable,
RealFileSize,
):
pass
def _read_file(path):
with open(path) as f:
return f.read()
has_sticky_bit = RealHasStickyBit().has_sticky_bit
contents_of = RealContentsOf().contents_of
remove_file = RealRemoveFile().remove_file
move = RealMove().move
list_files_in_dir = RealListFilesInDir().list_files_in_dir
mkdirs = RealMkDirs().mkdirs
atomic_write = RealAtomicWrite().atomic_write
open_for_write_in_exclusive_and_create_mode = RealAtomicWrite().open_for_write_in_exclusive_and_create_mode
read_file = RealReadFile().read_file
write_file = RealWriteFile().write_file
make_file_executable = RealMakeFileExecutable().make_file_executable
file_size = RealFileSize().file_size
remove_file2 = RealRemoveFile2().remove_file2
is_sticky_dir = RealIsStickyDir().is_sticky_dir
|