From 335a58c0ca2f3cad5f479f7e9a384ef1fe30ccc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katar=C3=ADna=20Kubiniov=C3=A1?= Date: Thu, 16 Nov 2023 00:31:55 +0100 Subject: [PATCH 1/2] ISSUE-1480 Deleting temporary _MEI files --- apps/stable_diffusion/web/index.py | 28 +++++++++++++++++++++++++++ shark/tests/test_index.py | 31 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 shark/tests/test_index.py diff --git a/apps/stable_diffusion/web/index.py b/apps/stable_diffusion/web/index.py index 32d40e3dad..a2a07111f3 100644 --- a/apps/stable_diffusion/web/index.py +++ b/apps/stable_diffusion/web/index.py @@ -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": @@ -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) @@ -430,3 +457,4 @@ def register_outputgallery_button(button, selectedid, inputs, outputs): server_port=actual_port, favicon_path=nodicon_loc, ) + cleanup_mei_folders() diff --git a/shark/tests/test_index.py b/shark/tests/test_index.py new file mode 100644 index 0000000000..3bb98a7abd --- /dev/null +++ b/shark/tests/test_index.py @@ -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) \ No newline at end of file From 63da8720913cf7bec710cdd0a50c57039dd50de4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katar=C3=ADna=20Kubiniov=C3=A1?= Date: Tue, 5 Dec 2023 20:38:24 +0100 Subject: [PATCH 2/2] ISSUE-1480 reformating code --- apps/stable_diffusion/web/index.py | 12 +++++++----- shark/tests/test_index.py | 15 +++++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/apps/stable_diffusion/web/index.py b/apps/stable_diffusion/web/index.py index f22bdd2a3f..07f8d3ac68 100644 --- a/apps/stable_diffusion/web/index.py +++ b/apps/stable_diffusion/web/index.py @@ -25,10 +25,9 @@ # 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') + 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. @@ -36,10 +35,13 @@ def cleanup_mei_folders(): # temp_dir = '/tmp' else: - warnings.warn("Temporary files weren't deleted due to an unsupported OS; program functionality is unaffected.") + warnings.warn( + "Temporary files weren't deleted due to an unsupported OS;" + + " program functionality is unaffected." + ) return - prefix = '_MEI' + prefix = "_MEI" # Iterate through the items in the temporary directory for item in os.listdir(temp_dir): diff --git a/shark/tests/test_index.py b/shark/tests/test_index.py index 3bb98a7abd..50e87fa359 100644 --- a/shark/tests/test_index.py +++ b/shark/tests/test_index.py @@ -8,14 +8,14 @@ # 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 mock.patch("sys.platform", "win32"): with tempfile.TemporaryDirectory() as temp_dir: - temp_temp_dir = os.path.join(temp_dir, 'Temp') + 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') + 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() @@ -24,8 +24,11 @@ def test_cleanup_mei_folders_windows(): # Test for removing temporary folders at unsupported OS def test_cleanup_mei_folders_unsupported_os(): - with mock.patch('sys.platform', '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) \ No newline at end of file + assert ( + "Temporary files weren't deleted due to an unsupported OS" + in str(record.list[0].message) + )