2022-11-24 02:08:58 +00:00
|
|
|
|
import re
|
|
|
|
|
import subprocess
|
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
from selfprivacy_api.jobs import JobStatus, Jobs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
COMPLETED_WITH_ERROR = "Completed with an error"
|
|
|
|
|
RESULT_WAAS_NOT_FOUND_ERROR = "We are sorry, result was not found :("
|
|
|
|
|
CLEAR_COMPLETED = "Сleaning completed."
|
2022-11-24 02:08:58 +00:00
|
|
|
|
|
|
|
|
|
|
2022-12-13 01:44:52 +00:00
|
|
|
|
def run_nix_store_print_dead():
|
2022-12-19 02:27:44 +00:00
|
|
|
|
return subprocess.check_output(["nix-store", "--gc", "--print-dead"]).decode(
|
|
|
|
|
"utf-8"
|
|
|
|
|
)
|
2022-11-24 02:08:58 +00:00
|
|
|
|
|
|
|
|
|
|
2022-12-13 01:44:52 +00:00
|
|
|
|
def run_nix_collect_garbage():
|
|
|
|
|
return subprocess.Popen(
|
|
|
|
|
["nix-collect-garbage", "-d"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
|
2022-12-19 02:27:44 +00:00
|
|
|
|
).stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_job_status_wrapper(Jobs, job):
|
|
|
|
|
def set_job_status(status, progress, status_text, result="Default result"):
|
|
|
|
|
Jobs.update(
|
|
|
|
|
job=job,
|
|
|
|
|
status=status,
|
|
|
|
|
progress=progress,
|
|
|
|
|
status_text=status_text,
|
|
|
|
|
result=result,
|
|
|
|
|
)
|
2022-11-24 02:08:58 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
return set_job_status
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
|
|
|
|
|
def parse_line(line):
|
|
|
|
|
pattern = re.compile(r"[+-]?\d+\.\d+ \w+(?= freed)")
|
2022-12-13 01:44:52 +00:00
|
|
|
|
match = re.search(
|
|
|
|
|
pattern,
|
|
|
|
|
line,
|
2022-12-03 18:27:10 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-12-13 01:44:52 +00:00
|
|
|
|
if match is None:
|
2022-12-19 02:27:44 +00:00
|
|
|
|
return (
|
|
|
|
|
JobStatus.FINISHED,
|
|
|
|
|
100,
|
|
|
|
|
COMPLETED_WITH_ERROR,
|
|
|
|
|
RESULT_WAAS_NOT_FOUND_ERROR,
|
2022-12-03 18:27:10 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-12-13 01:44:52 +00:00
|
|
|
|
else:
|
2022-12-19 02:27:44 +00:00
|
|
|
|
return (
|
|
|
|
|
JobStatus.FINISHED,
|
|
|
|
|
100,
|
|
|
|
|
CLEAR_COMPLETED,
|
|
|
|
|
f"{match.group(0)} have been cleared",
|
2022-12-13 01:44:52 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def stream_process(
|
2022-12-19 02:27:44 +00:00
|
|
|
|
stream,
|
2022-12-13 01:44:52 +00:00
|
|
|
|
package_equal_to_percent,
|
2022-12-19 02:27:44 +00:00
|
|
|
|
set_job_status,
|
2022-12-13 01:44:52 +00:00
|
|
|
|
):
|
|
|
|
|
percent = 0
|
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
for line in stream:
|
2022-12-13 01:44:52 +00:00
|
|
|
|
if "deleting '/nix/store/" in line:
|
|
|
|
|
percent += package_equal_to_percent
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
set_job_status(
|
2022-12-13 01:44:52 +00:00
|
|
|
|
status=JobStatus.RUNNING,
|
|
|
|
|
progress=int(percent),
|
|
|
|
|
status_text="Сleaning...",
|
2022-12-03 18:27:10 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-12-13 01:44:52 +00:00
|
|
|
|
elif "store paths deleted," in line:
|
2022-12-19 02:27:44 +00:00
|
|
|
|
status = parse_line(line)
|
|
|
|
|
set_job_status(
|
|
|
|
|
status=status[0],
|
|
|
|
|
progress=status[1],
|
|
|
|
|
status_text=status[2],
|
|
|
|
|
result=status[3],
|
|
|
|
|
)
|
|
|
|
|
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
def get_dead_packages(output):
|
|
|
|
|
dead = len(re.findall("/nix/store/", output))
|
|
|
|
|
percent = None
|
|
|
|
|
if dead != 0:
|
|
|
|
|
percent = 100 / dead
|
|
|
|
|
return dead, percent
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
|
|
|
|
|
2022-12-13 01:44:52 +00:00
|
|
|
|
def nix_collect_garbage(
|
2022-12-19 02:27:44 +00:00
|
|
|
|
job,
|
|
|
|
|
jobs=Jobs,
|
2022-12-13 01:44:52 +00:00
|
|
|
|
run_nix_store=run_nix_store_print_dead,
|
|
|
|
|
run_nix_collect=run_nix_collect_garbage,
|
2022-12-19 02:27:44 +00:00
|
|
|
|
set_job_status=None,
|
2022-12-13 01:44:52 +00:00
|
|
|
|
): # innocent as a pure function
|
2022-12-19 02:27:44 +00:00
|
|
|
|
set_job_status = set_job_status or set_job_status_wrapper(jobs, job)
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
set_job_status(
|
2022-12-13 01:44:52 +00:00
|
|
|
|
status=JobStatus.RUNNING,
|
|
|
|
|
progress=0,
|
|
|
|
|
status_text="Сalculate the number of dead packages...",
|
|
|
|
|
)
|
2022-12-03 18:27:10 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
dead_packages, package_equal_to_percent = get_dead_packages(run_nix_store())
|
2022-12-13 01:44:52 +00:00
|
|
|
|
|
|
|
|
|
if dead_packages == 0:
|
2022-12-19 02:27:44 +00:00
|
|
|
|
set_job_status(
|
2022-12-13 01:44:52 +00:00
|
|
|
|
status=JobStatus.FINISHED,
|
|
|
|
|
progress=100,
|
|
|
|
|
status_text="Nothing to clear",
|
|
|
|
|
result="System is clear",
|
|
|
|
|
)
|
2022-12-19 02:27:44 +00:00
|
|
|
|
return
|
2022-12-13 01:44:52 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
set_job_status(
|
2022-12-13 01:44:52 +00:00
|
|
|
|
status=JobStatus.RUNNING,
|
|
|
|
|
progress=0,
|
|
|
|
|
status_text=f"Found {dead_packages} packages to remove!",
|
2022-11-24 02:08:58 +00:00
|
|
|
|
)
|
2022-12-13 01:44:52 +00:00
|
|
|
|
|
2022-12-19 02:27:44 +00:00
|
|
|
|
stream_process(run_nix_collect(), package_equal_to_percent, set_job_status)
|