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.

129 lines
3.7 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. SHIELD_TRAVIS = "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \
  60. "(https://travis-ci.org/{USER}/{REPO})"
  61. SHIELD_VERSION = "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)"
  62. SHIELD_BRANCH = "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)]" \
  63. "(https://github.org/{USER}/{REPO}/tree/{BRANCH}/)"
  64. SHIELD_CODACY = "[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/{BRANCH}.svg)]" \
  65. "(https://www.codacy.com/app/{USER}/{REPO}/dashboard)"
  66. README = "README.md"
  67. if __name__ == "__main__":
  68. base = os.getcwd()
  69. user, repo = git_parse_remote()
  70. template = {
  71. "USER": user,
  72. "REPO": repo,
  73. "BRANCH": git_branch(),
  74. "VERSION": espurna_get_version(base)
  75. }
  76. shield_travis = SHIELD_TRAVIS.format(**template)
  77. shield_version = SHIELD_VERSION.format(**template)
  78. shield_branch = SHIELD_BRANCH.format(**template)
  79. shield_codacy = SHIELD_CODACY.format(**template)
  80. path = os.path.join(base, README)
  81. with FileInput(path, inplace=True) as readme:
  82. for line in readme:
  83. if "![travis]" in line:
  84. print(shield_travis)
  85. elif "![version]" in line:
  86. print(shield_version)
  87. elif "![branch]" in line:
  88. print(shield_branch)
  89. elif "![codacy]" in line:
  90. print(shield_codacy)
  91. else:
  92. print(line, end="")
  93. sys.exit(call(["git", "add", README], cwd=base))