version.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/python3
  2. import json
  3. import configparser
  4. import argparse
  5. import re
  6. import os
  7. import datetime
  8. travis_dir = os.path.dirname(os.path.abspath(__file__))
  9. base_dir = os.path.abspath(travis_dir + "/../")
  10. def write_header_file(version):
  11. hvs = version.split('.')
  12. intversion = int(hvs[0]) * 1000000 + int(hvs[1]) * 1000 + int(hvs[2])
  13. now = datetime.datetime.now()
  14. text = f'''/**
  15. * @file WebSocketsVersion.h
  16. * @date {now.strftime("%d.%m.%Y")}
  17. * @author Markus Sattler
  18. *
  19. * Copyright (c) 2015 Markus Sattler. All rights reserved.
  20. * This file is part of the WebSockets for Arduino.
  21. *
  22. * This library is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU Lesser General Public
  24. * License as published by the Free Software Foundation; either
  25. * version 2.1 of the License, or (at your option) any later version.
  26. *
  27. * This library is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. * Lesser General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Lesser General Public
  33. * License along with this library; if not, write to the Free Software
  34. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  35. *
  36. */
  37. #ifndef WEBSOCKETSVERSION_H_
  38. #define WEBSOCKETSVERSION_H_
  39. #define WEBSOCKETS_VERSION "{version}"
  40. #define WEBSOCKETS_VERSION_MAJOR {hvs[0]}
  41. #define WEBSOCKETS_VERSION_MINOR {hvs[1]}
  42. #define WEBSOCKETS_VERSION_PATCH {hvs[2]}
  43. #define WEBSOCKETS_VERSION_INT {intversion}
  44. #endif /* WEBSOCKETSVERSION_H_ */
  45. '''
  46. with open(f'{base_dir}/src/WebSocketsVersion.h', 'w') as f:
  47. f.write(text)
  48. def get_library_properties_version():
  49. library_properties = {}
  50. with open(f'{base_dir}/library.properties', 'r') as f:
  51. library_properties = configparser.ConfigParser()
  52. library_properties.read_string('[root]\n' + f.read())
  53. return library_properties['root']['version']
  54. def get_library_json_version():
  55. library_json = {}
  56. with open(f'{base_dir}/library.json', 'r') as f:
  57. library_json = json.load(f)
  58. return library_json['version']
  59. def get_header_versions():
  60. data = {}
  61. define = re.compile('^#define WEBSOCKETS_VERSION_?(.*) "?([0-9\.]*)"?$')
  62. with open(f'{base_dir}/src/WebSocketsVersion.h', 'r') as f:
  63. for line in f:
  64. m = define.match(line)
  65. if m:
  66. name = m[1]
  67. if name == "":
  68. name = "VERSION"
  69. data[name] = m[2]
  70. return data
  71. parser = argparse.ArgumentParser(description='Checks and update Version files')
  72. parser.add_argument(
  73. '--update', action='store_true', default=False)
  74. parser.add_argument(
  75. '--check', action='store_true', default=True)
  76. args = parser.parse_args()
  77. if args.update:
  78. library_properties_version = get_library_properties_version()
  79. with open(f'{base_dir}/library.json', 'r') as f:
  80. library_json = json.load(f)
  81. library_json['version'] = library_properties_version
  82. with open(f'{base_dir}/library.json', 'w') as f:
  83. json.dump(library_json, f, indent=4, sort_keys=True)
  84. write_header_file(library_properties_version)
  85. library_json_version = get_library_json_version()
  86. library_properties_version = get_library_properties_version()
  87. header_version = get_header_versions()
  88. print("WebSocketsVersion.h", header_version)
  89. print(f"library.json: {library_json_version}")
  90. print(f"library.properties: {library_properties_version}")
  91. if args.check:
  92. if library_json_version != library_properties_version or header_version['VERSION'] != library_properties_version:
  93. raise Exception('versions did not match!')
  94. hvs = header_version['VERSION'].split('.')
  95. if header_version['MAJOR'] != hvs[0]:
  96. raise Exception('header MAJOR version wrong!')
  97. if header_version['MINOR'] != hvs[1]:
  98. raise Exception('header MINOR version wrong!')
  99. if header_version['PATCH'] != hvs[2]:
  100. raise Exception('header PATCH version wrong!')
  101. intversion = int(hvs[0]) * 1000000 + int(hvs[1]) * 1000 + int(hvs[2])
  102. if int(header_version['INT']) != intversion:
  103. raise Exception('header INT version wrong!')