diff options
author | 2025-02-13 14:13:49 +0530 | |
---|---|---|
committer | 2025-02-13 14:13:49 +0530 | |
commit | 8a2e1006b3b272126332aa064f3ad95387129544 (patch) | |
tree | 944c80ac612a65980d94a54ba11b6c7102037ecf /.local/bin/clear_comtypes_cache.py | |
parent | dcbb16d8b08ff5956abef5e6478b59df2e93ad35 (diff) | |
download | dotfiles-master.tar.gz dotfiles-master.tar.bz2 dotfiles-master.zip |
Diffstat (limited to '.local/bin/clear_comtypes_cache.py')
-rw-r--r-- | .local/bin/clear_comtypes_cache.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/.local/bin/clear_comtypes_cache.py b/.local/bin/clear_comtypes_cache.py new file mode 100644 index 0000000..bd743cd --- /dev/null +++ b/.local/bin/clear_comtypes_cache.py @@ -0,0 +1,57 @@ +import os +import sys +import shutil + +def get_next_cache_dir(): + work_dir = os.getcwd() + try: + # change working directory to avoid import from local folder + # during installation process + os.chdir(os.path.dirname(sys.executable)) + import comtypes.client + return comtypes.client._code_cache._find_gen_dir() + except ImportError: + return None + finally: + os.chdir(work_dir) + + +def _remove(directory): + shutil.rmtree(directory) + print('Removed directory "%s"' % directory) + + +def remove_directory(directory, silent): + if directory: + if silent: + _remove(directory) + else: + try: + confirm = raw_input('Remove comtypes cache directories? (y/n): ') + except NameError: + confirm = input('Remove comtypes cache directories? (y/n): ') + if confirm.lower() == 'y': + _remove(directory) + else: + print('Directory "%s" NOT removed' % directory) + return False + return True + + +if len(sys.argv) > 1 and "-y" in sys.argv[1:]: + silent = True +else: + silent = False + + +# First iteration may get folder with restricted rights. +# Second iteration always gets temp cache folder (writable for all). +directory = get_next_cache_dir() +removed = remove_directory(directory, silent) + +if removed: + directory = get_next_cache_dir() + + # do not request the second confirmation + # if the first folder was already removed + remove_directory(directory, silent=removed) |