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.

132 lines
3.6 KiB

6 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 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. "![codacy]": "[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/{BRANCH}.svg)]" \
  65. "(https://www.codacy.com/app/{USER}/{REPO}/dashboard)\n"
  66. }
  67. README = "README.md"
  68. if __name__ == "__main__":
  69. base = os.getcwd()
  70. user, repo = git_parse_remote()
  71. fmt = {
  72. "USER": user,
  73. "REPO": repo,
  74. "BRANCH": git_branch(),
  75. "VERSION": espurna_get_version(base)
  76. }
  77. templates = [
  78. (k, tmpl.format(**fmt))
  79. for k, tmpl in TEMPLATES.items()
  80. ]
  81. def fmt_line(line):
  82. for match, tmpl in templates:
  83. if match in line:
  84. return tmpl
  85. return line
  86. path = os.path.join(base, README)
  87. with FileInput(path, inplace=True) as readme:
  88. for line in readme:
  89. sys.stdout.write(fmt_line(line))
  90. if call(["git", "add", README]):
  91. sys.exit(1)
  92. os.chdir("code")
  93. if call(["node", "node_modules/gulp/bin/gulp.js", "--silent"]):
  94. sys.exit(2)
  95. call(["git", "add", "code/espurna/data/index*"])
  96. call(["git", "add", "code/espurna/static/index*"])
  97. sys.exit(0);