Skip to content

Commit

Permalink
Improve installation with clean workspace if requested
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonghi committed Jan 10, 2025
1 parent a3c68c0 commit 5c7d51b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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__"}
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pyyaml
pexpect
requests
catkin_pkg
python-on-whales
jetson-stats
8 changes: 7 additions & 1 deletion src/nanosaur/installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
36 changes: 28 additions & 8 deletions src/nanosaur/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

0 comments on commit 5c7d51b

Please sign in to comment.