From cf2b1d678889eef6b6f755e6817449fff0b5ab6f Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sun, 22 Apr 2018 08:58:37 +0300 Subject: [PATCH 1/5] Use python from PATH --- pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pre-commit b/pre-commit index 90640b70..00a1b221 100755 --- a/pre-commit +++ b/pre-commit @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python """ Referencing current branch in github README.md [1] From 4b135ed65b4a2bb775554e1212bc56c7aa2b2c00 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sun, 22 Apr 2018 09:00:42 +0300 Subject: [PATCH 2/5] Make branch shield clickable --- README.md | 2 +- pre-commit | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6dc1d4b8..2eb670c9 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ESPurna ("spark" in Catalan) is a custom firmware for ESP8285/ESP8266 based smar It uses the Arduino Core for ESP8266 framework and a number of 3rd party libraries. [![version](https://img.shields.io/badge/version-1.12.6a-brightgreen.svg)](CHANGELOG.md) -![branch](https://img.shields.io/badge/branch-dev-orange.svg) +[![branch](https://img.shields.io/badge/branch-dev-orange.svg)](https://github.com/xoseperez/espurna/tree/dev/) [![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=dev)](https://travis-ci.org/xoseperez/espurna) [![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/dev.svg)](https://www.codacy.com/app/xoseperez/espurna/dashboard) [![license](https://img.shields.io/github/license/xoseperez/espurna.svg)](LICENSE) diff --git a/pre-commit b/pre-commit index 00a1b221..2fa07a7e 100755 --- a/pre-commit +++ b/pre-commit @@ -41,7 +41,10 @@ version = "[![version](https://img.shields.io/badge/version-{VERSION}-brightgree VERSION = VERSION ) -branch = "![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)\n".format( +branch = "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)] " + "(https://github.com/{USER}/{REPO}/tree/{BRANCH}/)\n".format( + USER = USER, + REPO = REPO, BRANCH = BRANCH ) From 180ff97916aae73c28bd30b978e01b41b084bac8 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sun, 22 Apr 2018 09:01:59 +0300 Subject: [PATCH 3/5] Reworked pre-commit hook - Fix cwd assumptions, pre-commit always starts at the repo base - Fix subprocess calls for python3 - Extract repository info from 'git config' - Refactored into separate functions and __main__ part - Use fileinput module to modify README --- pre-commit | 171 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 110 insertions(+), 61 deletions(-) diff --git a/pre-commit b/pre-commit index 2fa07a7e..48ccb8e7 100755 --- a/pre-commit +++ b/pre-commit @@ -13,68 +13,117 @@ Travis badge with the current branch. Based on [4]. Copy this file to .git/hooks/ """ +from __future__ import print_function import os import sys import re -import subprocess - -BASE = os.path.dirname(os.path.realpath(__file__)) + "/../../" -README = BASE + "README.md" -remote = subprocess.check_output(["git", "remote", "-v"]).strip().split('\n')[0] -parts = re.split('[/\.: ]', remote) -REPO = parts[ len(parts) - 3] -USER = parts[ len(parts) - 4] -BRANCH = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).strip() - -def getVersion(): - file_name = BASE + "code/espurna/config/version.h" - lines = open(file_name).readlines() - for line in lines: - if "APP_VERSION" in line: - parts = line.split('"') - return parts[1] - return "unknown" -VERSION = getVersion() - -version = "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)\n".format( - VERSION = VERSION -) - -branch = "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)] " - "(https://github.com/{USER}/{REPO}/tree/{BRANCH}/)\n".format( - USER = USER, - REPO = REPO, - BRANCH = BRANCH -) - -travis = "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \ - "(https://travis-ci.org/{USER}/{REPO})\n".format( - USER = USER, - REPO = REPO, - BRANCH = BRANCH -) - -codacy = "[![codacy](https://img.shields.io/codacy/grade/{HASH}/{BRANCH}.svg)]" \ - "(https://www.codacy.com/app/{USER}/{REPO}/dashboard)\n".format( - HASH = "c9496e25cf07434cba786b462cb15f49", - USER = USER, - REPO = REPO, - BRANCH = BRANCH -) - -lines = open(README).readlines() -with open(README, "w") as fh: - for line in lines: - if "![travis]" in line: - fh.write(travis) - elif "![version]" in line: - fh.write(version) - elif "![branch]" in line: - fh.write(branch) - elif "![codacy]" in line: - fh.write(codacy) - else: - fh.write(line) - -subprocess.check_output(["git", "add", README ]) + +from subprocess import call, check_output +try: + from urllib.parse import urlparse +except ImportError: + from urlparse import urlparse + +from fileinput import FileInput +# https://github.com/python/cpython/commit/6cb7b659#diff-78790b53ff259619377058acd4f74672 +if sys.version_info[0] < 3: + class FileInputCtx(FileInput): + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.close() + + FileInput = FileInputCtx + + +def run(cmd, cwd=None): + out = check_output(cmd, cwd=cwd) + out = out.decode("latin1").strip() + + return out + + +def parse_h_string(define, r_quotes=re.compile("\"(.*)\"")): + string = r_quotes.search(define).group(1) + return string + + +def git_parse_remote(cwd=None, remote="origin"): + remote_url = run([ + "git", "config", "--local", + "--get", "remote.{}.url".format(remote)], cwd) + + if remote_url.startswith("git"): + _, _, repo = remote_url.partition(":") + path = repo.replace(".git", "") + elif remote_url.startswith("https"): + parsed = urlparse(remote_url) + path = parsed.path[1:] + return path.split("/") + + +def git_branch(cwd=None): + return run(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd) + + +def espurna_get_version(base, version_h="code/espurna/config/version.h"): + version = "unknown" + + path = os.path.join(base, version_h) + with open(path, "r") as version_f: + for line in version_f: + if line.startswith("#define") and "APP_VERSION" in line: + version = parse_h_string(line) + break + + return version + + +SHIELD_TRAVIS = "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \ + "(https://travis-ci.org/{USER}/{REPO})" + +SHIELD_VERSION = "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)" + +SHIELD_BRANCH = "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)]" \ + "(https://github.org/{USER}/{REPO}/tree/{BRANCH}/)" + +SHIELD_CODACY = "[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/{BRANCH}.svg)]" \ + "(https://www.codacy.com/app/{USER}/{REPO}/dashboard)" + +README = "README.md" + + +if __name__ == "__main__": + base = os.getcwd() + + user, repo = git_parse_remote() + template = { + "USER": user, + "REPO": repo, + "BRANCH": git_branch(), + "VERSION": espurna_get_version(base) + } + + shield_travis = SHIELD_TRAVIS.format(**template) + shield_version = SHIELD_VERSION.format(**template) + shield_branch = SHIELD_BRANCH.format(**template) + shield_codacy = SHIELD_CODACY.format(**template) + + path = os.path.join(base, README) + + with FileInput(path, inplace=True) as readme: + for line in readme: + if "![travis]" in line: + print(shield_travis) + elif "![version]" in line: + print(shield_version) + elif "![branch]" in line: + print(shield_branch) + elif "![codacy]" in line: + print(shield_codacy) + else: + print(line, end="") + + sys.exit(call(["git", "add", README], cwd=base)) From 5ae00f992d56530a76df4be03df610c90f8d9d0c Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sun, 22 Apr 2018 13:35:39 +0300 Subject: [PATCH 4/5] Never print newline --- pre-commit | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/pre-commit b/pre-commit index 48ccb8e7..c79b9ec1 100755 --- a/pre-commit +++ b/pre-commit @@ -80,17 +80,15 @@ def espurna_get_version(base, version_h="code/espurna/config/version.h"): return version - -SHIELD_TRAVIS = "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \ - "(https://travis-ci.org/{USER}/{REPO})" - -SHIELD_VERSION = "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)" - -SHIELD_BRANCH = "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)]" \ - "(https://github.org/{USER}/{REPO}/tree/{BRANCH}/)" - -SHIELD_CODACY = "[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/{BRANCH}.svg)]" \ - "(https://www.codacy.com/app/{USER}/{REPO}/dashboard)" +TEMPLATES = { + "![travis]": "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \ + "(https://travis-ci.org/{USER}/{REPO})\n", + "![version]": "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)\n", + "![branch]": "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)]" \ + "(https://github.org/{USER}/{REPO}/tree/{BRANCH}/)\n", + "![codacy]": "[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/{BRANCH}.svg)]" \ + "(https://www.codacy.com/app/{USER}/{REPO}/dashboard)\n" +} README = "README.md" @@ -99,31 +97,28 @@ if __name__ == "__main__": base = os.getcwd() user, repo = git_parse_remote() - template = { + fmt = { "USER": user, "REPO": repo, "BRANCH": git_branch(), "VERSION": espurna_get_version(base) } + templates = [ + (k, tmpl.format(**fmt)) + for k, tmpl in TEMPLATES.items() + ] + + def fmt_line(line): + for match, tmpl in templates: + if match in line: + return tmpl - shield_travis = SHIELD_TRAVIS.format(**template) - shield_version = SHIELD_VERSION.format(**template) - shield_branch = SHIELD_BRANCH.format(**template) - shield_codacy = SHIELD_CODACY.format(**template) + return line path = os.path.join(base, README) with FileInput(path, inplace=True) as readme: for line in readme: - if "![travis]" in line: - print(shield_travis) - elif "![version]" in line: - print(shield_version) - elif "![branch]" in line: - print(shield_branch) - elif "![codacy]" in line: - print(shield_codacy) - else: - print(line, end="") - - sys.exit(call(["git", "add", README], cwd=base)) + print(fmt_line(line), end='') + + sys.exit(call(["git", "add", README])) From 072c491da6386654905b710e48b41e998b3a5cac Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sun, 22 Apr 2018 22:45:32 +0300 Subject: [PATCH 5/5] Use wrapped stdout directly --- pre-commit | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pre-commit b/pre-commit index c79b9ec1..acc37a69 100755 --- a/pre-commit +++ b/pre-commit @@ -13,7 +13,6 @@ Travis badge with the current branch. Based on [4]. Copy this file to .git/hooks/ """ -from __future__ import print_function import os import sys @@ -119,6 +118,6 @@ if __name__ == "__main__": with FileInput(path, inplace=True) as readme: for line in readme: - print(fmt_line(line), end='') + sys.stdout.write(fmt_line(line)) sys.exit(call(["git", "add", README]))