Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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/1162.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix: Improve code quality
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ classifiers = [

dependencies = [
"ansys-api-meshing-prime==0.1.4",
"docker>=7.1.0", # TODO: Discuss if this should be optional
"numpy>=1.14.0",
"appdirs>=1.4.0",
"importlib-metadata>=4.0,<5; python_version<='3.8'",
Expand Down
1 change: 1 addition & 0 deletions src/ansys/meshing/prime/core/dynaexportutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,7 @@ def _get_zone_with_id(self, _id):
zone_details = zone_details
return zone_name, zone_details
except:
self._logger.warning(f'Zone {zone} does not have id for it ')
pass
return None, {}

Expand Down
6 changes: 5 additions & 1 deletion src/ansys/meshing/prime/examples/download_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import urllib.request
from threading import Lock
from typing import Optional
from urllib.parse import urljoin
from urllib.parse import urljoin, urlparse

import ansys.meshing.prime.internals.defaults as defaults

Expand Down Expand Up @@ -142,6 +142,10 @@ def _get_filepath_on_default_server(self, filename: str, *directory: str):
return joiner(server, filename)

def _retrieve_url(self, url, dest):
parsed = urlparse(url)
if parsed.scheme not in ('http', 'https'):
raise ValueError(f"Unsupported URL scheme: {parsed.scheme}")

saved_file, _ = urllib.request.urlretrieve(url, filename=dest)
return saved_file

Expand Down
3 changes: 2 additions & 1 deletion src/ansys/meshing/prime/internals/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ def exit(self):
self._comm.close()
self._comm = None
if self._process is not None:
assert self._local == False
if self._local:
raise ValueError('Local client cannot have a server process')
terminate_process(self._process)
self._process = None

Expand Down
9 changes: 5 additions & 4 deletions src/ansys/meshing/prime/internals/grpc_communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

"""Module for communications with the gRPC server."""
__all__ = ['GRPCCommunicator']
import logging
from typing import Optional

import grpc
Expand All @@ -33,6 +34,7 @@
from ansys.meshing.prime.core.model import Model
from ansys.meshing.prime.internals.communicator import Communicator
from ansys.meshing.prime.internals.error_handling import (
PrimeRuntimeError,
communicator_error_handler,
error_code_handler,
)
Expand All @@ -58,7 +60,8 @@ def get_response_messages(response_generator):
if response.HasField('completion_token'):
break

assert response.HasField('content')
if not response.HasField('content'):
raise PrimeRuntimeError('Bad response from server')
yield response.content


Expand Down Expand Up @@ -128,7 +131,7 @@ def __init__(
except ConnectionError:
raise
except:
pass
logging.getLogger("PyPrimeMesh").error('Uncontrolled error, continuing execution')

@error_code_handler
@communicator_error_handler
Expand Down Expand Up @@ -185,8 +188,6 @@ def serve(self, model: Model, command: str, *args, **kwargs) -> dict:
)
message = get_response(response, '')
if defaults.print_communicator_stats():
import logging

logging.getLogger("PyPrimeMesh").info(
f'Data Transfer: Received {len(message)} bytes'
)
Expand Down
6 changes: 1 addition & 5 deletions src/ansys/meshing/prime/internals/prime_communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ def run_on_server(self, model, recipe: str) -> dict:
dict
Response from the server.
"""
exec(recipe, globals())
output = '{"Results" : "' + str(return_value) + '"}'
with config.numpy_array_optimization_disabled():
result = json.loads(output)
return result
pass

def close(self):
"""Close session."""
Expand Down
68 changes: 32 additions & 36 deletions src/ansys/meshing/prime/internals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@

import ansys.meshing.prime.internals.config as config
import ansys.meshing.prime.internals.defaults as defaults
import docker

_LOCAL_PORTS = []
_DOCKER_CLIENT = docker.from_env()


def make_unique_container_name(name: str):
Expand Down Expand Up @@ -83,15 +85,13 @@ def get_child_processes(process):
Process IDs of the processes.
"""
children = []
cmd = subprocess.Popen("pgrep -P %d" % process, shell=True, stdout=subprocess.PIPE)
cmd = subprocess.Popen(['pgrep', '-P', str(process)], stdout=subprocess.PIPE)
out = cmd.stdout.read().decode("utf-8")
cmd.wait()
for pid in out.split("\n")[:1]:
if pid.strip() == '':
break
ps_cmd = subprocess.Popen(
"ps -o cmd= {}".format(int(pid)), stdout=subprocess.PIPE, shell=True
)
ps_cmd = subprocess.Popen(['ps', '-o', 'cmd=', str(int(pid))], stdout=subprocess.PIPE)
ps_out = ps_cmd.stdout.read().decode("utf-8")
ps_cmd.wait()
cmd_name = ps_out.split()[0]
Expand Down Expand Up @@ -238,42 +238,38 @@ def launch_prime_github_container(
raise ValueError('Licensing information to launch container not found')
if version is None:
version = os.environ.get('PYPRIMEMESH_IMAGE_TAG', 'latest')
docker_command = [
'docker',
'run',
'--shm-size=4g',
'-d',
'--rm',
'--name',
f'{name}',
'-p',
f'{port}:{port}',
'-v',
f'{mount_host}:{mount_image}',
'-e',
f'ANSYSLMD_LICENSE_FILE={license_file}',
]
graphics_port = int(os.environ.get('PRIME_GRAPHICS_PORT', '0'))
if graphics_port > 0:
print(f'PyPrimeMesh: using Prime graphics port {graphics_port}')
docker_command += ['-p', f'{graphics_port}:{graphics_port}']
prime_arguments = [
f'{image_name}:{version}',
'--port',
f'{port}',
]
subprocess.run(docker_command + prime_arguments, stdout=subprocess.DEVNULL)


def stop_prime_github_container(name):
"""Stop a running container.

ports = {f'{port}/tcp': port}

volumes = {mount_host: {'bind': mount_image, 'mode': 'rw'}}

environment = {'ANSYSLMD_LICENSE_FILE': license_file}
container = _DOCKER_CLIENT.containers.run(
image=f'{image_name}:{version}',
name=name,
detach=True,
shm_size='4g',
ports=ports,
volumes=volumes,
environment=environment,
remove=True,
command=['--port', str(port)],
)


def stop_prime_github_container(container_name: str):
"""Stop a container.

Parameters
----------
name : str
Name of the container.
container : str
Name of the container to stop.
"""
subprocess.run(['docker', 'stop', f'{name}'], stdout=subprocess.DEVNULL)
try:
cont = _DOCKER_CLIENT.containers.get(container_name)
cont.stop()
except docker.errors.NotFound:
pass


@contextmanager
Expand Down
Loading