makeversionhdr.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """
  2. Generate header file with macros defining MicroPython version info.
  3. This script works with Python 2.6, 2.7, 3.3 and 3.4.
  4. """
  5. from __future__ import print_function
  6. import sys
  7. import os
  8. import datetime
  9. import subprocess
  10. def get_version_info_from_git():
  11. # Python 2.6 doesn't have check_output, so check for that
  12. try:
  13. subprocess.check_output
  14. subprocess.check_call
  15. except AttributeError:
  16. return None
  17. # Note: git describe doesn't work if no tag is available
  18. try:
  19. git_tag = subprocess.check_output(["git", "describe", "--dirty", "--always"], stderr=subprocess.STDOUT, universal_newlines=True).strip()
  20. except subprocess.CalledProcessError as er:
  21. if er.returncode == 128:
  22. # git exit code of 128 means no repository found
  23. return None
  24. git_tag = ""
  25. except OSError:
  26. return None
  27. try:
  28. git_hash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], stderr=subprocess.STDOUT, universal_newlines=True).strip()
  29. except subprocess.CalledProcessError:
  30. git_hash = "unknown"
  31. except OSError:
  32. return None
  33. try:
  34. # Check if there are any modified files.
  35. subprocess.check_call(["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], stderr=subprocess.STDOUT)
  36. # Check if there are any staged files.
  37. subprocess.check_call(["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], stderr=subprocess.STDOUT)
  38. except subprocess.CalledProcessError:
  39. git_hash += "-dirty"
  40. except OSError:
  41. return None
  42. # Try to extract MicroPython version from git tag
  43. if git_tag.startswith("v"):
  44. ver = git_tag[1:].split("-")[0].split(".")
  45. if len(ver) == 2:
  46. ver.append("0")
  47. else:
  48. ver = ["0", "0", "1"]
  49. return git_tag, git_hash, ver
  50. def get_version_info_from_docs_conf():
  51. with open(os.path.join(os.path.dirname(sys.argv[0]), "..", "docs", "conf.py")) as f:
  52. for line in f:
  53. if line.startswith("version = release = '"):
  54. ver = line.strip().split(" = ")[2].strip("'")
  55. git_tag = "v" + ver
  56. ver = ver.split(".")
  57. if len(ver) == 2:
  58. ver.append("0")
  59. return git_tag, "<no hash>", ver
  60. return None
  61. def make_version_header(filename):
  62. # Get version info using git, with fallback to docs/conf.py
  63. info = get_version_info_from_git()
  64. if info is None:
  65. info = get_version_info_from_docs_conf()
  66. git_tag, git_hash, ver = info
  67. # Generate the file with the git and version info
  68. file_data = """\
  69. // This file was generated by py/makeversionhdr.py
  70. #define MICROPY_GIT_TAG "%s"
  71. #define MICROPY_GIT_HASH "%s"
  72. #define MICROPY_BUILD_DATE "%s"
  73. #define MICROPY_VERSION_MAJOR (%s)
  74. #define MICROPY_VERSION_MINOR (%s)
  75. #define MICROPY_VERSION_MICRO (%s)
  76. #define MICROPY_VERSION_STRING "%s.%s.%s"
  77. """ % (git_tag, git_hash, datetime.date.today().strftime("%Y-%m-%d"),
  78. ver[0], ver[1], ver[2], ver[0], ver[1], ver[2])
  79. # Check if the file contents changed from last time
  80. write_file = True
  81. if os.path.isfile(filename):
  82. with open(filename, 'r') as f:
  83. existing_data = f.read()
  84. if existing_data == file_data:
  85. write_file = False
  86. # Only write the file if we need to
  87. if write_file:
  88. print("GEN %s" % filename)
  89. with open(filename, 'w') as f:
  90. f.write(file_data)
  91. if __name__ == "__main__":
  92. make_version_header(sys.argv[1])