]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Use tempfile.TemporaryDirectory() with with statement instead of gettempdir to avoid...
authorAnthony Geay <anthony.geay@edf.fr>
Tue, 31 Aug 2021 05:50:03 +0000 (07:50 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Tue, 31 Aug 2021 05:50:03 +0000 (07:50 +0200)
src/ConnectorAPI/Test/TestExportToGEOM.py
src/ConnectorAPI/Test/TestExportToGEOMAllGroupsAndFields.py
src/ConnectorAPI/Test/TestExportToGEOMWholeResult.py

index 5d3809d494cb006b6ac3007de53d44fa5532b70e..cdbb74f894de4a12c73d819abe09facd4c6d0d9c 100644 (file)
@@ -66,12 +66,6 @@ def dumpShaper(fileName):
   model.end()
   pass
 
-def getTmpFileName(ext):
-  tempdir = tempfile.gettempdir()
-  tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False)
-  tmp_filename = tmp_file.name
-  return tmp_filename
-
 # Create 2 boxes
 # Create a group of faces
 # Create a field of faces
index 27ed03a6f202a2c1d7f0125819f051e43317d27c..b954a79b6e10876bc4ef0b9ac3f9de77b3278142 100644 (file)
@@ -70,12 +70,6 @@ def dumpShaper(fileName):
   model.end()
   pass
 
-def getTmpFileName(ext):
-  tempdir = tempfile.gettempdir()
-  tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False)
-  tmp_filename = tmp_file.name
-  return tmp_filename
-
 def testGroupsAndFieldsExportToGEOM():
   model.begin()
   partSet = model.moduleDocument()
@@ -184,26 +178,23 @@ def checkResultInGEOM():
 
 def checkDump():
   # Dump the salome study (only CORBA modules, SHAPER dump is not in it)
-  tempdir = tempfile.gettempdir()
-  dumpFileGeomBase = "dump_test_geom"
-  dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase)
-  salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False)
-
-  # Dump SHAPER
-  dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py")
-  dumpShaper(dumpFileShaper)
+  with tempfile.TemporaryDirectory() as tempdir:
+    dumpFileGeomBase = "dump_test_geom"
+    dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase)
+    salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False)
 
-  # Load SHAPER dump
-  exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec'))
+    # Dump SHAPER
+    dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py")
+    dumpShaper(dumpFileShaper)
 
-  # Load GEOM dump
-  exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec'))
+    # Load SHAPER dump
+    exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec'))
 
-  # Clean files
-  files = [dumpFileGeom, dumpFileShaper]
-  for f in files:
-    os.remove(f)
+    # Load GEOM dump
+    exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec'))
 
+    # Clean files
+    files = [dumpFileGeom, dumpFileShaper]
   pass
 
 if __name__ == '__main__':
index 6bccc592b18472544ebc8f623e946f97cfed0e75..3c3a63cf3c4a904fc4ea6c8d3ba4de6fd4996fa9 100644 (file)
@@ -102,27 +102,25 @@ def testExportToGEOM():
   # Check that the group has 6+6 faces
   geomGroup_2 = getSubObject(geomObject_1, 2)
   assert geompy.NumberOfFaces(geomGroup_2) == 12
+  
+  with tempfile.TemporaryDirectory() as tempdir:
+    # Dump the salome study (only CORBA modules, SHAPER dump is not in it)
+    dumpFileGeomBase = "dump_test_geom"
+    dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase)
+    salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False)
 
-  # Dump the salome study (only CORBA modules, SHAPER dump is not in it)
-  tempdir = tempfile.gettempdir()
-  dumpFileGeomBase = "dump_test_geom"
-  dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase)
-  salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False)
+    # Dump SHAPER
+    dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py")
+    dumpShaper(dumpFileShaper)
 
-  # Dump SHAPER
-  dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py")
-  dumpShaper(dumpFileShaper)
+    # Load SHAPER dump
+    exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec'))
 
-  # Load SHAPER dump
-  exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec'))
+    # Load GEOM dump
+    exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec'))
 
-  # Load GEOM dump
-  exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec'))
-
-  # Clean files
-  files = [dumpFileGeom, dumpFileShaper]
-  for f in files:
-    os.remove(f)
+    # Clean files
+    files = [dumpFileGeom, dumpFileShaper]
 
   pass