makeversionhdr.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. return git_tag, git_hash
  43. def get_version_info_from_docs_conf():
  44. with open(os.path.join(os.path.dirname(sys.argv[0]), "..", "docs", "conf.py")) as f:
  45. for line in f:
  46. if line.startswith("version = release = '"):
  47. ver = line.strip().split(" = ")[2].strip("'")
  48. git_tag = "v" + ver
  49. return git_tag, "<no hash>"
  50. return None
  51. def make_version_header(filename):
  52. # Get version info using git, with fallback to docs/conf.py
  53. info = get_version_info_from_git()
  54. if info is None:
  55. info = get_version_info_from_docs_conf()
  56. git_tag, git_hash = info
  57. # Generate the file with the git and version info
  58. file_data = """\
  59. // This file was generated by py/makeversionhdr.py
  60. #define MICROPY_GIT_TAG "%s"
  61. #define MICROPY_GIT_HASH "%s"
  62. #define MICROPY_BUILD_DATE "%s"
  63. """ % (git_tag, git_hash, datetime.date.today().strftime("%Y-%m-%d"))
  64. # Check if the file contents changed from last time
  65. write_file = True
  66. if os.path.isfile(filename):
  67. with open(filename, 'r') as f:
  68. existing_data = f.read()
  69. if existing_data == file_data:
  70. write_file = False
  71. # Only write the file if we need to
  72. if write_file:
  73. print("GEN %s" % filename)
  74. with open(filename, 'w') as f:
  75. f.write(file_data)
  76. if __name__ == "__main__":
  77. make_version_header(sys.argv[1])