package_version_tool.py
Go to the documentation of this file.
1 #
2 # package_version_tool.py extracts the version number from package.xml
3 # and generates a headerfile sick_scan_xd_version.h defining that version.
4 #
5 # Usage example:
6 # python python/tools/package_version_tool.py --package_xml_file package.xml --cpp_header_path include/sick_scan/sick_scan_xd_version.h
7 #
8 
9 import xml.etree.ElementTree as ET
10 import argparse
11 
13  # Define the exact line keyword with version number
14  search_keyword = '#define SICK_SCAN_XD_VERSION'
15 
16  # Open the header file and read line by line
17  with open(file_path, 'r') as file:
18  for line in file:
19  # Ensure the line starts with the keyword including the version number
20  if line.strip().startswith(search_keyword):
21  # Extract the version number enclosed in quotes
22  try:
23  version = line.split()[2] # Split the line at the quotes and take the second element
24  return version
25  except IndexError:
26  # Handle unexpected format
27  print("Version format in the file does not match expected format.")
28  return ""
29 
30 def parse_xml_and_generate_header(package_xml_file, cpp_header_path):
31  # Parse the XML file
32  tree = ET.parse(package_xml_file)
33  root = tree.getroot()
34 
35  # Extract the version number from the <version> tag in package.xml
36  package_version = root.find('version').text
37  package_version = f'\"{package_version}\"'
38 
39  # Extract the version number from "define SICK_SCAN_XD_VERSION" in cpp_header_path
40  header_version = extract_version_from_header(cpp_header_path)
41  print(f"Found version {package_version} in file \"{package_xml_file}\" and version {header_version} in file \"{cpp_header_path}\"")
42 
43  if package_version != header_version:
44  # Create a C++ header file content with the version defined and write to C++ header file
45  cpp_header_content = f"#ifndef SICK_SCAN_XD_VERSION\n#define SICK_SCAN_XD_VERSION {package_version}\n#endif // SICK_SCAN_XD_VERSION\n"
46  with open(cpp_header_path, 'w') as cpp_file:
47  cpp_file.write(f'/* This file has been generated by package_version_tool.py. Do not edit this file. To provide a new version number, modify file package.xml and rebuild */\n')
48  cpp_file.write(cpp_header_content)
49  print(f"Generated file \"{cpp_header_path}\" with version {package_version}")
50  else:
51  print(f"Found identical versions, leaving file \"{cpp_header_path}\" unmodified")
52 
53 
54 def main():
55  parser = argparse.ArgumentParser(description="Parse XML to extract the version and generate a C++ header file.")
56  parser.add_argument('--package_xml_file', type=str, default='package.xml', help='Path to the package.xml file.')
57  parser.add_argument('--cpp_header_path', type=str, default='include/sick_scan/sick_scan_xd_version.h', help='Path to save the C++ header file.')
58 
59  args = parser.parse_args()
60 
61  parse_xml_and_generate_header(args.package_xml_file, args.cpp_header_path)
62 
63 if __name__ == '__main__':
64  main()
roswrap::console::print
ROSCONSOLE_DECL void print(FilterBase *filter, void *logger, Level level, const char *file, int line, const char *function, const char *fmt,...) ROSCONSOLE_PRINTF_ATTRIBUTE(7
Don't call this directly. Use the ROS_LOG() macro instead.
package_version_tool.parse_xml_and_generate_header
def parse_xml_and_generate_header(package_xml_file, cpp_header_path)
Definition: package_version_tool.py:30
package_version_tool.main
def main()
Definition: package_version_tool.py:54
package_version_tool.extract_version_from_header
def extract_version_from_header(file_path)
Definition: package_version_tool.py:12


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:09