Script to run tests.
[python_utils.git] / file_utils.py
index eb8c2c0dbe336a165068b724c4162dc3667a89bc..d5451244bb71bf8ce7fc4e0d03c1162b01e0f3e8 100644 (file)
@@ -4,6 +4,7 @@
 
 import datetime
 import errno
+import hashlib
 import logging
 import os
 import time
@@ -11,9 +12,6 @@ from typing import Optional
 import glob
 from os.path import isfile, join, exists
 
-import datetime_utils
-
-
 logger = logging.getLogger(__name__)
 
 
@@ -48,7 +46,35 @@ def create_path_if_not_exist(path, on_error=None):
 
 
 def does_file_exist(filename: str) -> bool:
-    return os.path.exists(filename)
+    return os.path.exists(filename) and os.path.isfile(filename)
+
+
+def does_directory_exist(dirname: str) -> bool:
+    return os.path.exists(dirname) and os.path.isdir(dirname)
+
+
+def does_path_exist(pathname: str) -> bool:
+    return os.path.exists(pathname)
+
+
+def get_file_size(filename: str) -> int:
+    return os.path.getsize(filename)
+
+
+def is_normal_file(filename: str) -> bool:
+    return os.path.isfile(filename)
+
+
+def is_directory(filename: str) -> bool:
+    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 +104,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 +197,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]:
@@ -188,5 +242,5 @@ 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