utils.py
Go to the documentation of this file.
00001 
00002 import os
00003 import re
00004 from optparse import OptionParser
00005 import rospkg
00006 
00007 ##################
00008 # Author
00009 ##################
00010 
00011 def author_name():
00012     """
00013     Utility to compute logged in user name
00014     
00015     :returns: name of current user, ``str``
00016     """
00017     import getpass
00018     name = getpass.getuser()
00019     try:
00020         import pwd
00021         login = name
00022         name = pwd.getpwnam(login)[4]
00023         name = ''.join(name.split(',')) # strip commas
00024         # in case pwnam is not set
00025         if not name:
00026             name = login
00027     except:
00028         #pwd failed
00029         pass
00030     if type(name) == str:
00031         name = name.decode('utf-8')
00032     return name
00033 
00034 ##################
00035 # Templates
00036 ##################
00037 
00038 # Finds and reads one of the templates.
00039 def read_template(tmplf):
00040     f = open(tmplf, 'r')
00041     try:
00042         t = f.read()
00043     finally:
00044         f.close()
00045     return t
00046 
00047 # This inserts the labelled variables into the template wherever the corresponding
00048 # %package, %brief, %description %manifest_depends and %cmake_depends is found.
00049 def instantiate_template(template, package, description, author, package_depends, cmake_depends):
00050     return template%locals()
00051 
00052 ##################
00053 # Names
00054 ##################
00055 
00056 # These routines are originally from roslib
00057 # Todo: might be nice if they were in rospkg, not here.
00058 
00059 BASE_RESOURCE_NAME_LEGAL_CHARS_P = re.compile('^[A-Za-z][\w_]*$') #ascii char followed by (alphanumeric, _)
00060 def is_legal_resource_base_name(name):
00061     """
00062     Validates that name is a legal resource base name. A base name has
00063     no package context, e.g. "String".
00064     """
00065     # resource names can be unicode due to filesystem
00066     if name is None:
00067         return False
00068     m = BASE_RESOURCE_NAME_LEGAL_CHARS_P.match(name)
00069     return m is not None and m.group(0) == name
00070 
00071 ##################
00072 # Argument Parser
00073 ##################
00074 
00075 def parse_arguments(extra_depends=[]):
00076         """
00077         Parse the command line arguments - in format <package-name> [dependencies]".
00078 
00079         This will need to upgrade from optparse to argparse post python 2.7 
00080         """
00081         parser = OptionParser(usage="usage: %prog <package-name> [dependencies...]")
00082         options, args = parser.parse_args()
00083         if not args:
00084                 parser.error("You must specify a package name and optionally also list package dependencies")
00085         package = args[0]
00086         if type(package) == str:
00087                 package = package.decode('utf-8')
00088         depends = args[1:] + extra_depends
00089 
00090         if not is_legal_resource_base_name(package):
00091                 parser.error("illegal package name [%s]\n\nNames must start with a letter and contain only alphanumeric characters\nand underscores."%package)
00092         if os.path.exists(os.path.abspath(package)):
00093                 parser.error("%s already exists, aborting"%package)
00094 
00095         return (package,depends)


winros_create_pkg
Author(s): Daniel Stonier
autogenerated on Mon Oct 6 2014 12:28:59