From af42e3bcd06a4ea7068d46b40b3ab65bec05b790 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Tue, 31 Dec 2019 12:03:23 -0700 Subject: [PATCH] add pre-commit; add line-count functions & badge --- .pre-commit-config.yaml | 11 ++++++++ Pipfile | 1 + Pipfile.lock | 44 ++++++++++++++++++++++++++++- README.md | 2 +- develop.py | 34 ++++++++++++++++++++++ hooks.sh | 10 +++++++ line_count.svg | 23 +++++++++++++++ manage.py | 62 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 develop.py create mode 100755 hooks.sh create mode 100644 line_count.svg diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..da09588 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,11 @@ +repos: + # - repo: https://github.com/pre-commit/pre-commit-hooks + # rev: v2.3.0 + # hooks: + # - id: flake8 + - repo: local + hooks: + - id: line_count + name: Line Count + entry: hooks.sh + language: script diff --git a/Pipfile b/Pipfile index fe5bb4d..c2d090f 100644 --- a/Pipfile +++ b/Pipfile @@ -26,6 +26,7 @@ flake8-print = "*" flake8-return = "*" pep8-naming = "*" flake8-docstrings = "*" +pre-commit = "*" [packages] aredis = "==1.1.5" diff --git a/Pipfile.lock b/Pipfile.lock index 645f727..25e5d31 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "ec1e8f837bd15e6e5867287b1cb9d37cf9795490ec3717899c706e918b6b33d9" + "sha256": "a99eac0076ede424ed44b8be6eb1694cc358a78593fe373aa2372084c8699d92" }, "pipfile-spec": 6, "requires": { @@ -609,6 +609,13 @@ ], "version": "==1.4.3" }, + "aspy.yaml": { + "hashes": [ + "sha256:463372c043f70160a9ec950c3f1e4c3a82db5fca01d334b6bc89c7164d744bdc", + "sha256:e7c742382eff2caed61f87a39d13f99109088e5e93f04d76eb8d4b28aa143f45" + ], + "version": "==1.3.0" + }, "attrs": { "hashes": [ "sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c", @@ -632,6 +639,13 @@ "index": "pypi", "version": "==19.3b0" }, + "cfgv": { + "hashes": [ + "sha256:edb387943b665bf9c434f717bf630fa78aecd53d5900d2e05da6ad6048553144", + "sha256:fbd93c9ab0a523bf7daec408f3be2ed99a980e20b2d19b50fc184ca6b820d289" + ], + "version": "==2.0.1" + }, "click": { "hashes": [ "sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13", @@ -800,6 +814,13 @@ ], "version": "==3.0.5" }, + "identify": { + "hashes": [ + "sha256:6f44e637caa40d1b4cb37f6ed3b262ede74901d28b1cc5b1fc07360871edd65d", + "sha256:72e9c4ed3bc713c7045b762b0d2e2115c572b85abfc1f4604f5a4fd4c6642b71" + ], + "version": "==1.4.9" + }, "importlib-metadata": { "hashes": [ "sha256:073a852570f92da5f744a3472af1b61e28e9f78ccf0c9117658dc32b15de7b45", @@ -833,6 +854,12 @@ ], "version": "==8.0.2" }, + "nodeenv": { + "hashes": [ + "sha256:ad8259494cf1c9034539f6cced78a1da4840a4b157e23640bc4a0c0546b0cb7a" + ], + "version": "==1.3.3" + }, "pbr": { "hashes": [ "sha256:139d2625547dbfa5fb0b81daebb39601c478c21956dc57e2e07b74450a8c506b", @@ -848,6 +875,14 @@ "index": "pypi", "version": "==0.9.1" }, + "pre-commit": { + "hashes": [ + "sha256:9f152687127ec90642a2cc3e4d9e1e6240c4eb153615cb02aa1ad41d331cbb6e", + "sha256:c2e4810d2d3102d354947907514a78c5d30424d299dc0fe48f5aa049826e9b50" + ], + "index": "pypi", + "version": "==1.20.0" + }, "pycodestyle": { "hashes": [ "sha256:95a2219d12372f05704562a14ec30bc76b05a5b297b21a5dfe3f6fac3491ae56", @@ -928,6 +963,13 @@ ], "version": "==0.10.0" }, + "virtualenv": { + "hashes": [ + "sha256:0d62c70883c0342d59c11d0ddac0d954d0431321a41ab20851facf2b222598f3", + "sha256:55059a7a676e4e19498f1aad09b8313a38fcc0cdbe4fdddc0e9b06946d21b4bb" + ], + "version": "==16.7.9" + }, "zipp": { "hashes": [ "sha256:3718b1cbcd963c7d4c5511a8240812904164b7f381b647143a89d3b98f9bcd8e", diff --git a/README.md b/README.md index cc57c43..6a56532 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ This branch is *very much* under construction. There's a 99% chance that hypergl [**Documentation**](https://hyperglass.readthedocs.io)   |   [**Screenshots**](https://hyperglass.readthedocs.io/en/latest/screenshots/)   |   [**Live Demo**](https://hyperglass.allroads.io/) [![Build Status](https://travis-ci.org/checktheroads/hyperglass.svg?branch=master)](https://travis-ci.org/checktheroads/hyperglass) -![GitHub issues](https://img.shields.io/github/issues/checktheroads/hyperglass.svg) +![LOC](https://raw.githubusercontent.com/checktheroads/hyperglass/v1.0.0/line_count.svg?sanitize=true) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black) diff --git a/develop.py b/develop.py new file mode 100644 index 0000000..62d9961 --- /dev/null +++ b/develop.py @@ -0,0 +1,34 @@ +"""Devloper functions.""" + +import os + + +def count_lines(directory): + """Count lines of code. + + Arguments: + directory {str} -- Path to count + + Returns: + {int} -- Line count + """ + lines = 0 + excluded = ("\n",) + for thing in os.listdir(directory): + thing = os.path.join(directory, thing) + if os.path.isfile(thing): + if thing.endswith(".py"): + with open(thing, "r") as f: + readlines = [ + line + for line in f.readlines() + if line not in excluded and not line.startswith("#") + ] + lines += len(readlines) + + for thing in os.listdir(directory): + thing = os.path.join(directory, thing) + if os.path.isdir(thing): + lines += count_lines(thing) + + return lines diff --git a/hooks.sh b/hooks.sh new file mode 100755 index 0000000..050cf95 --- /dev/null +++ b/hooks.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +LC=$(./manage.py line-count-badge) + +echo $LC + +if [[ ! $? == 0 ]]; then + exit 1 +fi +exit 0 \ No newline at end of file diff --git a/line_count.svg b/line_count.svg new file mode 100644 index 0000000..7f08606 --- /dev/null +++ b/line_count.svg @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + Lines of Code + Lines of Code + + + 3051 + 3051 + + \ No newline at end of file diff --git a/manage.py b/manage.py index 0945e49..584d10d 100755 --- a/manage.py +++ b/manage.py @@ -842,5 +842,67 @@ def generate_secret(length): ) +@hg.command("line-count", help="Get line count for source code.") +@click.option( + "-d", + "--directory", + type=str, + default="hyperglass", + help="Source code directory", +) +def line_count(directory): + """Get lines of code. + + Arguments: + directory {str} -- Source code directory + """ + from develop import count_lines + + count = count_lines(directory) + click.echo( + NL + + click.style("Line Count: ", fg="blue") + + click.style(str(count), fg="green", bold=True) + + NL + ) + + +@hg.command("line-count-badge", help="Generates line count badge") +@click.option( + "-d", + "--directory", + type=str, + default="hyperglass", + help="Source code directory", +) +def line_count_badge(directory): + """Generate shields.io-like badge for lines of code. + + Arguments: + directory {str} -- Source code directory + + Returns: + {int} -- Exit status + """ + import anybadge + from develop import count_lines + + this_dir = Path.cwd() + file_name = "line_count.svg" + badge_file = this_dir / file_name + + if badge_file.exists(): + badge_file.unlink() + + count = count_lines(directory) + badge = anybadge.Badge(label="Lines of Code", value=count, default_color="#007ec6") + badge.write_badge(file_name) + click.echo( + click.style("Created line count badge. Lines: ", fg="white") + + click.style(str(count), fg="green", bold=True) + ) + return 0 + + if __name__ == "__main__": hg()