# =============================================================================
# COPYRIGHT 2013 Brain Corporation.
# License under MIT license (see LICENSE file)
# =============================================================================

import os
from requirement import RequirementException
import subprocess


def install(robustus, requirement_specifier, rob_file, ignore_index):
    rosdep = os.path.join(robustus.env, 'bin/rosdep')
    rosinstall_generator = os.path.join(robustus.env, 'bin/rosinstall_generator')
    wstool = os.path.join(robustus.env, 'bin/wstool')
    if not os.path.isfile(rosinstall_generator)\
        or not os.path.isfile(rosdep)\
        or not os.path.isfile(wstool):
        raise RequirementException('To install ros you need rosinstall_generator, rosdep, etc.\n'
                                   'Here is the full list of dependencies ROS requires:\n'
                                   '    rosinstall==0.6.30\n'
                                   '    rosdep==0.10.23\n'
                                   '    rosinstall_generator==0.1.4\n'
                                   '    wstool==0.0.4')

    # rosdep may also be initialized resulting into failure, that's ok
    subprocess.call(rosdep + ' init', shell=True)

    # update ros dependencies
    retcode = subprocess.call(rosdep + ' update', shell=True)
    if retcode != 0:
        raise RequirementException('Failed to update ROS dependencies')

    # install bare bones ROS
    v = requirement_specifier.version
    retcode = subprocess.call([rosinstall_generator + ' ros_comm --rosdistro %s'
                               ' --deps --wet-only > %s-ros_comm-wet.rosinstall' % (v, v)])
    if retcode != 0:
        raise RequirementException('Failed to generate rosinstall file')
    retcode = subprocess.call([wstool +' init -j8 src %s-ros_comm-wet.rosinstall' % v])
    if retcode != 0:
        raise RequirementException('Failed to generate rosinstall file')

@@ -292,16 +292,30 @@
 ception%0A
+import shutil%0A
 import s
@@ -1147,24 +1147,553 @@
 l==0.0.4')%0A%0A
+    def in_cache():%0A        devel_dir = os.path.join(robustus.cache, 'ros-%25s' %25 requirement_specifier.version, 'devel_isolated')%0A        return os.path.isdir(devel_dir)%0A%0A    try:%0A        cwd = os.getcwd()%0A%0A        # create ros cache%0A        v = requirement_specifier.version%0A        ros_cache = os.path.join(robustus.cache, 'ros-%25s' %25 v)%0A        if not os.path.isdir(ros_cache):%0A            os.mkdir(ros_cache)%0A        os.chdir(ros_cache)%0A%0A        # build ros if necessary%0A        if not in_cache() and not ignore_index:%0A        
     # rosdep
@@ -1751,16 +1751,24 @@
 at's ok%0A
+        
     subp
@@ -1806,24 +1806,32 @@
 hell=True)%0A%0A
+        
     # update
@@ -1848,16 +1848,24 @@
 dencies%0A
+        
     retc
@@ -1918,16 +1918,24 @@
 l=True)%0A
+        
     if r
@@ -1939,32 +1939,40 @@
 f retcode != 0:%0A
+        
         raise Re
@@ -2024,24 +2024,32 @@
 ndencies')%0A%0A
+        
     # instal
@@ -2073,42 +2073,12 @@
     
-v = requirement_specifier.version%0A
+    
     
@@ -2103,17 +2103,16 @@
 ss.call(
-%5B
 rosinsta
@@ -2152,17 +2152,26 @@
 stro %25s'
-%0A
+ %25 v%0A     
         
@@ -2192,16 +2192,20 @@
         
+   +
  ' --dep
@@ -2253,17 +2253,31 @@
 ' %25 
-(
 v, 
-v)%5D)%0A
+shell=True)%0A        
     
@@ -2285,32 +2285,40 @@
 f retcode != 0:%0A
+        
         raise Re
@@ -2374,16 +2374,25 @@
  file')%0A
+%0A        
     retc
@@ -2417,17 +2417,16 @@
 all(
-%5B
 wstool +
 ' in
@@ -2421,16 +2421,17 @@
 wstool +
+ 
 ' init -
@@ -2472,11 +2472,30 @@
  %25 v
-%5D)%0A
+, shell=True)%0A        
     
@@ -2503,32 +2503,40 @@
 f retcode != 0:%0A
+        
         raise Re
@@ -2569,31 +2569,754 @@
  to 
-generate rosinstall file')
+build ROS')%0A%0A            # resolve dependencies%0A            retcode = subprocess.call(rosdep + ' install --from-paths src --ignore-src --rosdistro %25s -y' %25 v, shell=True)%0A            if retcode != 0:%0A                raise RequirementException('Failed to resolve ROS dependencies')%0A%0A        # create catkin workspace%0A        rosdir = os.path.join(robustus.env, 'ros')%0A        catkin_make_isolated = os.path.join(ros_cache, 'src/catkin/bin/catkin_make_isolated')%0A        retcode = subprocess.call(catkin_make_isolated + ' --install-space %25s --install' %25 rosdir, shell=True)%0A        if retcode != 0:%0A            raise RequirementException('Failed to create catkin workspace for ROS')%0A    finally:%0A        os.chdir(cwd)%0A        shutil.rmtree(ros_cache)%0A
 %0A

build ros in cache