forked from mundialis/grass-gis-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.py
117 lines (104 loc) · 3.9 KB
/
cleanup.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
############################################################################
#
# MODULE: lib with cleanup related helper functions for GRASS GIS
#
# AUTHOR(S): Anika Weinmann
#
# PURPOSE: lib with cleanup related helper functions for GRASS GIS
#
# COPYRIGHT: (C) 2023 by mundialis and the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
#############################################################################
import os
import shutil
import gc
from location import get_location_size
import grass.script as grass
def general_cleanup(
rm_rasters=[],
rm_vectors=[],
rm_files=[],
rm_dirs=[],
rm_groups=[],
rm_groups_wo_rasters=[],
rm_regions=[],
rm_strds=[],
orig_region=None,
):
"""General cleanup function"""
grass.message(_("Cleaning up..."))
nulldev = open(os.devnull, "w")
kwargs = {"flags": "f", "quiet": True, "stderr": nulldev}
for rmg in rm_groups:
if grass.find_file(name=rmg, element="group")["file"]:
group_rasters = grass.parse_command(
"i.group", flags="lg", group=rmg
)
rm_rasters.extend(group_rasters)
grass.run_command("g.remove", type="group", name=rmg, **kwargs)
for rmg_wor in rm_groups_wo_rasters:
if grass.find_file(name=rmg_wor, element="group")["file"]:
grass.run_command("g.remove", type="group", name=rmg_wor, **kwargs)
for rmrast in rm_rasters:
if grass.find_file(name=rmrast, element="raster")["file"]:
grass.run_command("g.remove", type="raster", name=rmrast, **kwargs)
for rmvect in rm_vectors:
if grass.find_file(name=rmvect, element="vector")["file"]:
grass.run_command("g.remove", type="vector", name=rmvect, **kwargs)
for rmfile in rm_files:
if os.path.isfile(rmfile):
os.remove(rmfile)
for rmdir in rm_dirs:
if os.path.isdir(rmdir):
shutil.rmtree(rmdir)
if orig_region is not None:
if grass.find_file(name=orig_region, element="windows")["file"]:
grass.run_command("g.region", region=orig_region)
grass.run_command(
"g.remove", type="region", name=orig_region, **kwargs
)
for rmreg in rm_regions:
if grass.find_file(name=rmreg, element="windows")["file"]:
grass.run_command("g.remove", type="region", name=rmreg, **kwargs)
strds = grass.parse_command("t.list", type="strds")
mapset = grass.gisenv()["MAPSET"]
for rm_s in rm_strds:
if f"{rm_s}@{mapset}" in strds:
grass.run_command(
"t.remove",
flags="rf",
type="strds",
input=rm_s,
quiet=True,
stderr=nulldev,
)
# get location size
get_location_size()
# Garbage Collector: release unreferenced memory
gc.collect()
def rm_vects(vects):
"""Function to remove clean vector maps
Args:
vects (list): list of vector maps which should be removed"""
nuldev = open(os.devnull, "w")
kwargs = {"flags": "f", "quiet": True, "stderr": nuldev}
for rmv in vects:
if grass.find_file(name=rmv, element="vector")["file"]:
grass.run_command("g.remove", type="vector", name=rmv, **kwargs)
def reset_region(region):
"""Function to set the region to the given region
Args:
region (str): the name of the saved region which should be set and
deleted
"""
nulldev = open(os.devnull, "w")
kwargs = {"flags": "f", "quiet": True, "stderr": nulldev}
if region:
if grass.find_file(name=region, element="windows")["file"]:
grass.run_command("g.region", region=region)
grass.run_command("g.remove", type="region", name=region, **kwargs)