Skip to content

Commit

Permalink
Remove use of vcs import and now use a local importer
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonghi committed Jan 27, 2025
1 parent f2437d2 commit 7e23c14
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/nanosaur/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,17 @@ def main():
parser.add_argument('--mode', type=str, help="Specify the mode of operation")
if ros2_installed is not None:
parser.add_argument('--default-debug', '-dd', type=str, choices=['host', 'docker'], help="Select the debug mode if on host or in docker")

if 'mode' in params and params['mode'] in ['Raffo']:
help_message = "Set the log level (default: INFO)"
else:
help_message = argparse.SUPPRESS
parser.add_argument(
"--log-level",
type=str,
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="INFO",
help="Set the logging level (default: INFO)",
help=help_message,
)
# Define subcommands
subparsers = parser.add_subparsers(dest='command', help="Available commands")
Expand Down
57 changes: 56 additions & 1 deletion src/nanosaur/ros.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
import signal
import shutil
import logging
import yaml
import urllib.parse
from python_on_whales import docker, DockerException
from nanosaur.prompt_colors import TerminalFormatter
from nanosaur.utilities import get_nanosaur_home
from git import Repo, GitCommandError
from git import Repo, GitCommandError, GitCommandError

# Set up the logger
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -150,7 +152,60 @@ def handle_signal(signum, frame):
print(TerminalFormatter.color_text("Dev script finished", color='green'))


def rosinstall_reader(workspace_path, rosinstall_path, src_folder="src") -> bool:
folder_path = os.path.join(workspace_path, src_folder)
if not os.path.exists(folder_path):
print(TerminalFormatter.color_text(f"Error: Folder {folder_path} does not exist.", color='red'))
return False
# Load the YAML file
with open(rosinstall_path, 'r') as file:
repos = yaml.safe_load(file)
# Iterate over the repositories in the YAML file
for repo in repos:
if git_info := repo.get('git'):
# Fetch the details from the YAML
local_name = git_info.get('local-name')
version = git_info.get('version', 'main') # Default to 'main' if no version is provided
uri = git_info.get('uri')

# Ensure that local_name is always defined
if not local_name:
# Extract the repository name from the URI (the last part of the URL)
parsed_uri = urllib.parse.urlparse(uri)
local_name = os.path.splitext(os.path.basename(parsed_uri.path))[0]
# If the repo does not exist, clone it
repo_path = os.path.join(folder_path, local_name)
print(f"=== {os.path.abspath(local_name)} ({uri}) ===")
if not os.path.exists(repo_path):
print(f"Cloning {repo_path}...")
Repo.clone_from(uri, repo_path, branch=version)
else:
# Open the existing repo and fetch the latest changes
repo_dir = Repo(repo_path)
# Fetch the latest changes
origin = repo_dir.remotes.origin
origin.fetch()
# Checkout the specified version (branch or tag)
try:
repo_dir.git.checkout(version)
# Check if there are any modified files
if modified_files := repo_dir.git.diff('--name-only'):
print(f"\nAlready on '{version}'")
for file in modified_files.splitlines():
print(TerminalFormatter.color_text(f"M\t{file}", color='yellow'))
else:
print(f"\nAlready on '{version}'")
print(f"Your branch is up to date with 'origin/{version}'.")
except GitCommandError:
print(f"Error: Version {version} not found in {local_name}.")
return False
return True

def run_vcs_import(workspace_path, rosinstall_path, src_folder="src") -> bool:
"""
Run the vcs import command to import repositories into a ROS workspace.
NOT USED ANYMORE, now we use the rosinstall_reader function.
"""
try:
# Run the command and stream the output live
process = subprocess.Popen(
Expand Down
4 changes: 2 additions & 2 deletions src/nanosaur/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def update_shared_workspace(force):
print(TerminalFormatter.color_text(f"Failed to download {workspace_type}.rosinstall", color='red'))
if os.path.exists(rosinstall_path):
print(TerminalFormatter.color_text(f"Found rosinstall file: {rosinstall_path}", bold=True))
if not ros.run_vcs_import(nanosaur_home_path, rosinstall_path, src_folder="shared_src"):
if not ros.rosinstall_reader(nanosaur_home_path, rosinstall_path, src_folder="shared_src"):
return False
return True
# Update rosinstall file and run vcs import
Expand All @@ -234,7 +234,7 @@ def update_workspace(params, workspace_type, workspace_name_key, force, skip_ros
# run vcs import to sync the workspace
if os.path.exists(rosinstall_path):
print(TerminalFormatter.color_text(f"Found rosinstall file: {rosinstall_path}", bold=True))
if not ros.run_vcs_import(workspace_path, rosinstall_path):
if not ros.rosinstall_reader(workspace_path, rosinstall_path):
return False
return True

Expand Down

0 comments on commit 7e23c14

Please sign in to comment.