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.4 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. from __future__ import print_function
  13. import os
  14. import sys
  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. def run(cmd, cwd=None):
  31. out = check_output(cmd, cwd=cwd)
  32. out = out.decode("latin1").strip()
  33. return out
  34. def parse_h_string(define, r_quotes=re.compile("\"(.*)\"")):
  35. string = r_quotes.search(define).group(1)
  36. return string
  37. def git_parse_remote(cwd=None, remote="origin"):
  38. remote_url = run([
  39. "git", "config", "--local",
  40. "--get", "remote.{}.url".format(remote)], cwd)
  41. if remote_url.startswith("git"):
  42. _, _, repo = remote_url.partition(":")
  43. path = repo.replace(".git", "")
  44. elif remote_url.startswith("https"):
  45. parsed = urlparse(remote_url)
  46. path = parsed.path[1:]
  47. return path.split("/")
  48. def git_branch(cwd=None):
  49. return run(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd)
  50. def espurna_get_version(base, version_h="code/espurna/config/version.h"):
  51. version = "unknown"
  52. path = os.path.join(base, version_h)
  53. with open(path, "r") as version_f:
  54. for line in version_f:
  55. if line.startswith("#define") and "APP_VERSION" in line:
  56. version = parse_h_string(line)
  57. break
  58. return version
  59. TEMPLATES = {
  60. "![travis]": "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \
  61. "(https://travis-ci.org/{USER}/{REPO})\n",
  62. "![version]": "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)\n",
  63. "![branch]": "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)]" \
  64. "(https://github.org/{USER}/{REPO}/tree/{BRANCH}/)\n",
  65. "![codacy]": "[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/{BRANCH}.svg)]" \
  66. "(https://www.codacy.com/app/{USER}/{REPO}/dashboard)\n"
  67. }
  68. README = "README.md"
  69. if __name__ == "__main__":
  70. base = os.getcwd()
  71. user, repo = git_parse_remote()
  72. fmt = {
  73. "USER": user,
  74. "REPO": repo,
  75. "BRANCH": git_branch(),
  76. "VERSION": espurna_get_version(base)
  77. }
  78. templates = [
  79. (k, tmpl.format(**fmt))
  80. for k, tmpl in TEMPLATES.items()
  81. ]
  82. def fmt_line(line):
  83. for match, tmpl in templates:
  84. if match in line:
  85. return tmpl
  86. return line
  87. path = os.path.join(base, README)
  88. with FileInput(path, inplace=True) as readme:
  89. for line in readme:
  90. print(fmt_line(line), end='')
  91. sys.exit(call(["git", "add", README]))