From 5a42e6f25cc18d02ab5bf45259b7e0cf50370d9e Mon Sep 17 00:00:00 2001 From: saket0187 Date: Mon, 25 May 2026 15:33:09 +0530 Subject: [PATCH 1/2] Remove g.mapset and use gs.create_environment --- python/grass/temporal/stds_import.py | 40 +++++++++++++++------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/python/grass/temporal/stds_import.py b/python/grass/temporal/stds_import.py index 6c21dc18d5b..c3bcb740d48 100644 --- a/python/grass/temporal/stds_import.py +++ b/python/grass/temporal/stds_import.py @@ -344,6 +344,8 @@ def import_stds( # Create a new location based on the projection information and switch # into it old_env = gs.gisenv() + target_gisrc = None + old_gisrc = os.environ.get("GISRC") if location: try: proj4_string = Path(proj_file_name).read_text() @@ -359,16 +361,17 @@ def import_stds( _("Unable to create location %(l)s. Reason: %(e)s") % {"l": location, "e": str(e)} ) - # Switch to the new created location + # Create a temporary environment try: - gs.run_command( - "g.mapset", - mapset="PERMANENT", - project=location, - dbase=old_env["GISDBASE"], + target_gisrc, target_env = gs.create_environment( + old_env["GISDBASE"], location, "PERMANENT" + ) + os.environ["GISRC"] = target_gisrc + except OSError as e: + gs.fatal( + _("Unable to create environment for location %s. Reason: %s") + % (location, e) ) - except CalledModuleError: - gs.fatal(_("Unable to switch to location %s") % location) # create default database connection try: gs.run_command("t.connect", flags="d") @@ -598,19 +601,18 @@ def import_stds( update_cmd_list=False, ) - os.chdir(old_cwd) # Make sure the location is switched back correctly finally: + os.chdir(old_cwd) if location: - # Switch to the old location - try: - gs.run_command( - "g.mapset", - mapset=old_env["MAPSET"], - project=old_env["LOCATION_NAME"], - dbase=old_env["GISDBASE"], - ) - except CalledModuleError: - gs.warning(_("Switching to original location failed")) + # Restore the original session + if old_gisrc is not None: + os.environ["GISRC"] = old_gisrc + else: + os.environ.pop("GISRC", None) + + # Delete the temporary session file + if target_gisrc: + gs.try_remove(target_gisrc) gs.set_raise_on_error(old_state) From e9d4189cfeee9a588d83e402c1dfcdb2b03b52f5 Mon Sep 17 00:00:00 2001 From: saket0187 Date: Tue, 2 Jun 2026 15:22:13 +0530 Subject: [PATCH 2/2] Fixed test file --- .../testsuite/test_temporal_rast_import.py | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/temporal/t.rast.import/testsuite/test_temporal_rast_import.py b/temporal/t.rast.import/testsuite/test_temporal_rast_import.py index 4a0decbf82d..1df553fa6e2 100644 --- a/temporal/t.rast.import/testsuite/test_temporal_rast_import.py +++ b/temporal/t.rast.import/testsuite/test_temporal_rast_import.py @@ -19,13 +19,17 @@ class TestRasterImport(TestCase): @classmethod def tearDownClass(cls): - """Remove the temporary region""" - cls.del_temp_region() + """Remove the imported data""" cls.runModule("t.remove", flags="df", inputs="A") def test_import(self): self.assertModule( - "t.rast.import", input=self.input_, output="A", basename="a", overwrite=True + "t.rast.import", + flags="o", + input=self.input_, + output="A", + basename="a", + overwrite=True, ) tinfo = """start_time='2000-01-01 00:00:00' end_time='2001-01-01 00:00:00' @@ -34,8 +38,14 @@ def test_import(self): north=320000.0 south=10000.0 east=935000.0 - west=120000.0 - """ + west=120000.0""" - info = SimpleModule("t.info", flags="g", input="A") - self.assertModuleKeyValue(module=info, reference=tinfo, precision=2, sep="=") + self.assertModuleKeyValue( + module="t.info", input="A", flags="g", reference=tinfo, precision=2, sep="=" + ) + + +if __name__ == "__main__": + from grass.gunittest.main import test + + test()