Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruyi self clean polishes #219

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ruyi/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ def init_argparse() -> argparse.ArgumentParser:
action="store_true",
help="Do not print out the actions being performed",
)
self_clean.add_argument(
"--all",
action="store_true",
help="Remove all covered data",
)
self_clean.add_argument(
"--distfiles",
action="store_true",
Expand All @@ -327,6 +332,11 @@ def init_argparse() -> argparse.ArgumentParser:
action="store_true",
help="Remove all installed packages if any",
)
self_clean.add_argument(
"--news-read-status",
action="store_true",
help="Mark all news items as unread",
)
self_clean.add_argument(
"--progcache",
action="store_true",
Expand Down
26 changes: 21 additions & 5 deletions ruyi/cli/self_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@

def cli_self_clean(cfg: config.GlobalConfig, args: argparse.Namespace) -> int:
quiet: bool = args.quiet
all: bool = args.all
distfiles: bool = args.distfiles
installed_pkgs: bool = args.installed_pkgs
news_read_status: bool = args.news_read_status
progcache: bool = args.progcache
repo: bool = args.repo
telemetry: bool = args.telemetry

if not any([distfiles, installed_pkgs, progcache, repo, telemetry]):
if all:
distfiles = True
installed_pkgs = True
news_read_status = True
progcache = True
repo = True
telemetry = True

if not any([distfiles, installed_pkgs, news_read_status, progcache, repo, telemetry]):
log.F("no data specified for cleaning")
log.I(
"please check [yellow]ruyi self clean --help[/] for a list of cleanable data"
Expand All @@ -41,6 +51,7 @@ def cli_self_clean(cfg: config.GlobalConfig, args: argparse.Namespace) -> int:
quiet=quiet,
distfiles=distfiles,
installed_pkgs=installed_pkgs,
news_read_status=news_read_status,
progcache=progcache,
repo=repo,
telemetry=telemetry,
Expand Down Expand Up @@ -75,10 +86,6 @@ def cli_self_uninstall(cfg: config.GlobalConfig, args: argparse.Namespace) -> in
else:
log.I("uninstallation consent given over CLI, proceeding")

if tm := cfg.telemetry:
# do not record any telemetry data if we're purging all data with us
tm.discard_events(purge)

_do_reset(
cfg,
quiet=False,
Expand All @@ -99,6 +106,7 @@ def _do_reset(
*,
installed_pkgs: bool = False,
all_state: bool = False,
news_read_status: bool = False, # ignored if all_state=True
telemetry: bool = False, # ignored if all_state=True
all_cache: bool = False,
distfiles: bool = False, # ignored if all_cache=True
Expand All @@ -119,7 +127,15 @@ def status(s: str) -> None:
status("removing state data")
shutil.rmtree(cfg.state_root, True)
else:
if news_read_status:
status("removing read status of news items")
cfg.news_read_status.remove()

if telemetry:
if tm := cfg.telemetry:
# do not record any telemetry data if we're purging it
tm.discard_events(True)

status("removing all telemetry data")
shutil.rmtree(cfg.telemetry_root, True)

Expand Down
6 changes: 6 additions & 0 deletions ruyi/config/news.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os


class NewsReadStatusStore:
def __init__(self, path: str) -> None:
self._path = path
Expand Down Expand Up @@ -27,3 +30,6 @@ def save(self) -> None:
content = "".join(f"{id}\n" for id in self._status)
with open(self._path, "w", encoding="utf-8") as fp:
fp.write(content)

def remove(self) -> None:
os.unlink(self._path)