From 05786131f670145d7055af6a24f1082c6025b37d Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 10 Jul 2024 14:19:41 +0200 Subject: [PATCH 1/2] ci: run extended array based on the person who open the PR (#3256) * testing: adding printing message * chore: adding changelog file 3256.added.md * ci: check type of event, and using pull request opener if it is a pull request. * chore: adding changelog file 3256.added.md * chore: clean up messages --------- Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> --- .github/workflows/ci.yml | 17 ++++++++++++++++- doc/changelog.d/3256.added.md | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 doc/changelog.d/3256.added.md diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d524cc4fab..04e1910ac9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -339,10 +339,25 @@ jobs: - name: "Install Git and checkout project" uses: actions/checkout@v4.1.7 + - name: Get event type and user to check permissions. + id: get_user + env: + type_event: ${{ github.event.issue.pull_request }} + run: | + if [[ $type_event ]]; then + echo "Event type: $type_event" + echo "event_type=$( echo "$type_event" )" >> $GITHUB_OUTPUT + export user=${{ github.event.pull_request.user.login }} + else + export user=${{ github.actor }} + fi + echo "This PR has been opened by: $user" + echo "user=$( echo "$user" )" >> $GITHUB_OUTPUT + - uses: tspascoal/get-user-teams-membership@v3 id: is_organization_member with: - username: ${{ github.actor }} + username: ${{ steps.get_user.outputs.user }} organization: ansys team: 'pymapdl-developers' GITHUB_TOKEN: ${{ secrets.TOKEN_TEAMS_USER_READ }} diff --git a/doc/changelog.d/3256.added.md b/doc/changelog.d/3256.added.md new file mode 100644 index 0000000000..880acd6a4d --- /dev/null +++ b/doc/changelog.d/3256.added.md @@ -0,0 +1 @@ +ci: run extended array based on the person who open the PR \ No newline at end of file From 6090584383649134c5303da4704e9964c463b868 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 10 Jul 2024 15:35:14 +0200 Subject: [PATCH 2/2] fix: Raising `ValueError` when using ips within pool library (#3240) * 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: Avoid raising error when using `launch_mapdl` within an `MapdlPool` instance. * feat: being more verbose about the errors raised by `protect_grpc`. * fix: dict not callable * chore: adding changelog file 3240.fixed.md * fix: wrong variable name * docs: fix spelling Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> * chore: adding changelog file 3240.fixed.md * fix: removing unused variable --------- 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> Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> --- doc/changelog.d/3239.miscellaneous.md | 1 + doc/changelog.d/3240.fixed.md | 1 + doc/source/api/pool.rst | 4 +-- src/ansys/mapdl/core/errors.py | 13 ++++++++- src/ansys/mapdl/core/launcher.py | 3 +- src/ansys/mapdl/core/misc.py | 40 ++++++++++++--------------- src/ansys/mapdl/core/pool.py | 4 +-- tests/test_misc.py | 14 ++++++++++ 8 files changed, 51 insertions(+), 29 deletions(-) create mode 100644 doc/changelog.d/3239.miscellaneous.md create mode 100644 doc/changelog.d/3240.fixed.md diff --git a/doc/changelog.d/3239.miscellaneous.md b/doc/changelog.d/3239.miscellaneous.md new file mode 100644 index 0000000000..69a82cd64a --- /dev/null +++ b/doc/changelog.d/3239.miscellaneous.md @@ -0,0 +1 @@ +feat: refactoring `create_temp_dir` \ No newline at end of file diff --git a/doc/changelog.d/3240.fixed.md b/doc/changelog.d/3240.fixed.md new file mode 100644 index 0000000000..8c1c3b5d5a --- /dev/null +++ b/doc/changelog.d/3240.fixed.md @@ -0,0 +1 @@ +fix: Raising `ValueError` when using ips within pool library \ No newline at end of file diff --git a/doc/source/api/pool.rst b/doc/source/api/pool.rst index b9134852d7..7231c3ef0c 100644 --- a/doc/source/api/pool.rst +++ b/doc/source/api/pool.rst @@ -1,7 +1,7 @@ .. _ref_pool_api: -Local MAPDL pool -================ +MAPDL pool +========== .. currentmodule:: ansys.mapdl.core diff --git a/src/ansys/mapdl/core/errors.py b/src/ansys/mapdl/core/errors.py index dfd858d921..dd6c8df4f7 100644 --- a/src/ansys/mapdl/core/errors.py +++ b/src/ansys/mapdl/core/errors.py @@ -309,6 +309,17 @@ def wrapper(*args, **kwargs): except: class_name = "" + # trying to get "cmd" argument: + cmd = args[1] if len(args) >= 2 else "" + cmd = kwargs.get("cmd", cmd) + + caller = func.__name__ + + if cmd: + msg_ = f"running:\n{cmd}\ncalled by:\n{caller}" + else: + msg_ = f"calling:{caller}\nwith the following arguments:\nargs: {list(*args)}\nkwargs: {list(**kwargs_)}" + if class_name == "MapdlGrpc": mapdl = args[0] elif hasattr(args[0], "_mapdl"): @@ -317,7 +328,7 @@ def wrapper(*args, **kwargs): # Must close unfinished processes mapdl._close_process() raise MapdlExitedError( - f"MAPDL server connection terminated with the following error\n{error}" + f"MAPDL server connection terminated unexpectedly while {msg_}\nwith the following error\n{error}" ) from None if threading.current_thread().__class__.__name__ == "_MainThread": diff --git a/src/ansys/mapdl/core/launcher.py b/src/ansys/mapdl/core/launcher.py index 8961324b95..f359f4afb1 100644 --- a/src/ansys/mapdl/core/launcher.py +++ b/src/ansys/mapdl/core/launcher.py @@ -1467,6 +1467,7 @@ def launch_mapdl( ) use_vtk = kwargs.pop("use_vtk", None) just_launch = kwargs.pop("just_launch", None) + on_pool = kwargs.pop("on_pool", False) _debug_no_launch = kwargs.pop("_debug_no_launch", None) # Transferring MAPDL arguments to start_parameters: @@ -1532,7 +1533,7 @@ def launch_mapdl( if ON_WSL: LOG.debug("On WSL: Allowing 'start_instance' and 'ip' arguments together.") else: - if start_instance is True: + if start_instance is True and not on_pool: raise ValueError( "When providing a value for the argument 'ip', the argument " "'start_instance' cannot be 'True'.\n" diff --git a/src/ansys/mapdl/core/misc.py b/src/ansys/mapdl/core/misc.py index 0be05d555c..e63cee8199 100644 --- a/src/ansys/mapdl/core/misc.py +++ b/src/ansys/mapdl/core/misc.py @@ -519,33 +519,27 @@ def create_temp_dir(tmpdir=None, name=None): """Create a new unique directory at a given temporary directory""" if tmpdir is None: tmpdir = tempfile.gettempdir() - elif not os.path.isdir(tmpdir): - os.makedirs(tmpdir) - if not name: - random_name = True - letters_ = string.ascii_lowercase.replace("n", "") - name = "ansys_" + random_string(10, letters_) - else: - random_name = False + # Possible letters + letters_ = string.ascii_lowercase.replace("n", "") - # running into a rare issue with MAPDL on Windows with "\n" being - # treated literally. - path = os.path.join(tmpdir, name) + def get_name(): + return random_string(10, letters_) - if random_name: - # in the *rare* case of a duplicate path - while os.path.isdir(path): - path = os.path.join(tempfile.gettempdir(), name) + name = name or get_name() + while os.path.exists(os.path.join(tmpdir, name)): + name = get_name() - if not os.path.exists(path): - try: - os.mkdir(path) - except: - raise MapdlRuntimeError( - "Unable to create temporary working " - f"directory {path}\nPlease specify 'run_location' argument" - ) + # create dir: + path = os.path.join(tmpdir, name) + + try: + os.mkdir(path) + except: # pragma: no cover + raise MapdlRuntimeError( + "Unable to create temporary working " + f"directory {path}.\nSpecify 'run_location' argument" + ) return path diff --git a/src/ansys/mapdl/core/pool.py b/src/ansys/mapdl/core/pool.py index 83aaf1acd6..707daaaa25 100755 --- a/src/ansys/mapdl/core/pool.py +++ b/src/ansys/mapdl/core/pool.py @@ -24,7 +24,6 @@ import os import shutil import socket -import tempfile import time from typing import Any, Dict, List, Optional, Union import warnings @@ -219,7 +218,7 @@ def __init__( _debug_no_launch = kwargs.pop("_debug_no_launch", None) if run_location is None: - run_location = tempfile.gettempdir() + run_location = create_temp_dir() self._root_dir: str = run_location kwargs["remove_temp_files"] = remove_temp_files @@ -875,6 +874,7 @@ def _spawn_mapdl( ip=ip, override=True, start_instance=start_instance, + on_pool=True, **self._spawn_kwargs, ) diff --git a/tests/test_misc.py b/tests/test_misc.py index 264da1811c..46f5db3301 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -23,6 +23,7 @@ """Small or misc tests that don't fit in other test modules""" import inspect import os +import pathlib import numpy as np import pytest @@ -37,6 +38,7 @@ check_valid_ip, check_valid_port, check_valid_routine, + create_temp_dir, last_created, load_file, no_return, @@ -376,3 +378,15 @@ def test_check_valid_routine(): assert check_valid_routine("begin level") with pytest.raises(ValueError, match="Invalid routine"): check_valid_routine("invalid") + + +@requires("local") +def test_create_temp_dir(): + + path = create_temp_dir() + + path = pathlib.Path(path) + parent = path.parent + dir_ = path.parts[-1] + + assert str(path) != create_temp_dir(parent, dir_)