generate_environment_variables.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import os
00004 import argparse
00005 
00006 CATKIN_MARKER_FILE = '.catkin'
00007 
00008 def parse_arguments():
00009     parser = argparse.ArgumentParser(description='Generate environment variables for the rosjava maven environment.')
00010     cmd_group = parser.add_mutually_exclusive_group()
00011     cmd_group.add_argument('-d', '--maven-deployment-repository', action='store_true', help='Return the current devel workspace maven directory.')
00012     cmd_group.add_argument('-r', '--maven-repository', action='store_true', help='The url to the external ros maven repository.')
00013     cmd_group.add_argument('-m', '--maven-path', action='store_true', help='Generate maven path across all chained workspcaes.')
00014     cmd_group.add_argument('-g', '--gradle-user-home', action='store_true', help='Generate the local gradle user home in the current devel workspace (share/gradle).')
00015     args = parser.parse_args()
00016     return args
00017 
00018 def get_workspaces(environ):
00019     '''
00020     Based on CMAKE_PREFIX_PATH return all catkin workspaces.
00021     '''
00022     # get all cmake prefix paths
00023     env_name = 'CMAKE_PREFIX_PATH'
00024     value = environ[env_name] if env_name in environ else ''
00025     paths = [path for path in value.split(os.pathsep) if path]
00026     # remove non-workspace paths
00027     workspaces = [path.replace(' ', '\ ') for path in paths if os.path.isfile(os.path.join(path, CATKIN_MARKER_FILE))]
00028     return workspaces
00029 
00030 def get_environment_variable(environ, key):
00031     var = None
00032     try:
00033         var = environ[key]
00034     except KeyError:
00035         pass
00036     if var == '':
00037         var = None
00038     return var
00039 
00040 if __name__ == '__main__':
00041     args = parse_arguments()
00042     environment_variables = dict(os.environ)
00043     workspaces = get_workspaces(environment_variables)
00044     if args.maven_deployment_repository:
00045         repo = get_environment_variable(environment_variables, 'ROS_MAVEN_DEPLOYMENT_REPOSITORY')
00046         if repo is None:
00047             repo = os.path.join(workspaces[0], 'share', 'maven')
00048         else:
00049             if repo in [os.path.join(w, 'share', 'maven') for w in workspaces]:
00050                 repo = os.path.join(workspaces[0], 'share', 'maven')
00051         print(repo)
00052     elif args.maven_repository:
00053         repo = get_environment_variable(environment_variables, 'ROS_MAVEN_REPOSITORY')
00054         if repo is None:
00055             repo = 'https://github.com/rosjava/rosjava_mvn_repo/raw/master'
00056         print(repo)
00057     elif args.maven_path:
00058         new_maven_paths = [os.path.join(path, 'share', 'maven') for path in workspaces]
00059         maven_paths = get_environment_variable(environment_variables, 'ROS_MAVEN_PATH')
00060         if maven_paths is None:
00061             maven_paths = new_maven_paths
00062         else:
00063             maven_paths = maven_paths.split(os.pathsep)
00064             common_paths = [p for p in maven_paths if p in new_maven_paths]
00065             if common_paths:
00066                 maven_paths = new_maven_paths
00067         print(os.pathsep.join(maven_paths))
00068     elif args.gradle_user_home:
00069         home = get_environment_variable(environment_variables, 'GRADLE_USER_HOME')
00070         if home is None:
00071             home = os.path.join(workspaces[0], 'share', 'gradle')
00072         else:
00073             if home in [os.path.join(w, 'share', 'gradle') for w in workspaces]:
00074                 home = os.path.join(workspaces[0], 'share', 'gradle')
00075         print(home)
00076     else:
00077         print("Nothing to see here - please provide one of the valid command switches.")


rosjava_build_tools
Author(s): Daniel Stonier
autogenerated on Sat Jun 8 2019 19:54:56