-
Notifications
You must be signed in to change notification settings - Fork 1
/
clean-up-workspaces.py
58 lines (47 loc) · 1.81 KB
/
clean-up-workspaces.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
from pathlib import Path
import os
import time
import shutil
from datetime import datetime
# Define the workspaces directory
workspaces_directory = Path("/workspaces-streamlit-template")
# Get the current time in seconds
current_time = time.time()
# Define the time threshold (7 days ago) 86400 seconds in a day
threshold = current_time - (86400 * 7)
# Print current time
print(
f"Current Time: {datetime.utcfromtimestamp(current_time).strftime('%Y-%m-%d %H:%M:%S UTC')}\n"
)
# Collect remaining dirctories to print out later
remaining_directories = []
# Iterate through directories in workspaces_directory
for directory in workspaces_directory.iterdir():
# Check if it's a directory
if directory.is_dir():
# Get the directory's modification time
modification_time = os.path.getmtime(directory)
# Check if the modification time is less than the threshold
if modification_time < threshold:
# Calculate the time difference in seconds
time_difference = current_time - modification_time
# Print the directory name and the time difference in minutes
print(
f"Deleting directory: {directory.name}, Last Modified: {time_difference / 86400:.1f} days ago"
)
# Remove workspace
shutil.rmtree(directory)
else:
remaining_directories.append(directory)
# Print info on remaining directories
if remaining_directories:
print(f"\nRemaining directories in {workspaces_directory.name}:")
for directory in remaining_directories:
print(
f"{directory.name}, Last Modified: {(current_time - os.path.getmtime(directory)) / 60:.2f} minutes ago"
)
else:
print(f"\n{workspaces_directory.name} is empty.")
# Print separator
print(100 * "-")