rospackage.py
Go to the documentation of this file.
1 '''
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19  '''
20 import rospkg
21 import os
22 from xml.dom import minidom
23 from PyQt5.QtCore import QCoreApplication, QSettings
24 
25 # we keep module variables not to calculate these lists every time gui needs them
26 rospackmodule = None
27 rosTypes = None
28 rosPackages = None
29 
30 
32  QCoreApplication.setOrganizationName('JdeRobot')
33  QCoreApplication.setOrganizationDomain('jderobot.org')
34  QCoreApplication.setApplicationName('VisualStates')
35  return QSettings()
36 
37 
39  workspaces = []
40  settings = setupSettings()
41  count = settings.beginReadArray('workspaces')
42  for i in range(count):
43  settings.setArrayIndex(i)
44  workspaces.append(settings.value('dir'))
45  settings.endArray()
46  return workspaces
47 
48 
49 def writeWorkspaces(workspaces):
50  global rosPackages
51  global rosTypes
52  settings = setupSettings()
53  settings.beginWriteArray('workspaces')
54  for i in range(len(workspaces)):
55  settings.setArrayIndex(i)
56  settings.setValue('dir', workspaces[i])
57  settings.endArray()
58  rosTypes = None
59  rosPackages = None
60 
61 
62 def getRosMessageTypes(rosDir=None):
63  if rosDir is None:
64  if 'ROS_DISTRO' in os.environ:
65  rosDir = '/opt/ros/' + os.environ['ROS_DISTRO']
66  else:
67  rosDir = '/opt/ros/kinetic'
68 
69  messageDir = rosDir + '/include'
70  if os.path.exists(messageDir):
71  allContents = os.listdir(messageDir)
72  messages = []
73  for entry in allContents:
74  if os.path.isdir(messageDir + '/' + entry):
75  if entry.find('_msgs') >= 0:
76  messages.append(entry)
77 
78  types = []
79  for msg in messages:
80  typeDir = msg
81  for entry in os.listdir(messageDir + '/' + msg):
82  if os.path.isfile(messageDir + '/' + msg + '/' + entry):
83  if entry.find('.h') >= 0 and entry[0].isupper():
84  type = {}
85  type['typeDir'] = typeDir
86  type['type'] = entry[:entry.find('.h')]
87  types.append(type)
88  return types
89  else:
90  return False
91 
92 
94  global rospackmodule
95  if rospackmodule is None:
96  rospackmodule = rospkg.RosPack()
97  return rospackmodule.get_path('visualstates')
98 
99 
101  global rospackmodule
102  global rosPackages
103  if rosPackages is None:
104  if rospackmodule is None:
105  rospackmodule = rospkg.RosPack()
106  rosPackages = rospackmodule.list()
107 
108  workspaces = readWorkspaces()
109  for dir in workspaces:
110  packages = getPackages(dir)
111  rosPackages += packages
112 
113  return rosPackages
114 
115 
117  global rosTypes
118  if rosTypes is None:
119  messageTypes = getRosMessageTypes()
120 
121  workspaces = readWorkspaces()
122  for dir in workspaces:
123  messageTypes += getRosMessageTypes(dir + '/devel')
124 
125  concatTypes = []
126  for type in messageTypes:
127  concatType = type['typeDir'] + '/' + type['type']
128  concatTypes.append(concatType)
129 
130  rosTypes = sorted(concatTypes)
131 
132  return rosTypes
133 
134 
135 def getPackages(workspaceDir):
136  packageNames = []
137  packageFiles = []
138  for root, dirs, files in os.walk(workspaceDir + '/src'):
139  visibleDirs = []
140  for dir in dirs:
141  if not dir.startswith('.'):
142  visibleDirs.append(dir)
143  dirs[:] = visibleDirs
144  for file in files:
145  if file == 'package.xml':
146  packageFiles.append(root + '/' + file)
147 
148  for pkgFile in packageFiles:
149  doc = minidom.parse(pkgFile)
150  nameElement = doc.getElementsByTagName('package')[0].getElementsByTagName('name')[0]
151  if len(nameElement.childNodes) > 0:
152  packageNames.append(nameElement.childNodes[0].nodeValue)
153  return packageNames
154 
155 
156 if __name__ == '__main__':
157  # print('packagePath:' + getPackagePath())
158  # print('packages:' + str(getAllPackages()))
159  print(getPackages('/media/okan/depo/jderobot/catkin_ws'))
160 
161 
def getRosMessageTypes(rosDir=None)
Definition: rospackage.py:62


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20