Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean nhg dt #28

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
44 changes: 44 additions & 0 deletions haros_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python
import sys
import os
import subprocess
from datetime import datetime
import shutil
import glob

from rosHumble_model_extractor_scripts.ros2runner import ros2Runner

# Arguments:
# 1: Package name or Git repository name
# 2: Node name or launch file name or '--all' to analyse all the available nodes
# 3: Type of the request: either 'launch' or 'node'
# 4: Path to the folder where the resulting model files should be stored
# 5: Path to the ROS workspace
# 6: Http address links of the Git repositories (to indicate the branch put the name of the repository between quotes and add the suffix -b *banch_name*, for example "https://github.com/ipa320/ros-model-extractors b main"

def main():

CLI_args = sys.argv[1:]

if len(CLI_args) < 6:
print(f'ERROR: At least 6 arguments expected, only {len(CLI_args)} are given.')
sys.exit()

pkgName = sys.argv[1]
NodeName = sys.argv[2]
typeOfRequest = sys.argv[3]
pathToOutput = sys.argv[4]
pathToROSws = sys.argv[5]
gitRepo = sys.argv[6]

ros2Extractor = ros2Runner(inputPkgName=pkgName,
inputNodeName=NodeName,
typeOfRequest=typeOfRequest,
pathToROSws=pathToROSws,
gitRepo=gitRepo,
outputDir=pathToOutput)
ros2Extractor.extractorRun(True)


if __name__ == "__main__":
main()
9 changes: 5 additions & 4 deletions haros_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ then
then
if [ "${2}" = "--all" ]
then
python3 /ros_model_extractor.py --package "$1" --"${3}" --model-path "${4}" --ws "${5}" --path-to-src "$path_to_src_code" --repo $model_repo -a >> extractor.log
python3 /ros_model_extractor.py --package "$1" --"${3}" --model-path "${4}" --ws "${5}" --path-to-src "$path_to_src_code" --repo $model_repo -a >> ${4}/extractor.log
else
python3 /ros_model_extractor.py --package "$1" --name "$2" --"${3}" --model-path "${4}" --ws "${5}" --path-to-src "$path_to_src_code" --repo $model_repo>> extractor.log
python3 /ros_model_extractor.py --package "$1" --name "$2" --"${3}" --model-path "${4}" --ws "${5}" --path-to-src "$path_to_src_code" --repo $model_repo>> ${4}/extractor.log
fi
#cat extractor.log
else
Expand All @@ -102,7 +102,7 @@ fi

echo "~~~~~~~~~~~"
echo "Extraction finished. See the following report:"
cat extractor.log
cat ${4}/extractor.log
echo "~~~~~~~~~~~"

echo "###########"
Expand All @@ -118,4 +118,5 @@ echo "###########"
done

## Clean and finish
rm -rf ${5}/src/*
# rm -rf ${5}/src/*
# rm -rf ${4}/*
435 changes: 435 additions & 0 deletions rosHumble_model_extractor_scripts/Ros_Extractor.py

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions rosHumble_model_extractor_scripts/file_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
import sys
import os
import subprocess
from datetime import datetime
import shutil

import glob


class fileHandling:
def __init__(self):
pass

@staticmethod
def wrtieToFile(fileNameWithPath, contentToWrite):
with open(fileNameWithPath, "w") as file:
file.write(contentToWrite)
print("-----Write to {} completed-----".format(fileNameWithPath))

@staticmethod
def showFileContent(fileNameWithPath):

print("-----Show the ROS model of {}".format(fileNameWithPath))
with open(fileNameWithPath,'r') as file:
print(file.read())

@staticmethod
def filterFileNamesByExtension(rootDir, extensionToSearch):

fileList = glob.glob(os.path.join(rootDir, '**', "*.{}".format(extensionToSearch)), recursive=True)

return fileList
119 changes: 119 additions & 0 deletions rosHumble_model_extractor_scripts/ros2runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env python
import sys
import os
import subprocess
from datetime import datetime
import shutil
from rosHumble_model_extractor_scripts.ros_pkg_runner import rosPkgRunner
from rosHumble_model_extractor_scripts.file_handling import fileHandling
import glob



class ros2Runner(rosPkgRunner):

ROS2setupBashFile = "install/setup.bash"
ROS2Version = 2
ROS2PythonVer = 3


def __init__(self,
inputPkgName:str,
inputNodeName:str,
typeOfRequest:str,
pathToROSws:str,
gitRepo:str,
outputDir:str):

super().__init__(inputPkgName=inputPkgName,
inputNodeName=inputNodeName,
typeOfRequest=typeOfRequest,
pathToROSws=pathToROSws,
gitRepo=gitRepo,
setupBashFile=self.ROS2setupBashFile,
outputDir=outputDir)

def runColconBuild(self,):
subprocess.run(["colcon", "build", "--cmake-args"]) #CMake Colcon Warning fix

def runColconList(self,):
os.system('colcon list > /tmp/colcon_list.txt')



def calcPathToPkgSRC(self, textList:str):

os.system('cat /tmp/colcon_list.txt | grep "^$1" | awk \'{ print $2}\' > /tmp/colcon_list_update.txt')
path_to_src_code=subprocess.run(["cat","/tmp/colcon_list_update.txt"], stdout=subprocess.PIPE, text=True).stdout
path_to_src_code=path_to_src_code.split("\n")

self.setPathToSRCcode([os.path.join(self.getPathToROSws(), item) for item in path_to_src_code])
self.validatePathToSRCcodeList()

def startInstallPkgProcedure(self):

self.runCommonCmd()
self.runColconBuild()
self.runSetupBash()
colconList = self.runColconList()
self.calcPathToPkgSRC(colconList)
print("## ROS pkg install and Build complete")

def callHarosPlugin(self,):

self.harosInit()


for pkgName in self.getPathToSRCcode():
currOutDir = os.path.join(self.outputDir, pkgName.split('/')[-1])
fileName = os.path.join(os.path.dirname(__file__), self.getROSModelExtractorPluginFile())
extractorFile = currOutDir+"/extractor.log"
extractorContent = None

if(not os.path.isdir(currOutDir)):
os.makedirs(currOutDir)

if self.getInputNodeName() == "--all":
extractorContent = subprocess.run(["python3", fileName,
"--package", str(self.getInputPkgName()),
"--"+str(self.getTypeOfRequest()),
"--model-path",str(currOutDir) ,
"--ws", str(self.getPathToROSws()),
"--path-to-src", pkgName,
"--repo", str(self.getGitModelRepo()),
"-a"], stdout=subprocess.PIPE, text=True).stdout
else:
extractorContent = subprocess.run(["python3", fileName,
"--package", str(self.getInputPkgName()),
"--name", str(self.getInputNodeName()),
"--"+str(self.getTypeOfRequest()),
"--model-path",str(currOutDir),
"--ws", str(self.getPathToROSws()),
"--path-to-src", pkgName,
"--repo", str(self.getGitModelRepo())
], stdout=subprocess.PIPE, text=True).stdout
fileHandling.wrtieToFile(extractorFile, extractorContent)

def extractorRun(self,clearFlag=False):


#1. clone the git repo at the required
rosPkgRunner.cloneRepo(self.getPathToROSws(), self.getGitRepo())

#2. Change working directory to ROS ws
os.chdir(self.getPathToROSws())

#3. Install dependencies and build
self.startInstallPkgProcedure()

#4. Init the plugin
self.callHarosPlugin()

#5. Show the ros models
self.showAllROSmodel()

#6. Clear the ROS ws SRC folder
if clearFlag:
self.cleanUPsrc()

print( "------------------Done---------------")
58 changes: 58 additions & 0 deletions rosHumble_model_extractor_scripts/rosHumble_model_extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python
#
# Copyright 2019 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
#
# Nadia Hammoudeh Garcia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from Ros_Extractor import RosExtractor


class RosInterface:
def __init__(self, name, ref):
self.name = name
self.ref = ref

class ros_component:
def __init__(self, name, ns):
self.name = ns+name if ns else name
self.ns = ns
self.pubs = []
self.subs = []
self.srvsrvs = []
self.srvcls = []
self.actsrvs = []
self.actcls = []
def add_interface(self, name, interface_type, ref):
if interface_type == "pubs":
self.pubs.append(RosInterface(name,ref))
if interface_type == "subs":
self.subs.append(RosInterface(name,ref))
if interface_type == "srvsrvs":
self.srvsrvs.append(RosInterface(name,ref))
if interface_type == "srvcls":
self.srvcls.append(RosInterface(name,ref))
if interface_type == "actsrvs":
self.actsrvs.append(RosInterface(name,ref))
if interface_type == "actcls":
self.actcls.append(RosInterface(name,ref))

def main(argv = None):
extractor = RosExtractor()
if extractor.launch():
return 0
return 1

if __name__== "__main__":
main()
Loading