1# Software License Agreement (BSD License)
2#
3# Copyright (c) 2010, Willow Garage, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10#  * Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12#  * Redistributions in binary form must reproduce the above
13#    copyright notice, this list of conditions and the following
14#    disclaimer in the documentation and/or other materials provided
15#    with the distribution.
16#  * Neither the name of Willow Garage, Inc. nor the names of its
17#    contributors may be used to endorse or promote products derived
18#    from this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31# POSSIBILITY OF SUCH DAMAGE.
32
33
34import os
35import subprocess
36from wstool.multiproject_cmd import cmd_persist_config as multipersist
37from rosinstall import setupfiles
38from wstool.helpers import ROSINSTALL_FILENAME
39from rosinstall.helpers import is_path_ros
40
41
42def cmd_persist_config(config, config_filename=ROSINSTALL_FILENAME, header=''):
43    ## Save .rosinstall
44    header = (header or '') + """\
45# IT IS UNLIKELY YOU WANT TO EDIT THIS FILE BY HAND,
46# UNLESS FOR REMOVING ENTRIES.
47# IF YOU WANT TO CHANGE THE ROS ENVIRONMENT VARIABLES
48# USE THE rosinstall TOOL INSTEAD.
49# IF YOU CHANGE IT, USE rosinstall FOR THE CHANGES TO TAKE EFFECT
50"""
51    multipersist(config, config_filename, header)
52
53
54def _ros_requires_boostrap(config):
55    """
56    Tests whether workspace contains a core ros stack, to decide
57    whether to rosmake
58
59    :param config: workspace config object
60    """
61    for entry in config.get_source():
62        if is_path_ros(os.path.join(config.get_base_path(), entry.get_local_name())):
63            # we assume that if any of the elements we installed came
64            # from a VCS source, a bootsrap might be useful
65            if entry.get_scmtype() is not None:
66                return True
67    return False
68
69
70def cmd_maybe_refresh_ros_files(config):
71    """
72    Regenerates setup.* files if they exist already
73
74    :param config: workspace config object
75    """
76    if (os.path.isfile(os.path.join(config.get_base_path(), 'setup.sh'))):
77        print("Overwriting setup.sh, setup.bash, and setup.zsh in %s" %
78              config.get_base_path())
79        setupfiles.generate_setup(config, no_ros_allowed=True)
80
81
82def cmd_generate_ros_files(config, path, nobuild=False, rosdep_yes=False, catkin=False, catkinpp=None, no_ros_allowed=False):
83    """
84    Generates ROS specific setup files
85
86    :param nobuild: Unless True, invokes rosmake to build all packages if core ROS stack is detected
87    :param rosdep_yes: If True, adds --rosdep-yes to rosmake command
88    :param catkin: if true, generates catkin(fuerte) CMakeLists.txt instead of invoking rosmake
89    :param catkinpp: Prefix path for catkin if generating for catkin
90    :param no_ros_allowed: if true, does not look for a core ros stack
91    """
92
93    # Catkin must be enabled if catkinpp is set
94    if catkinpp is not None:
95        catkin = True
96
97    ## bootstrap the build if installing ros
98    if catkin:
99        setupfiles.generate_catkin_cmake(path, catkinpp)
100
101    else:  # DRY install case
102        ## Generate setup.sh and save
103        print("(Over-)Writing setup.sh, setup.bash, and setup.zsh in %s" %
104              config.get_base_path())
105        setupfiles.generate_setup(config, no_ros_allowed)
106
107        if _ros_requires_boostrap(config) and not nobuild:
108            print("Bootstrapping ROS build")
109            rosdep_yes_insert = ""
110            if rosdep_yes:
111                rosdep_yes_insert = " --rosdep-yes"
112            ros_comm_insert = ""
113            if 'ros_comm' in [os.path.basename(tree.get_path()) for tree in config.get_config_elements()]:
114                print("Detected ros_comm bootstrapping it too.")
115                ros_comm_insert = " ros_comm"
116            cmd = ("source %s && rosmake ros%s --rosdep-install%s" %
117                   (os.path.join(path, 'setup.sh'),
118                    ros_comm_insert,
119                    rosdep_yes_insert))
120            subprocess.check_call(cmd, shell=True, executable='/bin/bash')
121