Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-1480 Deleting temporary _MEI files #1978

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions apps/stable_diffusion/web/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import sys
import logging
import warnings
import apps.stable_diffusion.web.utils.app as app

if sys.platform == "darwin":
Expand All @@ -22,6 +23,32 @@
clear_all()


# This function is intended to clean up MEI folders
def cleanup_mei_folders():

# Determine the operating system
if sys.platform.startswith('win'):
temp_dir = os.path.join(os.environ['LOCALAPPDATA'], 'Temp')

# For potential extension to support Linux or macOS systems:
# NOTE: Before enabling, ensure compatibility and testing.
# elif sys.platform.startswith('linux') or sys.platform == 'darwin':
# temp_dir = '/tmp'

else:
warnings.warn("Temporary files weren't deleted due to an unsupported OS; program functionality is unaffected.")
return

prefix = '_MEI'

# Iterate through the items in the temporary directory
for item in os.listdir(temp_dir):
if item.startswith(prefix):
path = os.path.join(temp_dir, item)
if os.path.isdir(path):
shutil.rmtree(path, ignore_errors=True)


if __name__ == "__main__":
if args.debug:
logging.basicConfig(level=logging.DEBUG)
Expand Down Expand Up @@ -444,3 +471,4 @@ def register_outputgallery_button(button, selectedid, inputs, outputs):
server_port=actual_port,
favicon_path=nodicon_loc,
)
cleanup_mei_folders()
31 changes: 31 additions & 0 deletions shark/tests/test_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import pytest
import tempfile
from unittest import mock
from apps.stable_diffusion.web.index import cleanup_mei_folders


# Test for removing temporary _MEI folders on windows
def test_cleanup_mei_folders_windows():
# Setting up the test environment for Windows
with mock.patch('sys.platform', 'win32'):
with tempfile.TemporaryDirectory() as temp_dir:
temp_temp_dir = os.path.join(temp_dir, 'Temp')
os.makedirs(temp_temp_dir)

# Creating a fictitious _MEI directory
with mock.patch.dict('os.environ', {'LOCALAPPDATA': temp_dir}):
mei_folder = os.path.join(temp_temp_dir, '_MEI12345')
os.makedirs(mei_folder)

cleanup_mei_folders()
assert not os.path.exists(mei_folder)


# Test for removing temporary folders at unsupported OS
def test_cleanup_mei_folders_unsupported_os():
with mock.patch('sys.platform', 'unsupported_os'):
with pytest.warns(UserWarning) as record:
cleanup_mei_folders()

assert "Temporary files weren't deleted due to an unsupported OS" in str(record.list[0].message)
Loading