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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
# modification, is not permitted without the express permission
# of Clearpath Robotics.

from datetime import datetime, UTC

from clearpath_config.common.types.discovery import Discovery
from clearpath_generator_common.bash.writer import BashWriter
from clearpath_generator_common.common import BaseGenerator, BashFile
Expand All @@ -48,18 +50,34 @@ def generate_setup_bash(self) -> None:
clearpath_setup_bash = BashFile(filename='setup.bash', path=self.setup_path)
bash_writer = BashWriter(clearpath_setup_bash)

workspaces = self.clearpath_config.system.workspaces
bash_writer.add_comment(f'Bash setup generated at {datetime.now(UTC)}')

# Additional ROS sources
sources = self.clearpath_config.system.bash.additional_sources
envs = self.clearpath_config.system.bash.additional_envars
if len(sources) > 0 or len(envs) > 0:
bash_writer.add_comment('Additional bash configuration from robot.yaml')
for s in sources:
bash_writer.add_source(BashFile(filename=s))

for e in envs:
bash_writer.add_export(e, envs[e])

# Source core ROS
bash_writer.add_comment('Core ROS setup')
ros_setup_bash = BashFile(filename='setup.bash', path=ROS_DISTRO_PATH)
bash_writer.add_source(ros_setup_bash)

# Additional workspaces
for workspace in workspaces:
bash_writer.add_source(
BashFile(filename='setup.bash', path=workspace.strip('setup.bash')))
workspaces = self.clearpath_config.system.workspaces
if len(workspaces) > 0:
bash_writer.add_comment('Additional workspaces')
for workspace in workspaces:
bash_writer.add_source(
BashFile(filename='setup.bash', path=workspace.strip('setup.bash')))

# ROS_DOMAIN_ID
bash_writer.add_comment('ROS configuration')
domain_id = self.clearpath_config.system.domain_id
bash_writer.add_export('ROS_DOMAIN_ID', domain_id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ def write(self, string, indent_level=0):
self.file.write(f'{self.tab * indent_level}{string}\n')

def add_export(self, envar: str, value, indent_level=0):
self.write(f'{self.tab * indent_level}export {envar}={value}')
value = str(value)
if (
value.startswith('"') and value.endswith('"')
) or (
value.startswith("'") and value.endswith("'")
):
self.write(f'{self.tab * indent_level}export {envar}={value}')
else:
self.write(f'{self.tab * indent_level}export {envar}="{value}"')

def add_unset(self, envar: str, indent_level=0):
self.write(f'{self.tab * indent_level}unset {envar}')
Expand All @@ -55,5 +63,8 @@ def add_source(self, bash_file: BashFile, indent_level=0):
def add_echo(self, msg: str, indent_level=0):
self.write(f'{self.tab * indent_level}echo "{msg}"')

def add_comment(self, msg: str, indent_level=0):
self.write(f'{self.tab * indent_level}# {msg}')

def close(self):
self.file.close()
14 changes: 12 additions & 2 deletions clearpath_generator_common/clearpath_generator_common/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,18 @@ def __add__(self, other):


class BashFile():
"""
A bash file we can source.

:param filename: The name of the file or the absolute path to the file
:param path: The absolute or relative directory containing the file if filename
is not a complete path. package must be specified if path is not absolute
:param package: The ROS package that contains path if it is not absolute
"""

def __init__(self,
filename: str,
path: str,
path: str = None,
package: Package = None,
) -> None:
self.package = package
Expand All @@ -310,8 +318,10 @@ def full_path(self):
if self.package:
return os.path.join(
get_package_share_directory(self.package.name), self.path, self.file)
else:
elif self.path:
return os.path.join(self.path, self.file)
else:
return self.file


class BaseGenerator():
Expand Down
Loading