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

feat: change method of getting version from setup.py to package.xml #177

Merged
merged 8 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}')
Loading