Skip to content

Commit 9adb039

Browse files
committed
feat: add support for finding virtual environments with pyvenv and remove duplicates
1 parent 0f719cf commit 9adb039

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

killpy/__main__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ def find_venvs(base_directory: Path):
4040
return venvs
4141

4242

43+
def find_venvs_with_pyvenv(base_directory: Path):
44+
venvs = []
45+
for dir_path in base_directory.rglob("pyvenv.cfg"):
46+
venv_dir = dir_path.parent
47+
last_modified_timestamp = dir_path.stat().st_mtime
48+
last_modified = datetime.fromtimestamp(last_modified_timestamp).strftime(
49+
"%d/%m/%Y"
50+
)
51+
size = get_total_size(venv_dir)
52+
size_to_show = format_size(size)
53+
venvs.append((venv_dir, "pyvenv.cfg", last_modified, size, size_to_show))
54+
55+
venvs.sort(key=lambda x: x[2], reverse=True)
56+
return venvs
57+
58+
4359
def remove_conda_env(env_name):
4460
try:
4561
subprocess.run(
@@ -87,6 +103,19 @@ def list_conda_environments():
87103
return []
88104

89105

106+
def remove_duplicates(venvs):
107+
seen_paths = set()
108+
unique_venvs = []
109+
110+
for venv in venvs:
111+
venv_path = venv[0]
112+
if venv_path not in seen_paths:
113+
unique_venvs.append(venv)
114+
seen_paths.add(venv_path)
115+
116+
return unique_venvs
117+
118+
90119
class TableApp(App):
91120
deleted_cells: Coordinate = []
92121
bytes_release: int = 0
@@ -131,6 +160,8 @@ def on_mount(self) -> None:
131160

132161
venvs = find_venvs(current_directory)
133162
venvs += list_conda_environments()
163+
venvs += find_venvs_with_pyvenv(current_directory)
164+
venvs = remove_duplicates(venvs)
134165
table = self.query_one(DataTable)
135166
table.focus()
136167
table.add_columns(

0 commit comments

Comments
 (0)