1
|
|
|
""" |
2
|
|
|
Module that cleans up temporary files. |
3
|
|
|
|
4
|
|
|
Original source: https://github.com/dmyersturnbull/tyrannosaurus |
5
|
|
|
Copyright 2020–2021 Douglas Myers-Turnbull |
6
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
you may not use this file except in compliance with the License. |
8
|
|
|
You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
""" |
10
|
|
|
|
11
|
|
|
from __future__ import annotations |
12
|
|
|
|
13
|
|
|
import logging |
14
|
|
|
from pathlib import Path |
15
|
|
|
from typing import Optional, Sequence |
16
|
|
|
from typing import Tuple as Tup |
17
|
|
|
|
18
|
|
|
from tyrannosaurus.context import Context |
19
|
|
|
from tyrannosaurus.helpers import TrashList, scandir_fast |
20
|
|
|
|
21
|
|
|
logger = logging.getLogger(__package__) |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class Clean: |
25
|
|
|
def __init__(self, dists: bool, aggressive: bool, hard_delete: bool, dry_run: bool): |
26
|
|
|
self.dists = dists |
27
|
|
|
self.aggressive = aggressive |
28
|
|
|
self.hard_delete = hard_delete |
29
|
|
|
self.dry_run = dry_run |
30
|
|
|
|
31
|
|
|
def clean(self, path: Path) -> Sequence[Tup[Path, Optional[Path]]]: |
32
|
|
|
context = Context(path, dry_run=self.dry_run) |
33
|
|
|
logger.info(f"Clearing {context.tmp_path}") |
34
|
|
|
trashed = [] |
35
|
|
|
destroyed = context.destroy_tmp() |
36
|
|
|
if destroyed: |
37
|
|
|
trashed.append((context.tmp_path, None)) |
38
|
|
|
trash = TrashList(self.dists, self.aggressive) |
39
|
|
|
# we're going to do these in order to save time overall |
40
|
|
|
for p in trash.get_list(): |
41
|
|
|
tup = context.trash(p, self.hard_delete) |
42
|
|
|
if tup[0] is not None: |
43
|
|
|
trashed.append(tup) |
44
|
|
|
for p in scandir_fast(path, trash): |
45
|
|
|
p = Path(p) |
46
|
|
|
if trash.should_delete(p): |
47
|
|
|
tup = context.trash(p, self.hard_delete) |
48
|
|
|
if tup[0] is not None: |
49
|
|
|
trashed.append(tup) |
50
|
|
|
return trashed |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
__all__ = ["Clean"] |
54
|
|
|
|