Skip to content

Commit

Permalink
feat: change method of getting version from setup.py to package.xml
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 13, 2024
1 parent b4581ae commit 64eec72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 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
25 changes: 15 additions & 10 deletions ros2caret/verb/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
# limitations under the License.

import os.path
import re
import xml.etree.ElementTree as ET

from ament_index_python.packages import get_package_share_directory, PackageNotFoundError

from ros2caret.verb import VerbExtension

Expand All @@ -25,12 +27,15 @@ def main(self, *, args):
print('v' + version)

def get_version(self):
version_path = f'{os.path.dirname(os.path.realpath(__file__))}/../../setup.py'
version_pattern = re.compile(r"\s*version\s*=\s*['\"](\d+\.\d+\.\d+)['\"]")
with open(version_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.')
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):
xml_path = os.path.join(get_package_share_directory('ros2caret'), 'package.xml')
raise RuntimeError(f'Error: Cannot find {xml_path}')

0 comments on commit 64eec72

Please sign in to comment.