Created
October 16, 2015 05:08
-
-
Save vijaravind/47701b639ccf01e67014 to your computer and use it in GitHub Desktop.
Python script to manage ROS environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Script to manage ROS environment. | |
""" | |
import os | |
import sys | |
import glob | |
HOME_DIR = os.environ['HOME'] | |
BASH_HASHBANG = '#!/bin/bash' | |
ROS_ROOT_SETUP = '/opt/ros/indigo/setup.bash' | |
ROS_ENV_FILE_PATH = os.path.join(HOME_DIR, '.bashrc.d', 'ros-env') | |
# folders to search for catkin workspace specified relative to $HOME folder | |
LOOKUP_DIRS = [] | |
def get_catkin_dirs(): | |
lookup_dirs = [HOME_DIR] + LOOKUP_DIRS | |
lookup_full_paths = map(lambda x:os.path.join(HOME_DIR, x), lookup_dirs) | |
all_paths = map(lambda x:glob.glob(os.path.join(x, '*')), lookup_full_paths) | |
search_paths = filter(lambda x:os.path.isdir(x), sum(all_paths, list())) | |
catkin_dirs = filter(lambda x:os.path.isfile( | |
os.path.join(x, '.catkin_workspace')), | |
search_paths) | |
return sorted(catkin_dirs) | |
def main(): | |
# get all the catkin directories in the lookup path | |
catkin_dirs = get_catkin_dirs() | |
# ask for user choice | |
for idx, item in enumerate(catkin_dirs): | |
print("{0}\t{1}".format(idx, item)) | |
raw_selection = raw_input("enter the numbers (space separated): ") | |
selection = map(int, raw_selection.split()) | |
catkin_selected = map(lambda x:catkin_dirs[x], selection) | |
catkin_setup = map(lambda x:os.path.join(x, 'devel', 'setup.bash'), | |
catkin_selected) | |
# make the `source` commandes | |
source_cmds = map(lambda x:"source {}".format(x), | |
[ROS_ROOT_SETUP] + catkin_setup) | |
# write to ros-env file | |
out_cmds = "\n".join([BASH_HASHBANG] + source_cmds) + "\n" | |
print("\noutput commands:") | |
print(out_cmds) | |
ros_env_file = open(ROS_ENV_FILE_PATH, 'w') | |
ros_env_file.write(out_cmds) | |
print("Run `source $HOME/.bashrc` to reflect the changes.\n") | |
if __name__ == '__main__': | |
try: | |
main() | |
except KeyboardInterrupt: | |
sys.exit(0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment