diff --git a/README.md b/README.md index fa01f20..5720b22 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Install this package in develop mode ```console python3 -m venv .venv source .venv/bin/activate -pip install -e . +pip install -e .[dev] ``` Test this package diff --git a/pyproject.toml b/pyproject.toml index 0fea0bb..4323e79 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,14 @@ classifiers = [ requires-python = ">=3.8" dynamic = ["dependencies","version"] +[project.optional-dependencies] +dev = [ + "catkin_pkg", + "empy==3.3.4", + "lark", + "flake8" +] + [tool.setuptools.dynamic] # Dynamically read requirements version = {attr = "nanosaur.__init__.__version__"} diff --git a/requirements.txt b/requirements.txt index 63f40a0..16702c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ pyyaml pexpect requests -catkin_pkg python-on-whales jetson-stats \ No newline at end of file diff --git a/src/nanosaur/installation.py b/src/nanosaur/installation.py index 6e539d1..0fa4b5e 100644 --- a/src/nanosaur/installation.py +++ b/src/nanosaur/installation.py @@ -30,7 +30,7 @@ import subprocess from nanosaur.prompt_colors import TerminalFormatter from nanosaur.utilities import Params, require_sudo_password -from nanosaur.workspace import get_workspace_path, create_workspace +from nanosaur.workspace import get_workspace_path, create_workspace, clean_workspace def download_rosinstall(url, folder_path, file_name): @@ -251,10 +251,16 @@ def install_simulation(platform, params: Params, args, password=None): def update(platform, params: Params, args, password=None): device_type = "robot" if platform['Machine'] == 'jetson' else "desktop" print(TerminalFormatter.color_text(f"Nanosaur updating on {device_type}", bold=True)) + # Check if the workspace exists workspace_path = get_workspace_path(params['nanosaur_workspace_name']) if workspace_path is None: print(TerminalFormatter.color_text(f"There are no {params['nanosaur_workspace_name']} in this device!", color='red')) return False + # Clean workspace if force + if args.force: + print(TerminalFormatter.color_text("- Force update", bold=True)) + # Check if the workspace exists + clean_workspace(workspace_path) # Build environment print(TerminalFormatter.color_text(f"- Build workspace {workspace_path}", bold=True)) if not run_colcon_build(workspace_path): diff --git a/src/nanosaur/workspace.py b/src/nanosaur/workspace.py index e846039..f189df8 100644 --- a/src/nanosaur/workspace.py +++ b/src/nanosaur/workspace.py @@ -27,6 +27,32 @@ from nanosaur.prompt_colors import TerminalFormatter +def clean_workspace(nanosaur_ws_name): + """ + Checks if a workspace folder exists in the user's home directory. + :param folder_name: The name of the workspace folder to check. + :return: The full path to the workspace if it exists, or None if it doesn't. + """ + # Check if the script is running with sudo + if os.geteuid() == 0: + # Get the original user's home directory + user_home_dir = os.path.expanduser(f"~{os.getenv('SUDO_USER')}") + else: + # 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 + workspace_path = os.path.join(user_home_dir, nanosaur_ws_name) + + # Check if the workspace folder exists + if os.path.exists(workspace_path) and os.path.isdir(workspace_path): + print(TerminalFormatter.color_text(f"Workspace '{workspace_path}' exists. Cleaning build, install and log folders", color='yellow')) + os.system(f"sudo rm -rf {workspace_path}/build {workspace_path}/install {workspace_path}/log") + print(TerminalFormatter.color_text(f"Workspace '{workspace_path}' cleaned up.",color='green')) + else: + print(TerminalFormatter.color_text(f"Folder '{workspace_path}' does not exist.", color='yellow')) + def get_workspace_path(nanosaur_ws_name): """ Checks if a workspace folder exists in the user's home directory. @@ -69,15 +95,9 @@ def create_workspace(nanosaur_ws_name): # Check if folder exists, if not, create it if not os.path.exists(workspace_path_src): os.makedirs(workspace_path_src) - print( - TerminalFormatter.color_text( - f"Folder '{workspace_path_src}' created.", - color='green')) + print(TerminalFormatter.color_text(f"Folder '{workspace_path_src}' created.", color='green')) else: - print( - TerminalFormatter.color_text( - f"Folder '{workspace_path_src}' already exists.", - color='yellow')) + print(TerminalFormatter.color_text(f"Folder '{workspace_path_src}' already exists.", color='yellow')) return workspace_path # EOF