Skip to content

Commit d77ed55

Browse files
authored
Merge pull request #47 from DavidCEllis/clear_cache_option
Add command to purge caches
2 parents 71343e3 + 0d4fcdd commit d77ed55

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/ducktools/pythonfinder/__main__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from ducktools.lazyimporter import LazyImporter, ModuleImport, FromImport
3030
from ducktools.pythonfinder import list_python_installs, __version__
31+
from ducktools.pythonfinder.shared import purge_caches
3132

3233
_laz = LazyImporter(
3334
[
@@ -122,6 +123,11 @@ def get_parser():
122123

123124
subparsers = parser.add_subparsers(dest="command", required=False)
124125

126+
clear_cache = subparsers.add_parser(
127+
"clear-cache",
128+
help="Clear the cache of Python install details"
129+
)
130+
125131
online = subparsers.add_parser(
126132
"online",
127133
help="Get links to binaries from python.org"
@@ -305,7 +311,9 @@ def main():
305311
parser = get_parser()
306312
vals = parser.parse_args(sys.argv[1:])
307313

308-
if vals.command == "online":
314+
if vals.command == "clear-cache":
315+
purge_caches()
316+
elif vals.command == "online":
309317
system = vals.system if vals.system else _laz.platform.system()
310318
machine = vals.machine if vals.machine else _laz.platform.machine()
311319
try:

src/ducktools/pythonfinder/shared.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
ModuleImport("json"),
4040
ModuleImport("platform"),
4141
ModuleImport("re"),
42+
ModuleImport("shutil"),
4243
ModuleImport("subprocess"),
4344
ModuleImport("tempfile"),
4445
ModuleImport("zipfile"),
@@ -84,6 +85,10 @@
8485
INSTALLER_CACHE_PATH = os.path.join(CACHE_FOLDER, "installer_details.json")
8586

8687

88+
def purge_caches(cache_folder=CACHE_FOLDER):
89+
_laz.shutil.rmtree(cache_folder, ignore_errors=True)
90+
91+
8792
def version_str_to_tuple(version):
8893
parsed_version = _laz.re.fullmatch(FULL_PY_VER_RE, version)
8994

0 commit comments

Comments
 (0)