utils.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # License: Apache 2.0
00004 #   https://raw.github.com/ros-java/rosjava_core/hydro-devel/rosjava_tools/LICENSE
00005 #
00006 
00007 ##############################################################################
00008 # Imports
00009 ##############################################################################
00010 
00011 import os
00012 import sys
00013 import errno
00014 import pwd
00015 import console
00016 
00017 ##############################################################################
00018 # Methods
00019 ##############################################################################
00020 
00021 
00022 def distro_version():
00023     '''
00024       This code is pulled from rosversion, which unfortunately has it buried inside
00025       the script, not in the python module.
00026     '''
00027     if 'ROS_DISTRO' in os.environ:
00028         return os.environ['ROS_DISTRO']
00029     else:
00030         console.error("could not determine the rosdistro")
00031         sys.exit(1)
00032 
00033 
00034 def which(program):
00035     '''
00036       Looks for program in the environment.
00037 
00038       @param program : string name of the program (e.g. 'ls')
00039       @type str
00040       @return absolute pathname of the program or None
00041       @rtype str or None
00042     '''
00043     def is_exe(fpath):
00044         return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
00045 
00046     fpath, unused_fname = os.path.split(program)
00047     if fpath:
00048         if is_exe(program):
00049             return program
00050     else:
00051         for path in os.environ["PATH"].split(os.pathsep):
00052             path = path.strip('"')
00053             exe_file = os.path.join(path, program)
00054             if is_exe(exe_file):
00055                 return exe_file
00056     return None
00057 
00058 
00059 def camel_case(word):
00060     return ''.join(x.capitalize() or '_' for x in word.split('_'))
00061 
00062 
00063 def author_name():
00064     """
00065     Utility to compute logged in user name
00066 
00067     :returns: name of current user, ``str``
00068     """
00069     import getpass
00070     name = getpass.getuser()
00071     try:
00072         login = name
00073         name = pwd.getpwnam(login)[4]
00074         name = ''.join(name.split(','))  # strip commas
00075         # in case pwnam is not set
00076         if not name:
00077             name = login
00078     except:
00079         #pwd failed
00080         pass
00081     if type(name) == str:
00082         name = name.decode('utf-8')
00083     return name
00084 
00085 
00086 def mkdir_p(path):
00087     '''
00088       Enables mkdir -p functionality (until python 3.2 is able to use
00089       the mode argument to do the same).
00090     '''
00091     try:
00092         os.makedirs(path)
00093     except OSError as e:
00094         if e.errno == errno.EEXIST and os.path.isdir(path):
00095             pass
00096         else:
00097             raise
00098 
00099 
00100 def validate_path(path):
00101     '''
00102       Validates that there isn't a gradle project in the specified path.
00103       We use this when creating an android repo/package.
00104 
00105       @param path : path where you wish to create the repository/package.
00106       @type str
00107       @raise RuntimeError
00108       @return path : validated (exists) absolute pathname
00109     '''
00110     if not os.path.isabs(path):
00111         absolute_path = os.path.join(os.getcwd(), path)
00112     else:
00113         absolute_path = path
00114     if not os.path.exists(absolute_path):
00115         os.mkdir(absolute_path)
00116     else:
00117         if os.path.isfile(os.path.join(path, 'build.gradle')):
00118             raise ValueError("Error: a gradle project already resides in this location [%s]" % absolute_path)
00119     return absolute_path
 All Classes Namespaces Files Functions Variables Typedefs Enumerations


rosjava_tools
Author(s): Daniel Stonier
autogenerated on Fri Oct 4 2013 10:34:26