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


rosjava_build_tools
Author(s): Daniel Stonier
autogenerated on Wed Sep 2 2015 11:34:26