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.

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