makeversionhdr.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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(
  20. ["git", "describe", "--dirty", "--always", "--match", "v[1-9].*"],
  21. stderr=subprocess.STDOUT,
  22. universal_newlines=True,
  23. ).strip()
  24. except subprocess.CalledProcessError as er:
  25. if er.returncode == 128:
  26. # git exit code of 128 means no repository found
  27. return None
  28. git_tag = ""
  29. except OSError:
  30. return None
  31. try:
  32. git_hash = subprocess.check_output(
  33. ["git", "rev-parse", "--short", "HEAD"],
  34. stderr=subprocess.STDOUT,
  35. universal_newlines=True,
  36. ).strip()
  37. except subprocess.CalledProcessError:
  38. git_hash = "unknown"
  39. except OSError:
  40. return None
  41. try:
  42. # Check if there are any modified files.
  43. subprocess.check_call(
  44. ["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], stderr=subprocess.STDOUT
  45. )
  46. # Check if there are any staged files.
  47. subprocess.check_call(
  48. ["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], stderr=subprocess.STDOUT
  49. )
  50. except subprocess.CalledProcessError:
  51. git_hash += "-dirty"
  52. except OSError:
  53. return None
  54. return git_tag, git_hash
  55. def get_version_info_from_docs_conf():
  56. with open(os.path.join(os.path.dirname(sys.argv[0]), "..", "docs", "conf.py")) as f:
  57. for line in f:
  58. if line.startswith("version = release = '"):
  59. ver = line.strip().split(" = ")[2].strip("'")
  60. git_tag = "v" + ver
  61. return git_tag, "<no hash>"
  62. return None
  63. def make_version_header(filename):
  64. # Get version info using git, with fallback to docs/conf.py
  65. info = get_version_info_from_git()
  66. if info is None:
  67. info = get_version_info_from_docs_conf()
  68. git_tag, git_hash = info
  69. # Generate the file with the git and version info
  70. file_data = """\
  71. // This file was generated by py/makeversionhdr.py
  72. #define MICROPY_GIT_TAG "%s"
  73. #define MICROPY_GIT_HASH "%s"
  74. #define MICROPY_BUILD_DATE "%s"
  75. """ % (
  76. git_tag,
  77. git_hash,
  78. datetime.date.today().strftime("%Y-%m-%d"),
  79. )
  80. # Check if the file contents changed from last time
  81. write_file = True
  82. if os.path.isfile(filename):
  83. with open(filename, "r") as f:
  84. existing_data = f.read()
  85. if existing_data == file_data:
  86. write_file = False
  87. # Only write the file if we need to
  88. if write_file:
  89. print("GEN %s" % filename)
  90. with open(filename, "w") as f:
  91. f.write(file_data)
  92. if __name__ == "__main__":
  93. make_version_header(sys.argv[1])