Skip to content

Commit

Permalink
Improve script with multiple workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonghi committed Jan 18, 2025
1 parent 67b14e7 commit e577915
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 123 deletions.
50 changes: 31 additions & 19 deletions src/nanosaur/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@
from nanosaur import workspace
from nanosaur import simulation
from nanosaur import robot
from nanosaur.workspace import get_workspace_path
from nanosaur.utilities import require_sudo_password
from nanosaur.prompt_colors import TerminalFormatter

NANOSAUR_CONFIG_FILE = 'nanosaur.yaml'
NANOSAUR_CONFIG_FILE_NAME = 'nanosaur.yaml'
NANOSAUR_HOME_NAME = 'nanosaur'

# Define default parameters
DEFAULT_PARAMS = {
'nanosaur_workspace_name': 'nanosaur_ws',
'nanosaur_home': NANOSAUR_HOME_NAME,
'robot_ws_name': 'robot_ws',
'simulation_ws_name': 'simulation_ws',
'perception_ws_name': 'perception_ws',
'nanosaur_branch': 'nanosaur2',
}

Expand All @@ -60,14 +64,11 @@ def info(platform, params: Params, args):
for key, value in platform.items():
print(f" {key}: {value}")


def install(platform, params: Params, args):
device_type = "robot" if platform['Machine'] == 'jetson' else "desktop"
print(TerminalFormatter.color_text(f"Installing Nanosaur for {device_type}...", color='green'))
if device_type == 'desktop':
simulation.simulation_install(platform, params, args)
elif device_type == 'robot':
print(TerminalFormatter.color_text("Robot installation not supported yet.", color='red'))
def install(platform, params: Params, args, password=None):
if args.developer:
workspace.create_developer_workspace(platform, params, args)
else:
print(TerminalFormatter.color_text("Not implemented yet", color='red'))


def parser_workspace_menu(subparsers: argparse._SubParsersAction) -> argparse.ArgumentParser:
Expand All @@ -78,14 +79,24 @@ def parser_workspace_menu(subparsers: argparse._SubParsersAction) -> argparse.Ar
# Add workspace clean subcommand
parser_workspace_clean = workspace_subparsers.add_parser(
'clean', help="Clean the workspace")
parser_workspace_clean.add_argument(
'workspace', type=str, nargs='?', help="Specify the workspace to clean")
parser_workspace_clean.add_argument(
'--force', action='store_true', help="Force the workspace clean")
parser_workspace_clean.add_argument(
'--all-platforms', '--all', action='store_true', help="Clean all workspaces")
parser_workspace_clean.add_argument(
'--perception', action='store_true', help="Clean the perception workspace")
parser_workspace_clean.set_defaults(func=workspace.clean)
# Add workspace update subcommand
parser_workspace_update = workspace_subparsers.add_parser(
'update', help="Update the workspace")
parser_workspace_update.add_argument(
'workspace', type=str, nargs='?', help="Specify the workspace to clean")
parser_workspace_update.add_argument(
'--force', action='store_true', help="Force the update")
parser_workspace_update.add_argument(
'--all-platforms', '--all', action='store_true', help="Clean all workspaces")
parser_workspace_update.set_defaults(func=workspace.update)
return parser_workspace

Expand Down Expand Up @@ -133,8 +144,7 @@ def parser_swarm_menu(subparsers: argparse._SubParsersAction, params: Params) ->

def main():
# Load the parameters
user_home_dir = os.path.expanduser("~")
params = Params.load(DEFAULT_PARAMS, params_file=f'{user_home_dir}/{NANOSAUR_CONFIG_FILE}')
params = Params.load(DEFAULT_PARAMS, home_folder=NANOSAUR_HOME_NAME, params_file_name=NANOSAUR_CONFIG_FILE_NAME)

# Extract device information with jtop
try:
Expand All @@ -160,24 +170,26 @@ def main():
parser_info.set_defaults(func=info)

# Subcommand: install (hidden if workspace already exists)
if get_workspace_path(params['nanosaur_workspace_name']) is None:
#if get_workspace_path(params['nanosaur_workspace_name']) is None:
if 'developer_mode' not in params and not params['developer_mode']:
parser_install = subparsers.add_parser('install', help="Install the Nanosaur workspace")
else:
parser_install = subparsers.add_parser('install')
# Add simulation install subcommand
parser_install.add_argument('--developer', action='store_true', help="Install developer workspace")
parser_install.add_argument('--developer', '--dev', action='store_true', help="Install developer workspace")
parser_install.add_argument('--force', action='store_true', help="Force the update")
parser_install.add_argument('--all-platforms', action='store_true', help="Install for all platforms")
parser_install.set_defaults(func=install)

# Subcommand: workspace (with a sub-menu for workspace operations)
if get_workspace_path(params['nanosaur_workspace_name']) is not None:
if 'developer_mode' in params and params['developer_mode']:
# Add workspace subcommand
parser_workspace = parser_workspace_menu(subparsers)

# Subcommand: simulation (with a sub-menu for simulation types)
if device_type == 'desktop' and get_workspace_path(params['nanosaur_workspace_name']) is not None:
# Add simulation subcommand
parser_simulation = parser_simulation_menu(subparsers, params)
#if device_type == 'desktop' and get_workspace_path(params['nanosaur_workspace_name']) is not None:
# # Add simulation subcommand
parser_simulation = parser_simulation_menu(subparsers, params)

# Subcommand: robot (with a sub-menu for robot operations)
robot_data = robot.RobotList.get_robot(params)
Expand Down
41 changes: 33 additions & 8 deletions src/nanosaur/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,25 @@
import getpass
from nanosaur.prompt_colors import TerminalFormatter


class Params:

@classmethod
def load(cls, default_params, params_file=None):
def load(cls, default_params, home_folder, params_file_name):
params_file = Params.get_params_file(home_folder, params_file_name)
# Load parameters from YAML file if it exists
if os.path.exists(params_file):
with open(params_file, 'r') as file:
params_dict = yaml.safe_load(file)
else:
params_dict = default_params

return cls(params_dict, params_file)
return cls(params_dict, home_folder, params_file)

def __init__(self, params_dict, params_file=None):
def __init__(self, params_dict, home_folder, params_file_name):
self._params_dict = params_dict
self._default_params = copy.deepcopy(params_dict)
self.params_file = params_file
self.home_folder = home_folder
self.params_file_name = params_file_name
for key, value in params_dict.items():
setattr(self, key, value)

Expand All @@ -73,11 +74,20 @@ def __repr__(self):
return str(self._params_dict)

def save(self):
if self.params_file and self._params_dict != self._default_params:
print(TerminalFormatter.color_text(f"Saving parameters to {self.params_file}", color='yellow'))
with open(self.params_file, 'w') as file:
params_file = Params.get_params_file(self.home_folder, self.params_file_name)
# Save the parameters to the file if they are different from the default
if params_file and self._params_dict != self._default_params:
# Get the current nanosaur's home directory
create_nanosaur_home(self.home_folder)
# Save the parameters to the file
print(TerminalFormatter.color_text(f"Saving parameters to {self.params_file_name}", color='yellow'))
with open(params_file, 'w') as file:
yaml.dump(self._params_dict, file)

@staticmethod
def get_params_file(home_folder, params_file_name):
return os.path.join(get_nanosaur_home(home_folder), params_file_name)

def get(self, key, default=None):
return getattr(self, key, default)

Expand All @@ -90,6 +100,21 @@ def set(self, key, value):
def items(self):
return self._params_dict.items()

def create_nanosaur_home(nanosaur_home):
# Get the current user's home directory
user_home_dir = os.path.expanduser("~")
# Create the full path for the workspace folder in the user's home directory
nanosaur_home_path = os.path.join(user_home_dir, nanosaur_home)
# Check if folder exists, if not, create it
if not os.path.exists(nanosaur_home_path):
os.makedirs(nanosaur_home_path)
print(TerminalFormatter.color_text(f"Folder '{nanosaur_home_path}' created.", color='green'))
return nanosaur_home_path

def get_nanosaur_home(nanosaur_home):
# Get the current user's home directory
user_home_dir = os.path.expanduser("~")
return os.path.join(user_home_dir, nanosaur_home)

def require_sudo(func):
def wrapper(*args, **kwargs):
Expand Down
Loading

0 comments on commit e577915

Please sign in to comment.