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.

124 lines
3.2 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 re
  15. from subprocess import call, check_output
  16. try:
  17. from urllib.parse import urlparse
  18. except ImportError:
  19. from urlparse import urlparse
  20. from fileinput import FileInput
  21. # https://github.com/python/cpython/commit/6cb7b659#diff-78790b53ff259619377058acd4f74672
  22. if sys.version_info[0] < 3:
  23. class FileInputCtx(FileInput):
  24. def __enter__(self):
  25. return self
  26. def __exit__(self, type, value, traceback):
  27. self.close()
  28. FileInput = FileInputCtx
  29. def run(cmd, cwd=None):
  30. out = check_output(cmd, cwd=cwd)
  31. out = out.decode("latin1").strip()
  32. return out
  33. def parse_h_string(define, r_quotes=re.compile("\"(.*)\"")):
  34. string = r_quotes.search(define).group(1)
  35. return string
  36. def git_parse_remote(cwd=None, remote="origin"):
  37. remote_url = run([
  38. "git", "config", "--local",
  39. "--get", "remote.{}.url".format(remote)], cwd)
  40. if remote_url.startswith("git"):
  41. _, _, repo = remote_url.partition(":")
  42. path = repo.replace(".git", "")
  43. elif remote_url.startswith("https"):
  44. parsed = urlparse(remote_url)
  45. path = parsed.path[1:]
  46. return path.split("/")
  47. def git_branch(cwd=None):
  48. return run(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd)
  49. def espurna_get_version(base, version_h="code/espurna/config/version.h"):
  50. version = "unknown"
  51. path = os.path.join(base, version_h)
  52. with open(path, "r") as version_f:
  53. for line in version_f:
  54. if line.startswith("#define") and "APP_VERSION" in line:
  55. version = parse_h_string(line)
  56. break
  57. return version
  58. TEMPLATES = {
  59. "![travis]": "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \
  60. "(https://travis-ci.org/{USER}/{REPO})\n",
  61. "![version]": "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)\n",
  62. "![branch]": "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)]" \
  63. "(https://github.com/{USER}/{REPO}/tree/{BRANCH}/)\n"
  64. }
  65. README = "README.md"
  66. if __name__ == "__main__":
  67. base = os.getcwd()
  68. user, repo = git_parse_remote()
  69. fmt = {
  70. "USER": user,
  71. "REPO": repo,
  72. "BRANCH": git_branch(),
  73. "VERSION": espurna_get_version(base)
  74. }
  75. templates = [
  76. (k, tmpl.format(**fmt))
  77. for k, tmpl in TEMPLATES.items()
  78. ]
  79. def fmt_line(line):
  80. for match, tmpl in templates:
  81. if match in line:
  82. return tmpl
  83. return line
  84. path = os.path.join(base, README)
  85. with FileInput(path, inplace=True) as readme:
  86. for line in readme:
  87. sys.stdout.write(fmt_line(line))
  88. if call(["git", "add", README]):
  89. sys.exit(1)
  90. sys.exit(0);