3 """Utilities for working with files."""
13 from typing import Optional
15 from os.path import isfile, join, exists
16 from uuid import uuid4
19 logger = logging.getLogger(__name__)
22 # os.remove(file) you fuckwit.
23 # os.path.basename too.
26 def create_path_if_not_exist(path, on_error=None):
28 Attempts to create path if it does not exist. If on_error is
29 specified, it is called with an exception if one occurs, otherwise
30 exception is rethrown.
34 >>> path = os.path.join("/tmp", str(uuid.uuid4()), str(uuid.uuid4()))
35 >>> os.path.exists(path)
37 >>> create_path_if_not_exist(path)
38 >>> os.path.exists(path)
41 logger.debug(f"Creating path {path}")
42 previous_umask = os.umask(0)
47 if ex.errno != errno.EEXIST and not os.path.isdir(path):
48 if on_error is not None:
53 os.umask(previous_umask)
56 def does_file_exist(filename: str) -> bool:
57 """Returns True if a file exists and is a normal file.
59 >>> does_file_exist(__file__)
62 return os.path.exists(filename) and os.path.isfile(filename)
65 def does_directory_exist(dirname: str) -> bool:
66 """Returns True if a file exists and is a directory.
68 >>> does_directory_exist('/tmp')
71 return os.path.exists(dirname) and os.path.isdir(dirname)
74 def does_path_exist(pathname: str) -> bool:
75 """Just a more verbose wrapper around os.path.exists."""
76 return os.path.exists(pathname)
79 def get_file_size(filename: str) -> int:
80 """Returns the size of a file in bytes."""
81 return os.path.getsize(filename)
84 def is_normal_file(filename: str) -> bool:
85 """Returns True if filename is a normal file.
87 >>> is_normal_file(__file__)
90 return os.path.isfile(filename)
93 def is_directory(filename: str) -> bool:
94 """Returns True if filename is a directory.
96 >>> is_directory('/tmp')
99 return os.path.isdir(filename)
102 def is_symlink(filename: str) -> bool:
103 return os.path.islink(filename)
106 def is_same_file(file1: str, file2: str) -> bool:
107 return os.path.samefile(file1, file2)
110 def get_file_raw_timestamps(filename: str) -> Optional[os.stat_result]:
112 return os.stat(filename)
113 except Exception as e:
118 def get_file_raw_timestamp(filename: str, extractor) -> Optional[float]:
119 tss = get_file_raw_timestamps(filename)
121 return extractor(tss)
125 def get_file_raw_atime(filename: str) -> Optional[float]:
126 return get_file_raw_timestamp(filename, lambda x: x.st_atime)
129 def get_file_raw_mtime(filename: str) -> Optional[float]:
130 return get_file_raw_timestamp(filename, lambda x: x.st_mtime)
133 def get_file_raw_ctime(filename: str) -> Optional[float]:
134 return get_file_raw_timestamp(filename, lambda x: x.st_ctime)
137 def get_file_md5(filename: str) -> str:
138 file_hash = hashlib.md5()
139 with open(filename, "rb") as f:
142 file_hash.update(chunk)
144 return file_hash.hexdigest()
147 def set_file_raw_atime(filename: str, atime: float):
148 mtime = get_file_raw_mtime(filename)
149 os.utime(filename, (atime, mtime))
152 def set_file_raw_mtime(filename: str, mtime: float):
153 atime = get_file_raw_atime(filename)
154 os.utime(filename, (atime, mtime))
157 def set_file_raw_atime_and_mtime(filename: str, ts: float = None):
159 os.utime(filename, (ts, ts))
161 os.utime(filename, None)
164 def convert_file_timestamp_to_datetime(
165 filename: str, producer
166 ) -> Optional[datetime.datetime]:
167 ts = producer(filename)
169 return datetime.datetime.fromtimestamp(ts)
173 def get_file_atime_as_datetime(filename: str) -> Optional[datetime.datetime]:
174 return convert_file_timestamp_to_datetime(filename, get_file_raw_atime)
177 def get_file_mtime_as_datetime(filename: str) -> Optional[datetime.datetime]:
178 return convert_file_timestamp_to_datetime(filename, get_file_raw_mtime)
181 def get_file_ctime_as_datetime(filename: str) -> Optional[datetime.datetime]:
182 return convert_file_timestamp_to_datetime(filename, get_file_raw_ctime)
185 def get_file_timestamp_age_seconds(filename: str, extractor) -> Optional[int]:
187 ts = get_file_raw_timestamps(filename)
190 result = extractor(ts)
194 def get_file_atime_age_seconds(filename: str) -> Optional[int]:
195 return get_file_timestamp_age_seconds(filename, lambda x: x.st_atime)
198 def get_file_ctime_age_seconds(filename: str) -> Optional[int]:
199 return get_file_timestamp_age_seconds(filename, lambda x: x.st_ctime)
202 def get_file_mtime_age_seconds(filename: str) -> Optional[int]:
203 return get_file_timestamp_age_seconds(filename, lambda x: x.st_mtime)
206 def get_file_timestamp_timedelta(
207 filename: str, extractor
208 ) -> Optional[datetime.timedelta]:
209 age = get_file_timestamp_age_seconds(filename, extractor)
211 return datetime.timedelta(seconds=float(age))
215 def get_file_atime_timedelta(filename: str) -> Optional[datetime.timedelta]:
216 return get_file_timestamp_timedelta(filename, lambda x: x.st_atime)
219 def get_file_ctime_timedelta(filename: str) -> Optional[datetime.timedelta]:
220 return get_file_timestamp_timedelta(filename, lambda x: x.st_ctime)
223 def get_file_mtime_timedelta(filename: str) -> Optional[datetime.timedelta]:
224 return get_file_timestamp_timedelta(filename, lambda x: x.st_mtime)
227 def describe_file_timestamp(
228 filename: str, extractor, *, brief=False
230 from datetime_utils import describe_duration, describe_duration_briefly
231 age = get_file_timestamp_age_seconds(filename, extractor)
235 return describe_duration_briefly(age)
237 return describe_duration(age)
240 def describe_file_atime(filename: str, *, brief=False) -> Optional[str]:
241 return describe_file_timestamp(filename, lambda x: x.st_atime, brief=brief)
244 def describe_file_ctime(filename: str, *, brief=False) -> Optional[str]:
245 return describe_file_timestamp(filename, lambda x: x.st_ctime, brief=brief)
248 def describe_file_mtime(filename: str, *, brief=False) -> Optional[str]:
249 return describe_file_timestamp(filename, lambda x: x.st_mtime, brief=brief)
252 def touch_file(filename: str) -> bool:
253 return pathlib.Path(filename).touch()
256 def expand_globs(in_filename: str):
257 for filename in glob.glob(in_filename):
261 def get_files(directory: str):
262 for filename in os.listdir(directory):
263 full_path = join(directory, filename)
264 if isfile(full_path) and exists(full_path):
268 def get_directories(directory: str):
269 for d in os.listdir(directory):
270 full_path = join(directory, d)
271 if not isfile(full_path) and exists(full_path):
275 def get_files_recursive(directory: str):
276 for filename in get_files(directory):
278 for subdir in get_directories(directory):
279 for file_or_directory in get_files_recursive(subdir):
280 yield file_or_directory
283 class FileWriter(object):
284 def __init__(self, filename: str) -> None:
285 self.filename = filename
287 self.tempfile = f'{filename}-{uuid}.tmp'
290 def __enter__(self) -> io.TextIOWrapper:
291 assert not does_path_exist(self.tempfile)
292 self.handle = open(self.tempfile, mode="w")
295 def __exit__(self, exc_type, exc_val, exc_tb) -> bool:
296 if self.handle is not None:
298 cmd = f'/bin/mv -f {self.tempfile} {self.filename}'
301 raise Exception(f'{cmd} failed, exit value {ret>>8}')
305 if __name__ == '__main__':