diff --git a/python/grass/temporal/stds_import.py b/python/grass/temporal/stds_import.py index 6c21dc18d5b..c8aed5f9215 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,18 @@ 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 +602,22 @@ 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) + + from .core import init + + init() + + # Delete the temporary session file + if target_gisrc: + gs.try_remove(target_gisrc) gs.set_raise_on_error(old_state) diff --git a/temporal/t.rast.import/t.rast.import.py b/temporal/t.rast.import/t.rast.import.py index 7c176eff571..6c113cec784 100755 --- a/temporal/t.rast.import/t.rast.import.py +++ b/temporal/t.rast.import/t.rast.import.py @@ -69,7 +69,7 @@ # %option # % key: project # % type: string -# % description: Create a new project and import the data into it. Do not run this module in parallel or interrupt it when a new project should be created +# % description: Create a new project and import the data into it. # % required: no # % multiple: no # %end 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..8f16a1cdc3c 100644 --- a/temporal/t.rast.import/testsuite/test_temporal_rast_import.py +++ b/temporal/t.rast.import/testsuite/test_temporal_rast_import.py @@ -10,22 +10,30 @@ """ import os +import pathlib +import shutil +import grass.script as gs from grass.gunittest.case import TestCase class TestRasterImport(TestCase): input_ = os.path.join("data", "precip_2000.tar.bzip2") + new_project_name = "test_project_import" @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 +42,35 @@ 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="=" + ) + + def test_import_new_project(self): + """Test the project option.""" + path = os.path.join(gs.gisenv()["GISDBASE"], self.new_project_name) + original_env = gs.gisenv() + + shutil.rmtree(path, ignore_errors=True) + self.addCleanup(shutil.rmtree, path, ignore_errors=True) + + self.assertModule( + "t.rast.import", + flags="o", + input=self.input_, + output="B", + basename="b", + project=self.new_project_name, + overwrite=True, + ) + + self.assertTrue(pathlib.Path(path).exists()) + self.assertDictEqual(original_env, gs.gisenv()) + + +if __name__ == "__main__": + from grass.gunittest.main import test + + test()