Skip to content

Commit

Permalink
feat: refactoring create_temp_dir (#3239)
Browse files Browse the repository at this point in the history
* refactor: create_temp_dir function

* feat: using create_temp_dir in mapdlpool

* docs: API typo

* test: adding unit test

* chore: adding changelog file 3239.miscellaneous.md

* chore: adding changelog file 3239.miscellaneous.md

* Apply suggestions from code review

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: wrong test variable

* feat: using tempfile module more extensively.

* fix: testing changing user id and group

* chore: adding changelog file 3239.changed.md

* fix: user id

* chore: undo testing commits

* chore: adding changelog file 3239.miscellaneous.md

* refactor: using context to avoid delete error.

* fix: name call

* fix: wrong method name

* fix: removing __del__ method.

* Revert using NameTemp

This reverts commit a7a69cb.

---------

Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 17, 2024
1 parent 6de31cc commit 396ac7b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3239.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: refactoring `create_temp_dir`
31 changes: 16 additions & 15 deletions src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1884,24 +1884,25 @@ def _flush_stored(self):
if self._apdl_log:
self._apdl_log.write(commands + "\n")

# write to a temporary input file
tmp_inp = os.path.join(tempfile.gettempdir(), f"tmp_{rnd_str}.inp")
self._log.debug(
"Writing the following commands to a temporary " "apdl input file:\n%s",
commands,
)
self._store_commands = False
self._stored_commands = []

with open(tmp_inp, "w") as f:
f.writelines(commands)
# write to a temporary input file
self._log.debug(
"Writing the following commands to a temporary " "apdl input file:\n%s",
commands,
)

self._store_commands = False
self._stored_commands = []
tmp_inp = os.path.join(tempfile.gettempdir(), f"tmp_{random_string()}.inp")
with open(tmp_inp, "w") as f:
f.writelines(commands)

# interactive result
_ = self.input(tmp_inp, write_to_log=False)

# interactive result
_ = self.input(tmp_inp, write_to_log=False)
time.sleep(0.1) # allow MAPDL to close the file
if os.path.isfile(tmp_out):
self._response = "\n" + open(tmp_out).read()
time.sleep(0.1) # allow MAPDL to close the file
if os.path.isfile(tmp_out):
self._response = "\n" + open(tmp_out).read()

if self._response is None: # pragma: no cover
self._log.warning("Unable to read response from flushed commands")
Expand Down
7 changes: 4 additions & 3 deletions src/ansys/mapdl/core/mapdl_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -2198,9 +2198,10 @@ def load_table(self, name, array, var1="", var2="", var3="", csysid=""):
# skip the first line its a header we wrote in np.savetxt
self.tread(name, filename, nskip=1, mute=True)

if self._local:
os.remove(filename)
else:
# skip the first line its a header we wrote in np.savetxt
self.tread(name, filename, nskip=1, mute=True)

if not self._local:
self.slashdelete(filename)

def load_array(self, name, array):
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/mapdl/core/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def get_name():
except: # pragma: no cover
raise MapdlRuntimeError(
"Unable to create temporary working "
f"directory {path}.\nSpecify 'run_location' argument"
f"directory {path}\nPlease specify 'run_location' argument"
)

return path
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2294,7 +2294,7 @@ def test_use_vtk(mapdl):

@requires("local")
@pytest.mark.xfail(reason="Flaky test. See #2435")
def test__remove_temp_dir_on_exit(mapdl, tmpdir):
def test_remove_temp_dir_on_exit(mapdl, tmpdir):
path = os.path.join(tempfile.gettempdir(), "ansys_" + random_string())
os.makedirs(path)
filename = os.path.join(path, "file.txt")
Expand Down
7 changes: 1 addition & 6 deletions tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@

class TestMapdlPool:

def __del__(self):
# making sure we are deleting everything
for each_mapdl in self.pool._instances:
each_mapdl.exit(force=True)

@pytest.fixture(scope="class")
def pool_creator(self, tmpdir_factory):
run_path = str(tmpdir_factory.mktemp("ansys_pool"))
Expand Down Expand Up @@ -102,7 +97,7 @@ def pool_creator(self, tmpdir_factory):
wait=True,
)

self.pool = mapdl_pool
self._pool = mapdl_pool
VALID_PORTS.extend(mapdl_pool._ports)

yield mapdl_pool
Expand Down

0 comments on commit 396ac7b

Please sign in to comment.