2
2
import itertools
3
3
import shutil
4
4
import sys
5
- import traceback
6
5
from pathlib import Path
7
6
8
7
import attr
9
8
import click
10
9
from _pytask .config import hookimpl
11
10
from _pytask .config import IGNORED_TEMPORARY_FILES_AND_FOLDERS
11
+ from _pytask .console import console
12
+ from _pytask .enums import ColorCode
12
13
from _pytask .enums import ExitCode
13
14
from _pytask .exceptions import CollectionError
15
+ from _pytask .path import find_common_ancestor
16
+ from _pytask .path import relative_to
14
17
from _pytask .pluginmanager import get_plugin_manager
15
18
from _pytask .session import Session
16
19
from _pytask .shared import get_first_non_none_value
20
+ from rich .traceback import Traceback
17
21
18
22
19
23
_HELP_TEXT_MODE = (
@@ -80,7 +84,7 @@ def clean(**config_from_cli):
80
84
except Exception :
81
85
session = Session ({}, None )
82
86
session .exit_code = ExitCode .CONFIGURATION_FAILED
83
- traceback . print_exception ( * sys .exc_info ())
87
+ console . print ( Traceback . from_exception ( * sys .exc_info () ))
84
88
85
89
else :
86
90
try :
@@ -92,38 +96,48 @@ def clean(**config_from_cli):
92
96
unknown_paths = _find_all_unknown_paths (
93
97
session , known_paths , include_directories
94
98
)
99
+ common_ancestor = find_common_ancestor (
100
+ * unknown_paths , * session .config ["paths" ]
101
+ )
95
102
96
103
if unknown_paths :
97
104
targets = "Files"
98
105
if session .config ["directories" ]:
99
106
targets += " and directories"
100
- click . echo (f"\n { targets } which can be removed:\n " )
107
+ console . print (f"\n { targets } which can be removed:\n " )
101
108
for path in unknown_paths :
109
+ short_path = relative_to (path , common_ancestor )
102
110
if session .config ["mode" ] == "dry-run" :
103
- click . echo (f"Would remove { path } " )
111
+ console . print (f"Would remove { short_path } " )
104
112
else :
105
113
should_be_deleted = session .config [
106
114
"mode"
107
115
] == "force" or click .confirm (
108
- f"Would you like to remove { path } ?"
116
+ f"Would you like to remove { short_path } ?"
109
117
)
110
118
if should_be_deleted :
111
119
if not session .config ["quiet" ]:
112
- click . echo (f"Remove { path } " )
120
+ console . print (f"Remove { short_path } " )
113
121
if path .is_dir ():
114
122
shutil .rmtree (path )
115
123
else :
116
124
path .unlink ()
117
125
else :
118
- click .echo ("\n There are no files and directories which can be deleted." )
126
+ console .print ()
127
+ console .print (
128
+ "There are no files and directories which can be deleted."
129
+ )
119
130
120
- click .echo ("\n " + "=" * config ["terminal_width" ])
131
+ console .print ()
132
+ console .rule (style = None )
121
133
122
134
except CollectionError :
123
135
session .exit_code = ExitCode .COLLECTION_FAILED
136
+ console .rule (style = ColorCode .FAILED )
124
137
125
138
except Exception :
126
- traceback .print_exception (* sys .exc_info ())
139
+ console .print (Traceback .from_exception (* sys .exc_info ()))
140
+ console .rule (style = ColorCode .FAILED )
127
141
session .exit_code = ExitCode .FAILED
128
142
129
143
sys .exit (session .exit_code )
0 commit comments