Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.5 KiB

  1. #!/usr/bin/env python
  2. """
  3. Referencing current branch in github README.md [1]
  4. This pre-commit hook [2] updates the README.md file's
  5. Travis badge with the current branch. Based on [4].
  6. [1] http://stackoverflow.com/questions/18673694/referencing-current-branch-in-github-readme-md
  7. [2] http://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
  8. [3] https://docs.travis-ci.com/user/status-images/
  9. [4] https://gist.github.com/dandye/dfe0870a6a1151c89ed9
  10. Copy this file to .git/hooks/
  11. """
  12. import os
  13. import sys
  14. import string
  15. import re
  16. from subprocess import call, check_output
  17. try:
  18. from urllib.parse import urlparse
  19. except ImportError:
  20. from urlparse import urlparse
  21. from fileinput import FileInput
  22. # https://github.com/python/cpython/commit/6cb7b659#diff-78790b53ff259619377058acd4f74672
  23. if sys.version_info[0] < 3:
  24. class FileInputCtx(FileInput):
  25. def __enter__(self):
  26. return self
  27. def __exit__(self, type, value, traceback):
  28. self.close()
  29. FileInput = FileInputCtx
  30. class CustomFormatter(string.Formatter):
  31. def format_field(self, value, spec):
  32. if spec == "escape_hyphen":
  33. return value.replace("-", "--")
  34. else:
  35. return super(CustomFormatter, self).format_field(value, spec)
  36. def run(cmd, cwd=None):
  37. out = check_output(cmd, cwd=cwd)
  38. out = out.decode("latin1").strip()
  39. return out
  40. def parse_h_string(define, r_quotes=re.compile("\"(.*)\"")):
  41. string = r_quotes.search(define).group(1)
  42. return string
  43. def git_parse_remote(cwd=None, remote="origin"):
  44. remote_url = run([
  45. "git", "config", "--local",
  46. "--get", "remote.{}.url".format(remote)], cwd)
  47. if remote_url.startswith("git"):
  48. _, _, repo = remote_url.partition(":")
  49. path = repo.replace(".git", "")
  50. elif remote_url.startswith("https"):
  51. parsed = urlparse(remote_url)
  52. path = parsed.path[1:]
  53. return path.split("/")
  54. def git_branch(cwd=None):
  55. return run(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd)
  56. def espurna_get_version(base, version_h="code/espurna/config/version.h"):
  57. version = "unknown"
  58. path = os.path.join(base, version_h)
  59. with open(path, "r") as version_f:
  60. for line in version_f:
  61. if line.startswith("#define") and "APP_VERSION" in line:
  62. version = parse_h_string(line)
  63. break
  64. return version
  65. TEMPLATES = {
  66. "![travis]": "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]"
  67. "(https://travis-ci.org/{USER}/{REPO})\n",
  68. "![version]": "[![version](https://img.shields.io/badge/version-{VERSION:escape_hyphen}-brightgreen.svg)](CHANGELOG.md)\n",
  69. "![branch]": "[![branch](https://img.shields.io/badge/branch-{BRANCH:escape_hyphen}-orange.svg)]"
  70. "(https://github.com/{USER}/{REPO}/tree/{BRANCH}/)\n",
  71. }
  72. README = "README.md"
  73. if __name__ == "__main__":
  74. base = os.getcwd()
  75. user, repo = git_parse_remote()
  76. fmt = {
  77. "USER": user,
  78. "REPO": repo,
  79. "BRANCH": git_branch(),
  80. "VERSION": espurna_get_version(base),
  81. }
  82. formatter = CustomFormatter()
  83. templates = [
  84. (k, formatter.format(tmpl, **fmt))
  85. for k, tmpl in TEMPLATES.items()
  86. ]
  87. def fmt_line(line):
  88. for match, tmpl in templates:
  89. if match in line:
  90. return tmpl
  91. return line
  92. path = os.path.join(base, README)
  93. with FileInput(path, inplace=True) as readme:
  94. for line in readme:
  95. sys.stdout.write(fmt_line(line))
  96. if call(["git", "add", README]):
  97. sys.exit(1)
  98. sys.exit(0)