From c75f29f0de852ca8f6b55e233c3a12b96a2ebd74 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Sun, 5 May 2024 20:38:49 +0200 Subject: [PATCH 1/4] REFACTOR: Generic modules to avoid bad pratices Note: use 'secrets' module instead of random values created through 'random' module. --- pyaedt/generic/clr_module.py | 4 +++- pyaedt/generic/filesystem.py | 4 +++- pyaedt/generic/general_methods.py | 37 ++++++++++++------------------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/pyaedt/generic/clr_module.py b/pyaedt/generic/clr_module.py index 78ee75095d5..7564603c433 100644 --- a/pyaedt/generic/clr_module.py +++ b/pyaedt/generic/clr_module.py @@ -3,6 +3,8 @@ import sys import warnings +from pyaedt.aedt_logger import pyaedt_logger as logger + modules = [tup[1] for tup in pkgutil.iter_modules()] pyaedt_path = os.path.dirname(os.path.dirname(__file__)) cpython = "IronPython" not in sys.version and ".NETFramework" not in sys.version @@ -49,7 +51,7 @@ is_clr = True except Exception: - pass + logger.error("An error occurred while loading clr.") try: # work around a number formatting bug in the EDB API for non-English locales diff --git a/pyaedt/generic/filesystem.py b/pyaedt/generic/filesystem.py index d6fbc2f661c..694351b7a6d 100644 --- a/pyaedt/generic/filesystem.py +++ b/pyaedt/generic/filesystem.py @@ -3,6 +3,8 @@ import shutil import string +from pyaedt.aedt_logger import pyaedt_logger as logger + def search_files(dirname, pattern="*"): """Search for files inside a directory given a specific pattern. @@ -69,7 +71,7 @@ def remove(self): # TODO check why on Anaconda 3.7 get errors with os.path.exists shutil.rmtree(self._scratch_path, ignore_errors=True) except Exception: - pass + logger.error("An error occurred while removing {}".format(self._scratch_path)) def copyfile(self, src_file, dst_filename=None): """ diff --git a/pyaedt/generic/general_methods.py b/pyaedt/generic/general_methods.py index 5dad1863a90..929069fe39f 100644 --- a/pyaedt/generic/general_methods.py +++ b/pyaedt/generic/general_methods.py @@ -14,8 +14,8 @@ import json import math import os -import random import re +import secrets import string import sys import tempfile @@ -29,20 +29,11 @@ is_ironpython = "IronPython" in sys.version or ".NETFramework" in sys.version is_linux = os.name == "posix" is_windows = not is_linux -_pythonver = sys.version_info[0] -_python_current_release = sys.version_info[1] inside_desktop = True if is_ironpython and "4.0.30319.42000" in sys.version else False if not is_ironpython: import psutil -try: - import xml.etree.cElementTree as ET - - ET.VERSION -except ImportError: - ET = None - class GrpcApiError(Exception): """ """ @@ -137,7 +128,6 @@ def _exception(ex_info, func, args, kwargs, message="Type Error"): if message_to_print: _write_mes("Last Electronics Desktop Message - " + message_to_print) - args_name = [] try: args_dict = _get_args_dicts(func, args, kwargs) first_time_log = True @@ -149,12 +139,12 @@ def _exception(ex_info, func, args, kwargs, message="Type Error"): first_time_log = False _write_mes(" {} = {} ".format(el, args_dict[el])) except Exception: - pass - args = [func.__name__] + [i for i in args_name if i not in ["self"]] + pyaedt_logger.error("An error occurred while parsing and logging an error with method {}.") + if not func.__name__.startswith("_"): _write_mes( "Check Online documentation on: https://aedt.docs.pyansys.com/version/stable/search.html?q={}".format( - "+".join(args) + func.__name__ ) ) _write_mes(header) @@ -705,8 +695,8 @@ def generate_unique_name(rootname, suffix="", n=6): Newly generated name. """ - char_set = string.ascii_uppercase + string.digits - uName = "".join(random.choice(char_set) for _ in range(n)) + alphabet = string.ascii_uppercase + string.digits + uName = "".join(secrets.choice(alphabet) for _ in range(n)) unique_name = rootname + "_" + uName if suffix: unique_name += "_" + suffix @@ -799,7 +789,7 @@ def _retry_ntimes(n, function, *args, **kwargs): if function.__name__ == "InvokeAedtObjMethod": func_name = args[1] except Exception: - pass + pyaedt_logger.debug("An error occurred while accessing the arguments of a function " "called multiple times.") retry = 0 ret_val = None inclusion_list = [ @@ -1409,7 +1399,7 @@ def active_sessions(version=None, student_version=False, non_graphical=False): return_dict[p.pid] = i.laddr.port break except Exception: - pass + pyaedt_logger.error("An error occurred while retrieving information for the active AEDT sessions") return return_dict @@ -1910,8 +1900,9 @@ def _uname(name=None): str """ - char_set = string.ascii_uppercase + string.digits - unique_name = "".join(random.sample(char_set, 6)) + alphabet = string.ascii_uppercase + string.digits + generator = secrets.SystemRandom() + unique_name = "".join(secrets.SystemRandom.sample(generator, alphabet, 6)) if name: return name + unique_name else: @@ -1995,7 +1986,7 @@ def _check_installed_version(install_path, long_version): if install_version == long_version: return True except Exception: - pass + pyaedt_logger.debug("An error occurred while parsing installation version") return False @@ -2015,9 +2006,9 @@ def install_with_pip(package_name, package_path=None, upgrade=False, uninstall=F Whether to install the package or uninstall the package. """ if is_linux and is_ironpython: - import subprocessdotnet as subprocess + import subprocessdotnet as subprocess # nosec B404 else: - import subprocess + import subprocess # nosec B404 executable = '"{}"'.format(sys.executable) if is_windows else sys.executable commands = [] From 7eca143ae4dd0c772877f83c51eed2241a81f393 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Mon, 6 May 2024 08:52:35 +0200 Subject: [PATCH 2/4] REFACTOR: Remove import to removed variable --- pyaedt/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyaedt/__init__.py b/pyaedt/__init__.py index dbfbf26f363..9ce689dce41 100644 --- a/pyaedt/__init__.py +++ b/pyaedt/__init__.py @@ -49,7 +49,6 @@ def custom_show_warning(message, category, filename, lineno, file=None, line=Non from pyaedt.generic import constants import pyaedt.generic.DataHandlers as data_handler import pyaedt.generic.general_methods as general_methods -from pyaedt.generic.general_methods import _pythonver from pyaedt.generic.general_methods import _retry_ntimes from pyaedt.generic.general_methods import generate_unique_folder_name from pyaedt.generic.general_methods import generate_unique_name From daadc5aa4d432f0f36561a2fa54305ac3a80fb14 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Mon, 6 May 2024 09:16:58 +0200 Subject: [PATCH 3/4] CI: Fix nightly-docs needs --- .github/workflows/nightly-docs.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/nightly-docs.yml b/.github/workflows/nightly-docs.yml index 9c9ccb30c6b..f729d9ea891 100644 --- a/.github/workflows/nightly-docs.yml +++ b/.github/workflows/nightly-docs.yml @@ -20,7 +20,6 @@ jobs: doc-build: name: Documentation build without examples runs-on: ubuntu-latest - needs: [doc-style] steps: - name: Install Git and checkout project uses: actions/checkout@v4 @@ -87,7 +86,7 @@ jobs: upload-dev-doc: name: Upload dev documentation runs-on: ubuntu-latest - needs: [doc-build] + needs: doc-build steps: - name: Upload development documentation uses: ansys/actions/doc-deploy-dev@v4 From e2cb727cf02a7b3c494a49150fd0213a67e40125 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Mon, 6 May 2024 09:36:35 +0200 Subject: [PATCH 4/4] FIX: Use random in IronPython Note: package secrets is not available with IronPython --- pyaedt/generic/general_methods.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pyaedt/generic/general_methods.py b/pyaedt/generic/general_methods.py index 929069fe39f..ccdecf72d88 100644 --- a/pyaedt/generic/general_methods.py +++ b/pyaedt/generic/general_methods.py @@ -15,7 +15,6 @@ import math import os import re -import secrets import string import sys import tempfile @@ -676,6 +675,7 @@ def get_filename_without_extension(path): return os.path.splitext(os.path.split(path)[1])[0] +# FIXME: Remove usage of random module once IronPython compatibility is removed @pyaedt_function_handler() def generate_unique_name(rootname, suffix="", n=6): """Generate a new name given a root name and optional suffix. @@ -696,7 +696,15 @@ def generate_unique_name(rootname, suffix="", n=6): """ alphabet = string.ascii_uppercase + string.digits - uName = "".join(secrets.choice(alphabet) for _ in range(n)) + if is_ironpython: + import random + + uName = "".join(random.choice(alphabet) for _ in range(n)) # nosec B311 + else: + import secrets + + uName = "".join(secrets.choice(alphabet) for _ in range(n)) + unique_name = rootname + "_" + uName if suffix: unique_name += "_" + suffix @@ -1887,6 +1895,7 @@ def _arg2dict(arg, dict_out): raise ValueError("Incorrect data argument format") +# FIXME: Remove usage of random module once IronPython compatibility is removed def _uname(name=None): """Append a 6-digit hash code to a specified name. @@ -1901,8 +1910,15 @@ def _uname(name=None): """ alphabet = string.ascii_uppercase + string.digits - generator = secrets.SystemRandom() - unique_name = "".join(secrets.SystemRandom.sample(generator, alphabet, 6)) + if is_ironpython: + import random + + unique_name = "".join(random.sample(alphabet, 6)) # nosec B311 + else: + import secrets + + generator = secrets.SystemRandom() + unique_name = "".join(secrets.SystemRandom.sample(generator, alphabet, 6)) if name: return name + unique_name else: