util.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import os
  2. import re
  3. from modules import shared
  4. from modules.paths_internal import script_path, cwd
  5. def natural_sort_key(s, regex=re.compile('([0-9]+)')):
  6. return [int(text) if text.isdigit() else text.lower() for text in regex.split(s)]
  7. def listfiles(dirname):
  8. filenames = [os.path.join(dirname, x) for x in sorted(os.listdir(dirname), key=natural_sort_key) if not x.startswith(".")]
  9. return [file for file in filenames if os.path.isfile(file)]
  10. def html_path(filename):
  11. return os.path.join(script_path, "html", filename)
  12. def html(filename):
  13. path = html_path(filename)
  14. try:
  15. with open(path, encoding="utf8") as file:
  16. return file.read()
  17. except OSError:
  18. return ""
  19. def walk_files(path, allowed_extensions=None):
  20. if not os.path.exists(path):
  21. return
  22. if allowed_extensions is not None:
  23. allowed_extensions = set(allowed_extensions)
  24. items = list(os.walk(path, followlinks=True))
  25. items = sorted(items, key=lambda x: natural_sort_key(x[0]))
  26. for root, _, files in items:
  27. for filename in sorted(files, key=natural_sort_key):
  28. if allowed_extensions is not None:
  29. _, ext = os.path.splitext(filename)
  30. if ext.lower() not in allowed_extensions:
  31. continue
  32. if not shared.opts.list_hidden_files and ("/." in root or "\\." in root):
  33. continue
  34. yield os.path.join(root, filename)
  35. def ldm_print(*args, **kwargs):
  36. if shared.opts.hide_ldm_prints:
  37. return
  38. print(*args, **kwargs)
  39. def truncate_path(target_path, base_path=cwd):
  40. abs_target, abs_base = os.path.abspath(target_path), os.path.abspath(base_path)
  41. try:
  42. if os.path.commonpath([abs_target, abs_base]) == abs_base:
  43. return os.path.relpath(abs_target, abs_base)
  44. except ValueError:
  45. pass
  46. return abs_target
  47. class MassFileListerCachedDir:
  48. """A class that caches file metadata for a specific directory."""
  49. def __init__(self, dirname):
  50. self.files = None
  51. self.files_cased = None
  52. self.dirname = dirname
  53. stats = ((x.name, x.stat(follow_symlinks=False)) for x in os.scandir(self.dirname))
  54. files = [(n, s.st_mtime, s.st_ctime) for n, s in stats]
  55. self.files = {x[0].lower(): x for x in files}
  56. self.files_cased = {x[0]: x for x in files}
  57. def update_entry(self, filename):
  58. """Add a file to the cache"""
  59. file_path = os.path.join(self.dirname, filename)
  60. try:
  61. stat = os.stat(file_path)
  62. entry = (filename, stat.st_mtime, stat.st_ctime)
  63. self.files[filename.lower()] = entry
  64. self.files_cased[filename] = entry
  65. except FileNotFoundError as e:
  66. print(f'MassFileListerCachedDir.add_entry: "{file_path}" {e}')
  67. class MassFileLister:
  68. """A class that provides a way to check for the existence and mtime/ctile of files without doing more than one stat call per file."""
  69. def __init__(self):
  70. self.cached_dirs = {}
  71. def find(self, path):
  72. """
  73. Find the metadata for a file at the given path.
  74. Returns:
  75. tuple or None: A tuple of (name, mtime, ctime) if the file exists, or None if it does not.
  76. """
  77. dirname, filename = os.path.split(path)
  78. cached_dir = self.cached_dirs.get(dirname)
  79. if cached_dir is None:
  80. cached_dir = MassFileListerCachedDir(dirname)
  81. self.cached_dirs[dirname] = cached_dir
  82. stats = cached_dir.files_cased.get(filename)
  83. if stats is not None:
  84. return stats
  85. stats = cached_dir.files.get(filename.lower())
  86. if stats is None:
  87. return None
  88. try:
  89. os_stats = os.stat(path, follow_symlinks=False)
  90. return filename, os_stats.st_mtime, os_stats.st_ctime
  91. except Exception:
  92. return None
  93. def exists(self, path):
  94. """Check if a file exists at the given path."""
  95. return self.find(path) is not None
  96. def mctime(self, path):
  97. """
  98. Get the modification and creation times for a file at the given path.
  99. Returns:
  100. tuple: A tuple of (mtime, ctime) if the file exists, or (0, 0) if it does not.
  101. """
  102. stats = self.find(path)
  103. return (0, 0) if stats is None else stats[1:3]
  104. def reset(self):
  105. """Clear the cache of all directories."""
  106. self.cached_dirs.clear()
  107. def update_file_entry(self, path):
  108. """Update the cache for a specific directory."""
  109. dirname, filename = os.path.split(path)
  110. if cached_dir := self.cached_dirs.get(dirname):
  111. cached_dir.update_entry(filename)
  112. def topological_sort(dependencies):
  113. """Accepts a dictionary mapping name to its dependencies, returns a list of names ordered according to dependencies.
  114. Ignores errors relating to missing dependencies or circular dependencies
  115. """
  116. visited = {}
  117. result = []
  118. def inner(name):
  119. visited[name] = True
  120. for dep in dependencies.get(name, []):
  121. if dep in dependencies and dep not in visited:
  122. inner(dep)
  123. result.append(name)
  124. for depname in dependencies:
  125. if depname not in visited:
  126. inner(depname)
  127. return result
  128. def open_folder(path):
  129. """Open a folder in the file manager of the respect OS."""
  130. # import at function level to avoid potential issues
  131. import gradio as gr
  132. import platform
  133. import sys
  134. import subprocess
  135. if not os.path.exists(path):
  136. msg = f'Folder "{path}" does not exist. after you save an image, the folder will be created.'
  137. print(msg)
  138. gr.Info(msg)
  139. return
  140. elif not os.path.isdir(path):
  141. msg = f"""
  142. WARNING
  143. An open_folder request was made with an path that is not a folder.
  144. This could be an error or a malicious attempt to run code on your computer.
  145. Requested path was: {path}
  146. """
  147. print(msg, file=sys.stderr)
  148. gr.Warning(msg)
  149. return
  150. path = os.path.normpath(path)
  151. if platform.system() == "Windows":
  152. os.startfile(path)
  153. elif platform.system() == "Darwin":
  154. subprocess.Popen(["open", path])
  155. elif "microsoft-standard-WSL2" in platform.uname().release:
  156. subprocess.Popen(["explorer.exe", subprocess.check_output(["wslpath", "-w", path])])
  157. else:
  158. subprocess.Popen(["xdg-open", path])