Repository: NTNU-IHB/PythonFMU
Pull request #93 (2020-03-19 08:32:16)

Return fmu from builder


Patch:
diff --git a/pythonfmu/builder.py b/pythonfmu/builder.py
index bd4a219..6f9467c 100644
--- a/pythonfmu/builder.py
+++ b/pythonfmu/builder.py
@@ -62,7 +62,6 @@ class ModelDescriptionFetcher:
 
 
 class FmuBuilder:
-    FMI2SLAVE_FILENAME = HERE / "fmi2slave.py"
 
     @staticmethod
     def build_FMU(
@@ -71,7 +70,7 @@ class FmuBuilder:
         project_files: Iterable[FilePath] = set(),
         documentation_folder: Optional[FilePath] = None,
         **options,
-    ):
+    ) -> Path:
         script_file = Path(script_file)
         if not script_file.exists():
             raise ValueError(f"No such file {script_file!s}")
@@ -190,6 +189,8 @@ class FmuBuilder:
                     "modelDescription.xml", xml_str.toprettyxml(encoding="UTF-8")
                 )
 
+            return dest_file
+
     @staticmethod
     def has_binary() -> bool:
         """Does the binary for this platform exits?"""


Test patch:
diff --git a/pythonfmu/tests/test_builder.py b/pythonfmu/tests/test_builder.py
index abe71cc..127d577 100644
--- a/pythonfmu/tests/test_builder.py
+++ b/pythonfmu/tests/test_builder.py
@@ -27,9 +27,7 @@ lib_extension = ({"Darwin": "dylib", "Linux": "so", "Windows": "dll"}).get(
 def test_zip_content(tmp_path):
     script_file = Path(__file__).parent / DEMO
 
-    FmuBuilder.build_FMU(script_file, dest=tmp_path)
-
-    fmu = tmp_path / "PythonSlave.fmu"
+    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path)
     assert fmu.exists()
     assert zipfile.is_zipfile(fmu)
 
@@ -77,33 +75,34 @@ def test_project_files(tmp_path, pfiles):
     script_file = Path(__file__).parent / DEMO
     pfiles = map(Path, pfiles)
 
-    with tempfile.TemporaryDirectory() as project_dir:
-        project_dir = Path(project_dir)
-
-        project_files = set()
-        for f in pfiles:
-            full_name = project_dir / f
-            if full_name.suffix:
-                full_name.parent.mkdir(parents=True, exist_ok=True)
-                full_name.write_text("dummy content")
-            else:
-                full_name.mkdir(parents=True, exist_ok=True)
-
-            # Add subfolder and not file if common parent exits
-            to_remove = None
-            for p in project_files:
-                if p.parent == full_name.parent or p == full_name.parent:
-                    to_remove = p
-                    break
-            if to_remove is None:
-                project_files.add(full_name)
-            else:
-                project_files.remove(to_remove)
-                project_files.add(full_name.parent)
-
-        FmuBuilder.build_FMU(script_file, dest=tmp_path, project_files=project_files)
-
-    fmu = tmp_path / "PythonSlave.fmu"
+    def build():
+        with tempfile.TemporaryDirectory() as project_dir:
+            project_dir = Path(project_dir)
+
+            project_files = set()
+            for f in pfiles:
+                full_name = project_dir / f
+                if full_name.suffix:
+                    full_name.parent.mkdir(parents=True, exist_ok=True)
+                    full_name.write_text("dummy content")
+                else:
+                    full_name.mkdir(parents=True, exist_ok=True)
+
+                # Add subfolder and not file if common parent exits
+                to_remove = None
+                for p in project_files:
+                    if p.parent == full_name.parent or p == full_name.parent:
+                        to_remove = p
+                        break
+                if to_remove is None:
+                    project_files.add(full_name)
+                else:
+                    project_files.remove(to_remove)
+                    project_files.add(full_name.parent)
+
+            return FmuBuilder.build_FMU(script_file, dest=tmp_path, project_files=project_files)
+
+    fmu = build()
     with zipfile.ZipFile(fmu) as files:
         names = files.namelist()
 
@@ -124,25 +123,26 @@ def test_project_files_containing_script(tmp_path, pfiles):
     orig_script_file = Path(__file__).parent / DEMO
     pfiles = map(Path, pfiles)
 
-    with tempfile.TemporaryDirectory() as project_dir:
-        project_dir = Path(project_dir)
-        script_file = project_dir / DEMO
-        with open(orig_script_file) as script_f:
-            script_file.write_text(script_f.read())
-
-        for f in pfiles:
-            full_name = project_dir / f
-            if full_name.suffix:
-                full_name.parent.mkdir(parents=True, exist_ok=True)
-                full_name.write_text("dummy content")
-            else:
-                full_name.mkdir(parents=True, exist_ok=True)
-
-        FmuBuilder.build_FMU(
-            script_file, dest=tmp_path, project_files=[script_file.parent]
-        )
-
-    fmu = tmp_path / "PythonSlave.fmu"
+    def build():
+        with tempfile.TemporaryDirectory() as project_dir:
+            project_dir = Path(project_dir)
+            script_file = project_dir / DEMO
+            with open(orig_script_file) as script_f:
+                script_file.write_text(script_f.read())
+
+            for f in pfiles:
+                full_name = project_dir / f
+                if full_name.suffix:
+                    full_name.parent.mkdir(parents=True, exist_ok=True)
+                    full_name.write_text("dummy content")
+                else:
+                    full_name.mkdir(parents=True, exist_ok=True)
+
+            return FmuBuilder.build_FMU(
+                script_file, dest=tmp_path, project_files=[script_file.parent]
+            )
+
+    fmu = build()
     with zipfile.ZipFile(fmu) as files:
         names = files.namelist()
 
@@ -161,18 +161,17 @@ def test_project_files_containing_script(tmp_path, pfiles):
 def test_documentation(tmp_path):
     script_file = Path(__file__).parent / DEMO
 
-    with tempfile.TemporaryDirectory() as documentation_dir:
-        doc_dir = Path(documentation_dir)
-        license_file = doc_dir / "licenses" / "license.txt"
-        license_file.parent.mkdir()
-        license_file.write_text("Dummy license")
-        index_file = doc_dir / "index.html"
-        index_file.write_text("dummy index")
-
-        FmuBuilder.build_FMU(script_file, dest=tmp_path, documentation_folder=doc_dir)
-
-    fmu = tmp_path / "PythonSlave.fmu"
-
+    def build():
+        with tempfile.TemporaryDirectory() as documentation_dir:
+            doc_dir = Path(documentation_dir)
+            license_file = doc_dir / "licenses" / "license.txt"
+            license_file.parent.mkdir()
+            license_file.write_text("Dummy license")
+            index_file = doc_dir / "index.html"
+            index_file.write_text("dummy index")
+            return FmuBuilder.build_FMU(script_file, dest=tmp_path, documentation_folder=doc_dir)
+
+    fmu = build()
     with zipfile.ZipFile(fmu) as files:
         names = files.namelist()
 
diff --git a/pythonfmu/tests/test_integration.py b/pythonfmu/tests/test_integration.py
index 0445d60..ce70abb 100644
--- a/pythonfmu/tests/test_integration.py
+++ b/pythonfmu/tests/test_integration.py
@@ -19,9 +19,7 @@ DEMO = "pythonslave.py"
 def test_integration_demo(tmp_path):
     script_file = Path(__file__).parent / DEMO
 
-    FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")
-
-    fmu = tmp_path / "PythonSlave.fmu"
+    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")
     assert fmu.exists()
     model = pyfmi.load_fmu(str(fmu))
     res = model.simulate(final_time=0.5)
@@ -33,9 +31,7 @@ def test_integration_demo(tmp_path):
 def test_integration_reset(tmp_path):
     script_file = Path(__file__).parent / DEMO
 
-    FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")
-
-    fmu = tmp_path / "PythonSlave.fmu"
+    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")
     assert fmu.exists()
 
     vr = 5  # realOut
@@ -55,13 +51,11 @@ def test_integration_reset(tmp_path):
 def test_integration_get_state(tmp_path):
     script_file = Path(__file__).parent / DEMO
 
-    FmuBuilder.build_FMU(
+    fmu = FmuBuilder.build_FMU(
         script_file,
         dest=tmp_path,
         needsExecutionTool="false",
         canGetAndSetFMUstate="true")
-
-    fmu = tmp_path / "PythonSlave.fmu"
     assert fmu.exists()
 
     model = pyfmi.load_fmu(str(fmu))
@@ -96,13 +90,11 @@ def test_integration_get_serialize_state(tmp_path):
 
     script_file = Path(__file__).parent / DEMO
 
-    FmuBuilder.build_FMU(
+    fmu = FmuBuilder.build_FMU(
         script_file,
         dest=tmp_path,
         canGetAndSetFMUstate="true",
         canSerializeFMUstate="true")
-
-    fmu = tmp_path / "PythonSlave.fmu"
     assert fmu.exists()
 
     model_description = fmpy.read_model_description(fmu)
@@ -155,9 +147,7 @@ def test_integration_get_serialize_state(tmp_path):
 def test_integration_get(tmp_path):
     script_file = Path(__file__).parent / DEMO
 
-    FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")
-
-    fmu = tmp_path / "PythonSlave.fmu"
+    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")
     assert fmu.exists()
     model = pyfmi.load_fmu(str(fmu))
 
@@ -195,9 +185,7 @@ def test_integration_get(tmp_path):
 def test_integration_set(tmp_path):
     script_file = Path(__file__).parent / DEMO
 
-    FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")
-
-    fmu = tmp_path / "PythonSlave.fmu"
+    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")
     assert fmu.exists()
     model = pyfmi.load_fmu(str(fmu))
 
@@ -239,9 +227,7 @@ def test_simple_integration_fmpy(tmp_path):
 
     script_file = Path(__file__).parent / DEMO
 
-    FmuBuilder.build_FMU(script_file, dest=tmp_path)
-
-    fmu = tmp_path / "PythonSlave.fmu"
+    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path)
     assert fmu.exists()
     res = fmpy.simulate_fmu(str(fmu), stop_time=2.0)
 
@@ -284,14 +270,12 @@ def get_time_constant():
     local_file = script_file.parent / "localmodule.py"
     local_file.write_text(local_module)
 
-    FmuBuilder.build_FMU(
+    fmu = FmuBuilder.build_FMU(
         script_file,
         dest=tmp_path,
         project_files=[local_file],
         needsExecutionTool="false",
     )
-
-    fmu = tmp_path / "PythonSlaveWithDep.fmu"
     assert fmu.exists()
     model = pyfmi.load_fmu(str(fmu))
     res = model.simulate(final_time=0.5)
@@ -329,9 +313,7 @@ class PythonSlaveWithException(Fmi2Slave):
     script_file.parent.mkdir(parents=True, exist_ok=True)
     script_file.write_text(slave_code)
 
-    FmuBuilder.build_FMU(script_file, dest=tmp_path)
-
-    fmu = tmp_path / "PythonSlaveWithException.fmu"
+    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path)
     assert fmu.exists()
 
     with pytest.raises(Exception):
diff --git a/pythonfmu/tests/test_logger.py b/pythonfmu/tests/test_logger.py
index ca1cf50..22c83e5 100644
--- a/pythonfmu/tests/test_logger.py
+++ b/pythonfmu/tests/test_logger.py
@@ -57,8 +57,7 @@ class {name}(Fmi2Slave):
     script_file.parent.mkdir(parents=True, exist_ok=True)
     script_file.write_text(slave_code)
 
-    FmuBuilder.build_FMU(script_file, dest=tmp_path)
-    fmu = tmp_path / f"{name}.fmu"
+    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path)
     assert fmu.exists()
 
     logger = MagicMock()
@@ -127,8 +126,7 @@ class {name}(Fmi2Slave):
     script_file.parent.mkdir(parents=True, exist_ok=True)
     script_file.write_text(slave_code)
 
-    FmuBuilder.build_FMU(script_file, dest=tmp_path)
-    fmu = tmp_path / f"{name}.fmu"
+    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path)
     assert fmu.exists()
 
     logger = MagicMock()