-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_projects_and_databases.py
71 lines (57 loc) · 1.94 KB
/
clean_projects_and_databases.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
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import bw2data as bd
from pathlib import Path
from tabulate import tabulate
DELETE = 0
HOME = Path.home()
bwstrings = ["bw", "brightway"]
bw2list = [x for x in os.listdir(HOME) if any(s in x for s in bwstrings)]
# output of bw2list
BW2DIRS = ["brightway25data-testing",
"brightway2data-testing",
"brightway25data",
"brightway2data"]
BW2DIRS = [os.path.join(HOME, x) for x in BW2DIRS]
# print a report for each directory
BW2DIR = BW2DIRS[0] # choose here (restart kernel to choose another)
print(f"Using: {BW2DIR}")
os.environ["BRIGHTWAY2_DIR"] = BW2DIR
assert os.environ.get("BRIGHTWAY2_DIR") == BW2DIR, "BRIGHTWAY2_DIR not set correctly"
import bw2data as bd
# Prepare data for tabulate
table_data = [(name, dbs, round(size, 2)) for name, dbs, size in bd.projects.report()]
headers = ["Name", "DBs", "Size (GB)"]
# Print the table
print("Projects:")
print(tabulate(table_data, headers=headers))
project_list = [x[0] for x in table_data]
# output of project_list
DELETE_LIST = [
"Premise-SSP2-cutoff",
"TreX-Premise-SSP2-cutoff",
# "TreX-test-premise-SSP2-cutoff",
# "default",
"premise-SSP2-cutoff",
# "test-premise-SSP2-cutoff",
]
print("\nAS LIST:")
print(project_list)
if DELETE:
print(f'{"-"*60}')
print("Deleting projects")
for project in DELETE_LIST:
if project not in project_list:
print(f"\t - Can't delete: {project}, not found")
continue
print(f"\t + Deleting {project}")
bd.projects.delete_project(project, delete_dir=True)
# check that it worked
print(f'{"-"*60}')
print("\nCheck that it worked...\n")
table_data = [(name, dbs, round(size, 2)) for name, dbs, size in bd.projects.report()]
# Print the table
print("Remaining projects:")
print(tabulate(table_data, headers=headers))
print("\n Purging deleted directories")
bd.projects.purge_deleted_directories()
print("\nDone!\n")