Skip to content

Commit

Permalink
Update: using xml.etree.ElementTree
Browse files Browse the repository at this point in the history
Signed-off-by: h-suzuki <[email protected]>
  • Loading branch information
h-suzuki-isp committed Mar 7, 2024
1 parent b0efbf1 commit 1b29853
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<license>Apache License 2.0</license>

<exec_depend>python3-tabulate</exec_depend>
<exec_depend>ament_index_python</exec_depend>
<depend>ros2cli</depend>
<depend>caret_analyze</depend>
<depend>caret_msgs</depend>
Expand Down
26 changes: 14 additions & 12 deletions ros2caret/verb/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# limitations under the License.

import os.path
import re

from ros2caret.verb import VerbExtension
from ament_index_python.packages import get_package_share_directory
from ament_index_python.packages import get_package_share_directory, PackageNotFoundError
import xml.etree.ElementTree as ET


class CaretVersionVerb(VerbExtension):
Expand All @@ -26,13 +26,15 @@ def main(self, *, args):
print('v' + version)

def get_version(self):
dir_path = get_package_share_directory('ros2caret')
xml_path = os.path.join(dir_path, 'package.xml')
version_pattern = re.compile(r"\s*<version>\s*(\d+\.\d+\.\d+)\s*</version>\s*")
with open(xml_path) as f:
for line in f:
match = version_pattern.search(line)
if match:
return match.group(1)
else:
raise RuntimeError('Unable to find version string.')
# FileNotFoundError
try:
dir_path = get_package_share_directory('ros2caret')
xml_path = os.path.join(dir_path, 'package.xml')
tree = ET.parse(xml_path)
root = tree.getroot()
version_element = root.find('version')
if version_element is None:
raise RuntimeError("Error: Package version not found in package.xml")
return version_element.text
except(PackageNotFoundError, FileNotFoundError):
raise RuntimeError("Error: package or file share/ros2caret/package.xml not found")

0 comments on commit 1b29853

Please sign in to comment.