Skip to content

Commit

Permalink
Cache gazebo executable path
Browse files Browse the repository at this point in the history
  • Loading branch information
flferretti committed May 17, 2024
1 parent 11189dc commit 8d6da11
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/rod/utils/gazebo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@


class GazeboHelper:
@staticmethod
def get_gazebo_executable() -> pathlib.Path:
_cached_executable: pathlib.Path = None

@classmethod
def get_gazebo_executable(cls) -> pathlib.Path:
if cls._cached_executable is not None:
return cls._cached_executable

gz = shutil.which("gz")
ign = shutil.which("ign")

Expand All @@ -25,13 +30,17 @@ def get_gazebo_executable() -> pathlib.Path:
raise TypeError(executable)

# Check if the sdf plugin of the simulator is installed
cp = subprocess.run([executable, "sdf", "--help"], capture_output=True)

if cp.returncode != 0:
try:
subprocess.run(
[executable, "sdf", "--help"], check=True, capture_output=True
)
except subprocess.CalledProcessError as e:
msg = f"Failed to find 'sdf' command part of {executable} installation"
raise RuntimeError(msg)
raise RuntimeError(msg) from e

cls._cached_executable = executable

return executable
return cls._cached_executable

@staticmethod
def has_gazebo() -> bool:
Expand Down Expand Up @@ -94,17 +103,19 @@ def process_model_description_with_sdformat(
fp.write(model_description_string)
fp.close()

cp = subprocess.run(
[str(gazebo_executable), "sdf", "-p", fp.name],
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
)

if cp.returncode != 0:
print(cp.stdout)
raise RuntimeError("Failed to process the input with sdformat")
try:
cp = subprocess.run(
[str(gazebo_executable), "sdf", "-p", fp.name],
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as e:
if cp.returncode != 0:
print(cp.stdout)
raise RuntimeError(
"Failed to process the input with sdformat"
) from e

# Get the resulting SDF string
sdf_string = cp.stdout
Expand Down

0 comments on commit 8d6da11

Please sign in to comment.