Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactoring create_temp_dir #3239

Merged
merged 28 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5191109
refactor: create_temp_dir function
germa89 Jul 2, 2024
c420147
feat: using create_temp_dir in mapdlpool
germa89 Jul 2, 2024
d7e592b
docs: API typo
germa89 Jul 2, 2024
098e115
test: adding unit test
germa89 Jul 2, 2024
c1748d1
chore: adding changelog file 3239.miscellaneous.md
pyansys-ci-bot Jul 2, 2024
8ed9c48
chore: adding changelog file 3239.miscellaneous.md
pyansys-ci-bot Jul 2, 2024
8cc8c13
Apply suggestions from code review
germa89 Jul 2, 2024
1e2a37e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 2, 2024
af0d64a
Merge branch 'main' into feat/refactoring-create_temp_dir
germa89 Jul 8, 2024
e37417e
fix: wrong test variable
germa89 Jul 8, 2024
a7a69cb
feat: using tempfile module more extensively.
germa89 Jul 9, 2024
8c3f9a5
chore: Merge branch 'main' into feat/refactoring-create_temp_dir
germa89 Jul 9, 2024
d9ba531
Merge branch 'main' into feat/refactoring-create_temp_dir
germa89 Jul 10, 2024
709f72b
Merge branch 'main' into feat/refactoring-create_temp_dir
germa89 Jul 12, 2024
f6dce6d
Merge branch 'main' into feat/refactoring-create_temp_dir
germa89 Jul 12, 2024
6fe4e66
Merge branch 'main' into feat/refactoring-create_temp_dir
germa89 Jul 15, 2024
a4ed031
fix: testing changing user id and group
germa89 Jul 15, 2024
f74ba36
chore: adding changelog file 3239.changed.md
pyansys-ci-bot Jul 15, 2024
a61e715
fix: user id
germa89 Jul 15, 2024
a47ccfe
chore: undo testing commits
germa89 Jul 16, 2024
1b51910
Merge branch 'main' into feat/refactoring-create_temp_dir
germa89 Jul 16, 2024
ede902f
chore: adding changelog file 3239.miscellaneous.md
pyansys-ci-bot Jul 16, 2024
ba12a85
refactor: using context to avoid delete error.
germa89 Jul 16, 2024
36ae94f
fix: name call
germa89 Jul 17, 2024
0ea3535
Merge branch 'main' into feat/refactoring-create_temp_dir
germa89 Jul 17, 2024
4afcba3
fix: wrong method name
germa89 Jul 17, 2024
4a26b89
fix: removing __del__ method.
germa89 Jul 17, 2024
a7e8f8f
Revert using NameTemp
germa89 Jul 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
4 changes: 2 additions & 2 deletions doc/source/api/pool.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. _ref_pool_api:

Local MAPDL pool
================
MAPDL pool
==========

.. currentmodule:: ansys.mapdl.core

Expand Down
40 changes: 17 additions & 23 deletions src/ansys/mapdl/core/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,33 +519,27 @@
"""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 = random_string(10, letters_)
else:
random_name = False
# Possible letters
letters_ = string.ascii_lowercase.replace("n", "")
germa89 marked this conversation as resolved.
Show resolved Hide resolved

# 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:
germa89 marked this conversation as resolved.
Show resolved Hide resolved
raise MapdlRuntimeError(

Check warning on line 539 in src/ansys/mapdl/core/misc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/misc.py#L538-L539

Added lines #L538 - L539 were not covered by tests
"Unable to create temporary working "
f"directory {path}\nPlease specify 'run_location' argument"
)

return path

Expand Down
3 changes: 1 addition & 2 deletions src/ansys/mapdl/core/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import os
import shutil
import socket
import tempfile
import time
from typing import Any, Dict, List, Optional, Union
import warnings
Expand Down Expand Up @@ -219,7 +218,7 @@
_debug_no_launch = kwargs.pop("_debug_no_launch", None)

if run_location is None:
run_location = tempfile.gettempdir()
run_location = create_temp_dir()

Check warning on line 221 in src/ansys/mapdl/core/pool.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/pool.py#L221

Added line #L221 was not covered by tests
self._root_dir: str = run_location

kwargs["remove_temp_files"] = remove_temp_files
Expand Down
14 changes: 14 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,6 +38,7 @@
check_valid_ip,
check_valid_port,
check_valid_routine,
create_temp_dir,
last_created,
load_file,
no_return,
Expand Down Expand Up @@ -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]
germa89 marked this conversation as resolved.
Show resolved Hide resolved

assert str(path) != create_temp_dir(parent, dir)
Loading