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