From 4b1053d2122ef8fac704deadeaf94f2c0048b9b8 Mon Sep 17 00:00:00 2001
From: TSC21 <n.marques21@hotmail.com>
Date: Sun, 27 Jan 2019 17:02:33 +0000
Subject: [PATCH] Jenkins: set autobuild and deploy for generating px4_msgs
 repo ROS msg definitions files

---
 .gitignore                               |   1 +
 Jenkinsfile                              |  27 ++++++
 msg/ros/actuator_controls_0.msg          |   4 -
 msg/ros/actuator_controls_virtual_mc.msg |   4 -
 msg/tools/uorb_to_ros_msgs.py            | 106 +++++++++++++++++++++++
 5 files changed, 134 insertions(+), 8 deletions(-)
 delete mode 100644 msg/ros/actuator_controls_0.msg
 delete mode 100644 msg/ros/actuator_controls_virtual_mc.msg
 create mode 100644 msg/tools/uorb_to_ros_msgs.py

diff --git a/.gitignore b/.gitignore
index f7c268a521..d4d5a9d446 100644
--- a/.gitignore
+++ b/.gitignore
@@ -59,6 +59,7 @@ posix-configs/SITL/init/test/*_generated
 /parameters.md
 /parameters.xml
 /modules
+/msg/ros/*.msg
 
 *.gcov
 .coverage
diff --git a/Jenkinsfile b/Jenkinsfile
index 21f5c05e5d..8dc67482c0 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -473,6 +473,33 @@ pipeline {
           }
         }
 
+        stage('Generate PX4 ROS msgs') {
+          agent {
+            docker { image 'px4io/px4-dev-base:2019-01-01' }
+          }
+          steps {
+            sh('export')
+            withCredentials([usernamePassword(credentialsId: 'px4buildbot_github_personal_token', passwordVariable: 'GIT_PASS', usernameVariable: 'GIT_USER')]) {
+              sh('git clone https://${GIT_USER}:${GIT_PASS}@github.com/PX4/px4_msgs.git')
+              sh('python msg/tools/uorb_to_ros_msgs.py msg/ msg/ros/')
+              sh('cp -a msg/ros/. px4_msgs/msg')
+              sh('cd px4_msgs; git status; git add .; git commit -a -m "Update message definitions `date`" || true')
+              sh('cd px4_msgs; git push origin master || true')
+              sh('rm -rf px4_msgs')
+              sh('rm -rf msg/ros')
+            }
+          }
+          when {
+            anyOf {
+              branch 'master'
+              branch 'pr-jenkins' // for testing
+            }
+          }
+          options {
+            skipDefaultCheckout()
+          }
+        }
+
         stage('S3') {
           agent {
             docker { image 'px4io/px4-dev-base:2019-01-26' }
diff --git a/msg/ros/actuator_controls_0.msg b/msg/ros/actuator_controls_0.msg
deleted file mode 100644
index 4424f197f8..0000000000
--- a/msg/ros/actuator_controls_0.msg
+++ /dev/null
@@ -1,4 +0,0 @@
-uint8 NUM_ACTUATOR_CONTROLS = 8
-uint8 NUM_ACTUATOR_CONTROL_GROUPS = 4
-uint64 timestamp_sample	    # the timestamp the data this control response is based on was sampled
-float32[8] control
diff --git a/msg/ros/actuator_controls_virtual_mc.msg b/msg/ros/actuator_controls_virtual_mc.msg
deleted file mode 100644
index 4424f197f8..0000000000
--- a/msg/ros/actuator_controls_virtual_mc.msg
+++ /dev/null
@@ -1,4 +0,0 @@
-uint8 NUM_ACTUATOR_CONTROLS = 8
-uint8 NUM_ACTUATOR_CONTROL_GROUPS = 4
-uint64 timestamp_sample	    # the timestamp the data this control response is based on was sampled
-float32[8] control
diff --git a/msg/tools/uorb_to_ros_msgs.py b/msg/tools/uorb_to_ros_msgs.py
new file mode 100644
index 0000000000..3d0823409f
--- /dev/null
+++ b/msg/tools/uorb_to_ros_msgs.py
@@ -0,0 +1,106 @@
+#!/usr/bin/env python
+"""
+Script to parse uORB message format to ROS msg format
+Adapted from https://github.com/eProsima/px4_to_ros/blob/master/px4_to_ros2_PoC/px4_msgs/scripts/copy_and_rename.py
+"""
+
+import os
+import sys
+from shutil import copyfile
+
+__author__ = 'PX4 Development Team'
+__copyright__ = \
+    '''
+     '
+     '   Copyright (C) 2018 PX4 Development Team. All rights reserved.
+     '
+     ' Redistribution and use in source and binary forms, or without
+     ' modification, permitted provided that the following conditions
+     ' are met:
+     '
+     ' 1. Redistributions of source code must retain the above copyright
+     '    notice, list of conditions and the following disclaimer.
+     ' 2. Redistributions in binary form must reproduce the above copyright
+     '    notice, list of conditions and the following disclaimer in
+     '    the documentation and/or other materials provided with the
+     '    distribution.
+     ' 3. Neither the name PX4 nor the names of its contributors may be
+     '    used to endorse or promote products derived from self software
+     '    without specific prior written permission.
+     '
+     ' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+     ' "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, NOT
+     ' LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+     ' FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+     ' COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+     ' INCIDENTAL, SPECIAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING,
+     ' BUT NOT LIMITED TO, OF SUBSTITUTE GOODS OR SERVICES; LOSS
+     ' OF USE, DATA, PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+     ' AND ON ANY THEORY OF LIABILITY, IN CONTRACT, STRICT
+     ' LIABILITY, TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+     ' ANY WAY OUT OF THE USE OF THIS SOFTWARE, IF ADVISED OF THE
+     ' POSSIBILITY OF SUCH DAMAGE.
+     '
+    '''
+__credits__ = ['Nuno Marques <nuno.marques@dronesolution.io>']
+__license__ = 'BSD-3-Clause'
+__version__ = '0.1.0'
+__maintainer__ = 'Nuno Marques'
+__email__ = 'nuno.marques@dronesolution.io'
+__status__ = 'Development'
+
+input_dir = sys.argv[1]
+output_dir = sys.argv[2]
+
+os.mkdir(os.path.abspath(output_dir))
+
+msg_list = list()
+
+for filename in os.listdir(input_dir):
+    if '.msg' in filename:
+        msg_list.append(filename.rstrip('.msg'))
+        input_file = input_dir + filename
+
+        output_file = output_dir + \
+            filename.partition(".")[0].title().replace('_', '') + ".msg"
+        copyfile(input_file, output_file)
+
+for filename in os.listdir(output_dir):
+    if '.msg' in filename:
+        input_file = output_dir + filename
+
+        fileUpdated = False
+
+        with open(input_file, 'r') as f:
+            lines = f.readlines()
+            newlines = []
+            alias_msgs = []
+            alias_msg_files = []
+
+            for line in lines:
+                for msg_type in msg_list:
+                    if ('px4/' + msg_type + ' ') in line:
+                        fileUpdated = True
+                        line = line.replace(('px4/' + msg_type),
+                                            msg_type.partition(".")[0].title().replace('_', ''))
+                    if ('' + msg_type + '[') in line.partition('#')[0] or ('' + msg_type + ' ') in line.partition('#')[0]:
+                        fileUpdated = True
+                        line = line.replace(msg_type,
+                                            msg_type.partition(".")[0].title().replace('_', ''))
+                    if '# TOPICS' in line:
+                        fileUpdated = True
+                        alias_msgs += line.split()
+                        alias_msgs.remove('#')
+                        alias_msgs.remove('TOPICS')
+                        line = line.replace(line, '')
+                newlines.append(line)
+
+        for msg_file in alias_msgs:
+            with open(output_dir + msg_file.partition(".")[0].title().replace('_', '') + ".msg", 'w+') as f:
+                for line in newlines:
+                    f.write(line)
+
+        if fileUpdated:
+            with open(input_file, 'w+') as f:
+                for line in newlines:
+                    f.write(line)
-- 
GitLab