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.

140 lines
3.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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 re
  14. import string
  15. import sys
  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)]"
  69. "(CHANGELOG.md)\n",
  70. "![branch]": "[![branch](https://img.shields.io/badge/branch-{BRANCH:escape_hyphen}-orange.svg)]"
  71. "(https://github.com/{USER}/{REPO}/tree/{BRANCH}/)\n",
  72. }
  73. README = "README.md"
  74. if __name__ == "__main__":
  75. base = os.getcwd()
  76. user, repo = git_parse_remote()
  77. fmt = {
  78. "USER": user,
  79. "REPO": repo,
  80. "BRANCH": git_branch(),
  81. "VERSION": espurna_get_version(base),
  82. }
  83. formatter = CustomFormatter()
  84. templates = [
  85. (k, formatter.format(tmpl, **fmt))
  86. for k, tmpl in TEMPLATES.items()
  87. ]
  88. def fmt_line(line):
  89. for match, tmpl in templates:
  90. if match in line:
  91. return tmpl
  92. return line
  93. path = os.path.join(base, README)
  94. with FileInput(path, inplace=True) as readme:
  95. for line in readme:
  96. sys.stdout.write(fmt_line(line))
  97. if call(["git", "add", README]):
  98. sys.exit(1)
  99. sys.exit(0)