Ugh, a bunch of things. @overrides. --lmodule. Chromecasts. etc...
[python_utils.git] / file_utils.py
index eb8c2c0dbe336a165068b724c4162dc3667a89bc..67e6f561f394e1a99c4ace7bb2f6ddbe733ea011 100644 (file)
@@ -4,19 +4,24 @@
 
 import datetime
 import errno
+import hashlib
 import logging
 import os
+import io
+import pathlib
 import time
 from typing import Optional
 import glob
 from os.path import isfile, join, exists
-
-import datetime_utils
+from uuid import uuid4
 
 
 logger = logging.getLogger(__name__)
 
 
+# os.remove(file) you fuckwit.
+
+
 def create_path_if_not_exist(path, on_error=None):
     """
     Attempts to create path if it does not exist. If on_error is
@@ -48,7 +53,57 @@ def create_path_if_not_exist(path, on_error=None):
 
 
 def does_file_exist(filename: str) -> bool:
-    return os.path.exists(filename)
+    """Returns True if a file exists and is a normal file.
+
+    >>> does_file_exist(__file__)
+    True
+    """
+    return os.path.exists(filename) and os.path.isfile(filename)
+
+
+def does_directory_exist(dirname: str) -> bool:
+    """Returns True if a file exists and is a directory.
+
+    >>> does_directory_exist('/tmp')
+    True
+    """
+    return os.path.exists(dirname) and os.path.isdir(dirname)
+
+
+def does_path_exist(pathname: str) -> bool:
+    """Just a more verbose wrapper around os.path.exists."""
+    return os.path.exists(pathname)
+
+
+def get_file_size(filename: str) -> int:
+    """Returns the size of a file in bytes."""
+    return os.path.getsize(filename)
+
+
+def is_normal_file(filename: str) -> bool:
+    """Returns True if filename is a normal file.
+
+    >>> is_normal_file(__file__)
+    True
+    """
+    return os.path.isfile(filename)
+
+
+def is_directory(filename: str) -> bool:
+    """Returns True if filename is a directory.
+
+    >>> is_directory('/tmp')
+    True
+    """
+    return os.path.isdir(filename)
+
+
+def is_symlink(filename: str) -> bool:
+    return os.path.islink(filename)
+
+
+def is_same_file(file1: str, file2: str) -> bool:
+    return os.path.samefile(file1, file2)
 
 
 def get_file_raw_timestamps(filename: str) -> Optional[os.stat_result]:
@@ -78,6 +133,33 @@ def get_file_raw_ctime(filename: str) -> Optional[float]:
     return get_file_raw_timestamp(filename, lambda x: x.st_ctime)
 
 
+def get_file_md5(filename: str) -> str:
+    file_hash = hashlib.md5()
+    with open(filename, "rb") as f:
+        chunk = f.read(8192)
+        while chunk:
+            file_hash.update(chunk)
+            chunk = f.read(8192)
+    return file_hash.hexdigest()
+
+
+def set_file_raw_atime(filename: str, atime: float):
+    mtime = get_file_raw_mtime(filename)
+    os.utime(filename, (atime, mtime))
+
+
+def set_file_raw_mtime(filename: str, mtime: float):
+    atime = get_file_raw_atime(filename)
+    os.utime(filename, (atime, mtime))
+
+
+def set_file_raw_atime_and_mtime(filename: str, ts: float = None):
+    if ts is not None:
+        os.utime(filename, (ts, ts))
+    else:
+        os.utime(filename, None)
+
+
 def convert_file_timestamp_to_datetime(
     filename: str, producer
 ) -> Optional[datetime.datetime]:
@@ -144,13 +226,14 @@ def get_file_mtime_timedelta(filename: str) -> Optional[datetime.timedelta]:
 def describe_file_timestamp(
     filename: str, extractor, *, brief=False
 ) -> Optional[str]:
+    from datetime_utils import describe_duration, describe_duration_briefly
     age = get_file_timestamp_age_seconds(filename, extractor)
     if age is None:
         return None
     if brief:
-        return datetime_utils.describe_duration_briefly(age)
+        return describe_duration_briefly(age)
     else:
-        return datetime_utils.describe_duration(age)
+        return describe_duration(age)
 
 
 def describe_file_atime(filename: str, *, brief=False) -> Optional[str]:
@@ -165,6 +248,10 @@ def describe_file_mtime(filename: str, *, brief=False) -> Optional[str]:
     return describe_file_timestamp(filename, lambda x: x.st_mtime, brief=brief)
 
 
+def touch_file(filename: str) -> bool:
+    return pathlib.Path(filename).touch()
+
+
 def expand_globs(in_filename: str):
     for filename in glob.glob(in_filename):
         yield filename
@@ -188,5 +275,32 @@ def get_files_recursive(directory: str):
     for filename in get_files(directory):
         yield filename
     for subdir in get_directories(directory):
-        for filename in get_files_recursive(subdir):
-            yield filename
+        for file_or_directory in get_files_recursive(subdir):
+            yield file_or_directory
+
+
+class FileWriter(object):
+    def __init__(self, filename: str) -> None:
+        self.filename = filename
+        uuid = uuid4()
+        self.tempfile = f'{filename}-{uuid}.tmp'
+        self.handle = None
+
+    def __enter__(self) -> io.TextIOWrapper:
+        assert not does_path_exist(self.tempfile)
+        self.handle = open(self.tempfile, mode="w")
+        return self.handle
+
+    def __exit__(self, exc_type, exc_val, exc_tb) -> bool:
+        if self.handle is not None:
+            self.handle.close()
+            cmd = f'/bin/mv -f {self.tempfile} {self.filename}'
+            ret = os.system(cmd)
+            if (ret >> 8) != 0:
+                raise Exception(f'{cmd} failed, exit value {ret>>8}')
+        return None
+
+
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()