From ad7fdbbd5b2c6bfcc4ddc35ea19c891657fa4a51 Mon Sep 17 00:00:00 2001 From: Martin Pecka Date: Fri, 30 Dec 2022 19:58:10 +0100 Subject: [PATCH] Allow specifying custom rosdep paths. --- src/catkin_lint/environment.py | 6 ++++-- src/catkin_lint/main.py | 18 ++++++++++++++++- src/catkin_lint/ros.py | 37 +++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/catkin_lint/environment.py b/src/catkin_lint/environment.py index b233d77..3c1e3d6 100644 --- a/src/catkin_lint/environment.py +++ b/src/catkin_lint/environment.py @@ -120,7 +120,8 @@ class PackageType: class CatkinEnvironment(object): - def __init__(self, os_env=None, use_rosdep=True, use_rosdistro=True, use_cache=True, quiet=False): + def __init__(self, os_env=None, use_rosdep=True, use_rosdistro=True, use_cache=True, quiet=False, + rosdep_sources_list_dir=None, rosdep_init=False, rosdep_sources_cache_dir=None, rosdep_update=False): self.package_path_order = [] self.searched_paths = {} self.known_catkin_pkgs = set() @@ -135,7 +136,8 @@ def __init__(self, os_env=None, use_rosdep=True, use_rosdistro=True, use_cache=T if use_rosdep: try: gc.disable() - self.rosdep = get_rosdep(quiet=self.quiet) + self.rosdep = get_rosdep(quiet=self.quiet, sources_list_dir=rosdep_sources_list_dir, init=rosdep_init, + sources_cache_dir=rosdep_sources_cache_dir, update=rosdep_update) except Exception as err: if not self.quiet: sys.stderr.write("catkin_lint: cannot load rosdep database: %s\n" % str(err)) diff --git a/src/catkin_lint/main.py b/src/catkin_lint/main.py index 231ff2f..fdc9f91 100644 --- a/src/catkin_lint/main.py +++ b/src/catkin_lint/main.py @@ -96,6 +96,10 @@ def prepare_arguments(parser): parser.add_argument("--disable-cache", action="store_true", help=argparse.SUPPRESS) parser.add_argument("--dump-cache", action="store_true", help=argparse.SUPPRESS) parser.add_argument("--list-check-ids", action="store_true", help=argparse.SUPPRESS) + parser.add_argument("--rosdep-sources-list-dir", metavar="ROSDEP_SOURCES_LIST_DIR", help="override the default rosdep sources list directory location.") + parser.add_argument("--rosdep-init", action="store_true", default=None, help="initialize rosdep sources list if it is not already initialized (use --rosdep-sources-list-dir to specify a writable location)") + parser.add_argument("--rosdep-sources-cache-dir", metavar="ROSDEP_SOURCES_CACHE_DIR", help="override the default rosdep sources cache directory location.") + parser.add_argument("--rosdep-update", action="store_true", default=None, help="update rosdep cache before checking for valid packages") return parser @@ -184,6 +188,14 @@ def run_linter(args): config["catkin_lint"]["severity_level"] = str(args.severity_level) if args.resolve_env is not None: config["catkin_lint"]["resolve_env"] = "yes" if args.resolve_env else "no" + if args.rosdep_sources_list_dir: + config["catkin_lint"]["rosdep_sources_list_dir"] = args.rosdep_sources_list_dir + if args.rosdep_init is not None: + config["catkin_lint"]["rosdep_init"] = "yes" if args.rosdep_init else "no" + if args.rosdep_sources_cache_dir: + config["catkin_lint"]["rosdep_sources_cache_dir"] = args.rosdep_sources_cache_dir + if args.rosdep_update is not None: + config["catkin_lint"]["rosdep_update"] = "yes" if args.rosdep_update else "no" for config_file in args.config: try: @@ -217,7 +229,11 @@ def run_linter(args): os_env=os.environ if config["catkin_lint"].getboolean("resolve_env", False) else None, use_rosdistro=not config["catkin_lint"].getboolean("offline", False), use_cache=not config["catkin_lint"].getboolean("disable_cache", False), - quiet=quiet + quiet=quiet, + rosdep_sources_list_dir=config["catkin_lint"].get("rosdep_sources_list_dir", None), + rosdep_init=config["catkin_lint"].getboolean("rosdep_init", False), + rosdep_sources_cache_dir=config["catkin_lint"].get("rosdep_sources_cache_dir", None), + rosdep_update=config["catkin_lint"].getboolean("rosdep_update", False), ) if not args.path and not args.pkg: if os.path.isfile("package.xml"): diff --git a/src/catkin_lint/ros.py b/src/catkin_lint/ros.py index f31ecdf..66dac29 100644 --- a/src/catkin_lint/ros.py +++ b/src/catkin_lint/ros.py @@ -54,12 +54,43 @@ def has_key(self, name): return self.view is not None and name in self.view.keys() -def get_rosdep(quiet): +def get_rosdep(quiet, sources_list_dir=None, init=False, sources_cache_dir=None, update=False): from rosdep2.lookup import RosdepLookup from rosdep2.rospkg_loader import DEFAULT_VIEW_KEY - from rosdep2.sources_list import SourcesListLoader + from rosdep2.sources_list import SourcesListLoader, update_sources_list, SOURCE_PATH_ENV,\ + get_default_sources_list_file + from rosdep2.main import command_init + from rosdep2.rosdistrohelper import PreRep137Warning + + if sources_list_dir is not None: + if not os.path.exists(sources_list_dir): + os.makedirs(sources_list_dir) + os.environ[SOURCE_PATH_ENV] = sources_list_dir + + if init and not os.path.exists(get_default_sources_list_file()): + # command_init() writes to stdout, so we silence it + try: + # Python 2 + from StringIO import StringIO + except ImportError: + # Python 3 + from io import StringIO + stdout = sys.stdout + sys.stdout = StringIO() + try: + command_init(None) + finally: + sys.stdout = stdout + + if update: + import warnings + warnings.filterwarnings('ignore', category=PreRep137Warning) + update_sources_list(sources_list_dir=sources_list_dir, sources_cache_dir=sources_cache_dir, + ros_distro=os.environ.get("ROS_DISTRO", None), skip_eol_distros=True, quiet=True) + warnings.filterwarnings('always', category=PreRep137Warning) + dummy = DummyRospkg() - sources_loader = SourcesListLoader.create_default() + sources_loader = SourcesListLoader.create_default(sources_cache_dir=sources_cache_dir) lookup = RosdepLookup.create_from_rospkg(rospack=dummy, rosstack=dummy, sources_loader=sources_loader) return Rosdep(view=lookup.get_rosdep_view(DEFAULT_VIEW_KEY), quiet=quiet)