Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r.slopeunits.optimize: clean up tmp mapsets #1283

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,29 @@
gisdbase = env["GISDBASE"]
location = env["LOCATION_NAME"]
master_mapset = env["MAPSET"]
rm_mapsets = []


def cleanup():
"""Cleanup function"""
current_mapset = gs.gisenv()["MAPSET"]
if current_mapset != master_mapset:
gs.run_command("g.mapset", mapset=master_mapset)
for mapset in rm_mapsets:
path = os.path.join(gisdbase, location, mapset)
if os.path.exists(path):
grass.utils.try_rmdir(path)

nuldev = open(os.devnull, "w")
kwargs = {"flags": "f", "quiet": True, "stderr": nuldev}
for rmrast in rm_rasters:
if gs.find_file(name=rmrast, element="cell")["file"]:
if gs.find_file(name=rmrast, element="cell", mapset=master_mapset)["file"]:
gs.run_command("g.remove", type="raster", name=rmrast, **kwargs)
for rmvect in rm_vectors:
if gs.find_file(name=rmvect, element="vector")["file"]:
if gs.find_file(name=rmvect, element="vector", mapset=master_mapset)["file"]:
gs.run_command("g.remove", type="vector", name=rmvect, **kwargs)

if gs.find_file("MASK")["file"]:
if gs.find_file(name="MASK", element="cell", mapset=master_mapset)["file"]:
# gs.run_command('r.mask', flags='r')
gs.run_command("g.remove", type="raster", name="MASK", flags="f", quiet=True)

Expand Down Expand Up @@ -197,6 +206,7 @@ def run_batch(
ico = COUNT_GLOBAL
mapset_prefix = "tmp_su"
nome_mapset = f"{mapset_prefix}_{ico}"
rm_mapsets.append(nome_mapset)

gs.utils.try_rmdir(os.path.join(gisdbase, location, nome_mapset))
gs.run_command("g.mapset", mapset=nome_mapset, flags="c")
Expand Down Expand Up @@ -607,7 +617,7 @@ def calculate_new_limits_caso_4(x_lims_cur, y_lims_cur):

def main():
"""Main function of r.slopeunits.optimize"""
global rm_rasters, rm_vectors
global rm_rasters, rm_vectors, rm_mapsets
global COUNT_GLOBAL

# pass fully qualified names of input maps
Expand Down
Loading