create_package.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 argparse
00010 import subprocess
00011 import catkin_pkg
00012 from catkin_pkg.package_templates import create_package_xml, PackageTemplate
00013 
00014 # local imports
00015 import utils
00016 import console
00017 
00018 ##############################################################################
00019 # Methods
00020 ##############################################################################
00021 
00022 
00023 def parse_arguments():
00024     argv = sys.argv[1:]
00025     parser = argparse.ArgumentParser(
00026         description='Creates a new rosjava/android repository based on catkin and gradle. \n\nNote that the path you provide will become the maven group for your repo.\n')
00027     parser.add_argument('path', nargs='?', default=os.getcwd(), help='path to the repository you wish to create (must not exist beforehand).')
00028     parser.add_argument('dependencies',
00029                         nargs='*',
00030                         help='Dependency list')
00031     parser.add_argument('-l', '--license',
00032                         action='append',
00033                         default=["Apache 2.0"],
00034                         help='Name for License, (e.g. BSD, MIT, GPLv3...)[BSD]')
00035     parser.add_argument('-a', '--author',
00036                         action='append',
00037                         help='A single author, may be used multiple times')
00038     parser.add_argument('-m', '--maintainer',
00039                         action='append',
00040                         help='A single maintainer, may be used multiple times')
00041     parser.add_argument('-V', '--pkg_version',
00042                         action='store',
00043                         default="0.1.0",
00044                         help='Initial Package version [0.1.0]')
00045     parser.add_argument('-D', '--description',
00046                         action='store',
00047                         help='Description')
00048     args = parser.parse_args(argv)
00049 #    if not args.author:
00050 #        args.author = []
00051 #        args.author.append(utils.author_name)
00052 #    if not args.maintainer:
00053 #        args.maintainer = []
00054 #        args.maintainer.append(catkin_pkg.package.Person(utils.author_name))
00055 #    else:
00056 #        args.maintainer = []
00057 #        args.maintainer.append(catkin_pkg.package.Person(utils.author_name))
00058     return args
00059 
00060 
00061 # Finds and reads one of the templates.
00062 def read_template(tmplf):
00063     f = open(tmplf, 'r')
00064     try:
00065         t = f.read()
00066     finally:
00067         f.close()
00068     return t
00069 
00070 
00071 # This inserts the labelled variables into the template wherever the corresponding
00072 # %package, %brief, %description and %depends is found.
00073 def instantiate_template(template, repo_name, author):
00074     return template % locals()
00075 
00076 
00077 def get_templates(template_directory):
00078     template_dir = os.path.join(os.path.dirname(__file__), 'templates', template_directory)
00079     templates = {}
00080     templates['CMakeLists.txt'] = read_template(os.path.join(template_dir, 'CMakeLists.txt.in'))
00081     templates['build.gradle'] = read_template(os.path.join(template_dir, 'build.gradle.in'))
00082     templates['settings.gradle'] = read_template(os.path.join(template_dir, 'settings.gradle'))
00083     return templates
00084 
00085 
00086 def populate_repo(repo_path, package_type):
00087     author = utils.author_name()
00088     repo_name = os.path.basename(repo_path)
00089     templates = get_templates(package_type)
00090     for filename, template in templates.iteritems():
00091         contents = instantiate_template(template, repo_name, author)
00092         try:
00093             p = os.path.abspath(os.path.join(repo_path, filename))
00094             f = open(p, 'w')
00095             f.write(contents)
00096             console.pretty_print("Created repo file: ", console.cyan)
00097             console.pretty_println("%s" % p, console.yellow)
00098         finally:
00099             f.close()
00100 
00101 
00102 def create_gradle_wrapper(repo_path):
00103     gradle_binary = os.path.join(os.path.dirname(__file__), 'gradle', 'gradlew')
00104     cmd = [gradle_binary, '-p', repo_path, 'wrapper']
00105     console.pretty_print("Creating gradle wrapper: ", console.cyan)
00106     console.pretty_println("%s" % ' '.join(cmd), console.yellow)
00107     try:
00108         subprocess.check_call(cmd)
00109     except subprocess.CalledProcessError:
00110         raise subprocess.CalledProcessError("failed to create the gradle wrapper.")
00111 
00112 
00113 def create_catkin_package_files(package_name, package_path, args):
00114     '''
00115       This is almost a direct copy from catkin_create_pkg.
00116     '''
00117     try:
00118         build_depends = []
00119         if 'rosjava_build_tools' not in args.dependencies:
00120             build_depends.append(catkin_pkg.package.Dependency('rosjava_build_tools'))
00121         for depend_name in args.dependencies:
00122             build_depends.append(catkin_pkg.package.Dependency(depend_name))
00123         package_template = PackageTemplate._create_package_template(
00124             package_name=package_name,
00125             description=args.description,
00126             licenses=args.license or [],
00127             maintainer_names=args.maintainer,
00128             author_names=args.author,
00129             version=args.pkg_version,
00130             catkin_deps=[],
00131             system_deps=[],
00132             boost_comps=None)
00133         package_template.exports = []
00134         package_template.build_depends = build_depends
00135         distro_version = utils.distro_version()
00136         package_xml = create_package_xml(package_template=package_template, rosdistro=distro_version)
00137         try:
00138             filename = os.path.join(package_path, 'package.xml')
00139             f = open(filename, 'w')
00140             f.write(package_xml)
00141             console.pretty_print('Created repo file: ', console.cyan)
00142             console.pretty_println('%s' % filename, console.yellow)
00143         finally:
00144             f.close()
00145     except Exception:
00146         raise
00147 
00148 ##############################################################################
00149 # Methods acting on classes
00150 ##############################################################################
00151 
00152 
00153 def init_package(package_type):
00154     args = parse_arguments()
00155     try:
00156         repo_path = utils.validate_path(args.path)
00157         repo_name = os.path.basename(os.path.normpath(repo_path)).lower()
00158         populate_repo(repo_path, package_type)
00159         create_catkin_package_files(repo_name, repo_path, args)
00160         create_gradle_wrapper(repo_path)
00161     except Exception:
00162         raise
00163 
00164 
00165 def init_rosjava_package():
00166     init_package('rosjava_package')
00167 
00168 
00169 def init_android_package():
00170     init_package('android_package')


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