mmngreco
4/3/2018 - 7:34 AM

[Pip clean] Python script to clean temp files created by pip. #tools #pip

[Pip clean] Python script to clean temp files created by pip. #tools #pip

import os, sys
from glob import glob
from pathlib import Path


def rmdir(pathlib_dir):
    walked = []
    if not pathlib_dir.exists():
        return False
    for item in pathlib_dir.iterdir():
        walked.append(item)
        if item.is_dir():
            rmdir(item)
            item.rmdir()
            print(item)
        else:
            item.unlink()
            print(item)
    return walked


print("Welcome to pip cache cleaner")
print("============================")

OK = False
PLATFORM = sys.platform

if PLATFORM == "linux":
    HOME = Path(os.environ["HOME"])
    print("Cleaning pip cache. Platform %s" % sys.platform)
    PATH_TO_RM = HOME / ".cache" / "pip"
    rmdir(PATH_TO_RM)
    OK = True
elif PLATFORM == "darwin":
    HOME = Path(os.environ["HOME"])
    print("Cleaning pip cache. Platform %s" % sys.platform)
    PATH_TO_RM = HOME / "Library" / "Caches" / "pip"
    rmdir(PATH_TO_RM)
    OK = True
elif sys.platform == "win32":
    print("Cleaning pip cache. Platform %s" % sys.platform)
    LOCAL_APP_DATA = Path(os.environ["LOCALAPPDATA"])
    rmdir(LOCAL_APP_DATA / "pip" / "cache")
    [rmdir(Path(path)) for path in glob(str(LOCAL_APP_DATA / "Temp" / "pip-*"))]
    OK = True

if OK:
    print("pip cache cleaned!")
else:
    print("pip cache not cleaned.")