Created
November 30, 2021 16:43
-
-
Save andreyfedoseev/2dc809edde66022dd9e33bd408fd16b4 to your computer and use it in GitHub Desktop.
Move caches from `~/.config` to `~/.cache` and replace them with symlinks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
from pathlib import Path | |
import shutil | |
import os | |
PATHS = ( | |
"balena-etcher-electron/Cache", | |
"Code/Cache", | |
"Code/CachedData", | |
"Code/Service Worker/CacheStorage", | |
"discord/Cache", | |
"google-chrome-beta/Default/Service Worker/CacheStorage", | |
"google-chrome-unstable/Default/Service Worker/CacheStorage", | |
"google-chrome/Default/Service Worker/CacheStorage", | |
"MongoDB Compass/Cache", | |
"Slack/Cache", | |
"Slack/Service Worker/CacheStorage", | |
) | |
HOME_DIR = Path.home() | |
CONFIG_DIR = HOME_DIR / ".config" | |
CACHE_DIR = HOME_DIR / ".cache" | |
def main(): | |
for path in PATHS: | |
process_path(path) | |
def process_path(path: str): | |
source_path = CONFIG_DIR / path | |
if not source_path.exists(): | |
print(f"{source_path} does not exist, skipping.") | |
return | |
if source_path.is_symlink(): | |
print(f"{source_path} is a symlink, skipping.") | |
return | |
if not source_path.is_dir(): | |
print(f"{source_path} is not a directory, skipping.") | |
return | |
target_path = CACHE_DIR / path | |
if input( | |
f"Do you want to move {source_path} to {target_path} and replace it with a symlink? Type `y` or `yes` to confirm: " | |
) not in ("y", "yes"): | |
return | |
if target_path.exists(): | |
if input( | |
f"{target_path} exists already. Do you want to remove it? Type `y` or `yes` to confirm: " | |
) not in ("y", "yes"): | |
return | |
print(f"Removing {target_path} ...", end="") | |
shutil.rmtree(target_path) | |
print("Done") | |
print(f"Moving {source_path} to {target_path} ...", end="") | |
shutil.move(source_path, target_path) | |
print("Done") | |
os.symlink(target_path, source_path) | |
print(f"{source_path} is now a symlink to {target_path}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment