Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format Python code with psf/black push #6

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions example/git-quality-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
git_all_branches,
set_output,
format_number,
parse_inputs
parse_inputs,
)

from git_quality_check.indicators.counters import (
count_old_branches,
count_coupled,
process_logs
process_logs,
)


Expand All @@ -25,28 +25,26 @@

if __name__ == "__main__":


bad_words, main_branches = parse_inputs()

logs = git_logs()
branches = git_all_branches()

bad_commit_index = process_logs(logs, [not_a_squashed_commit,
is_empty_body,
count_bad_words(bad_words)])
bad_commit_index = process_logs(
logs, [not_a_squashed_commit, is_empty_body, count_bad_words(bad_words)]
)
test_index = process_logs(logs, [is_test_commit])

old_branches_index = count_old_branches(branches)
coupling_index = count_coupled(branches, main_branches)

print(bad_commit_index)
print(test_index)
print(old_branches_index)
print(coupling_index)

overall = compute_score(
bad_commit_index, test_index, old_branches_index, coupling_index
)


overall = compute_score(bad_commit_index, test_index,
old_branches_index, coupling_index)

set_output(format_number(overall))
set_output(format_number(overall))
2 changes: 1 addition & 1 deletion git_quality_check/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = 'v0.0-beta'
__version__ = "v0.0-beta"
2 changes: 1 addition & 1 deletion git_quality_check/indicators/commits/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .is_empty_body import is_empty_body
from .not_a_squashed_commit import not_a_squashed_commit
from .count_bad_words import count_bad_words
from .is_test_commit import is_test_commit
from .is_test_commit import is_test_commit
3 changes: 2 additions & 1 deletion git_quality_check/indicators/commits/count_bad_words.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ def _count_bad_words(log: str):
if word in log.lower().split():
counter += 1
return counter
return _count_bad_words

return _count_bad_words
3 changes: 2 additions & 1 deletion git_quality_check/indicators/commits/is_empty_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
remove_header,
)


def is_empty_body(log: str):
if not is_valid_log(log):
return 1
log = remove_header(log)
if not is_valid_log(log):
return 1
return 0
return 0
2 changes: 1 addition & 1 deletion git_quality_check/indicators/commits/is_test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ def is_test_commit(log: str):
for word in ["test", "testing"]:
if word in log.lower().split():
return 1
return 0
return 0
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def not_a_squashed_commit(log: str):
if "(#" not in log:
return 1
return 0
return 0
2 changes: 1 addition & 1 deletion git_quality_check/indicators/counters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .count_old_branches import count_old_branches
from .count_coupled import count_coupled
from .process_logs import process_logs
from .process_logs import process_logs
3 changes: 2 additions & 1 deletion git_quality_check/indicators/counters/count_coupled.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from git_quality_check.utils import sample, are_coupled


def count_coupled(branches, main_branches):
branches, count = sample(branches, 10)
branches.extend(main_branches)
Expand All @@ -8,4 +9,4 @@ def count_coupled(branches, main_branches):
for bA in branches:
for bB in branches:
counter += 1 if are_coupled(bA, bB) else 0
return counter / count * 100
return counter / count * 100
8 changes: 3 additions & 5 deletions git_quality_check/indicators/counters/count_old_branches.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from git_quality_check.utils import (
sample,
is_old
)
from git_quality_check.utils import sample, is_old


def count_old_branches(branches):
counter = 0
branches, count = sample(branches, 10)
for branch in branches:
counter += 1 if is_old(branch) else 0
return counter / count * 100
return counter / count * 100
4 changes: 2 additions & 2 deletions git_quality_check/indicators/counters/process_logs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
def process_logs(logs: list[str], functions: list[str: int]):
def process_logs(logs: list[str], functions: list[str:int]):
counter = 0
count = len(logs) * len(functions)
for i in range(len(logs)):
log = logs[i]
for function in functions:
counter += function(log)
return counter / count * 100
return counter / count * 100
2 changes: 1 addition & 1 deletion git_quality_check/scoring/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .overall import compute_score
from .overall import compute_score
12 changes: 7 additions & 5 deletions git_quality_check/scoring/overall.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
def compute_score(bad_commit_index, test_index,
old_branches_index, coupling_index):
return ((100 - bad_commit_index) +
test_index + (100 - old_branches_index) +
(100 - coupling_index))/4
def compute_score(bad_commit_index, test_index, old_branches_index, coupling_index):
return (
(100 - bad_commit_index)
+ test_index
+ (100 - old_branches_index)
+ (100 - coupling_index)
) / 4
7 changes: 1 addition & 6 deletions git_quality_check/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,4 @@
git_all_branches,
)

from .common import (
sample,
set_output,
format_number,
parse_inputs
)
from .common import sample, set_output, format_number, parse_inputs
15 changes: 9 additions & 6 deletions git_quality_check/utils/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import random
import os
from datetime import datetime
Expand All @@ -17,26 +16,30 @@ def parse_inputs():
main_branches = ["origin/develop", "origin/master"]
return bad_words, main_branches


def strip(s: str):
return s.strip().lstrip()


def get_date():
return datetime.today()


def sample(li: list[str], min: int):
count = len(li)
if(count > min):
if count > min:
li = random.sample(li, min)
count = min
return (li, count)

def format_number(number:float):

def format_number(number: float):
return "{0:.2f}".format(number)

def set_output(output:str):

def set_output(output: str):
print(f"::set-output name=score::{output}")


def diff_month(d1, d2):
return (d1.year - d2.year) * 12 + d1.month - d2.month


35 changes: 21 additions & 14 deletions git_quality_check/utils/git.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
import subprocess
from datetime import datetime
from .common import (
get_date,
diff_month,
strip
)
from .common import get_date, diff_month, strip


def is_valid_log(log: str):
return not log == ""


def run_git(command: list[str]):
command.insert(0, "--no-pager")
command.insert(0, "git")
return subprocess.check_output(command).decode()


def git_logs():
return run_git(["log"]).split("commit ")


def is_old(branch):
branch_date = git_get_branch_date(branch)
if not branch_date:
return False
date = get_date()
return diff_month(date, branch_date) > 2


def git_all_branches():
ret = run_git(["branch", "-r"]).split("\n")
return [strip(r) for r in ret if not strip(r) == '']
return [strip(r) for r in ret if not strip(r) == ""]


def are_coupled(branchA: str, branchB: str):
if not is_well_formed_branch(branchA) or\
not is_well_formed_branch(branchB):
if not is_well_formed_branch(branchA) or not is_well_formed_branch(branchB):
return False
if branchA == branchB:
return False
Expand All @@ -44,31 +45,37 @@ def are_coupled(branchA: str, branchB: str):
return True
return False

def is_well_formed_branch(branch:str):

def is_well_formed_branch(branch: str):
return not "->" in branch

def git_get_branch_date(branch:str):

def git_get_branch_date(branch: str):
if not is_well_formed_branch(branch):
return None
ret = run_git(["log", "-n", "1", "--date=format:\"%Y-%m-%d\"", branch]).split("Date: ")[1]
ret = ret.replace("\"", "").split("-")
ret = run_git(["log", "-n", "1", '--date=format:"%Y-%m-%d"', branch]).split(
"Date: "
)[1]
ret = ret.replace('"', "").split("-")
year = int(strip(ret[0]))
month = int(ret[1])
day = int(ret[2].split("\n")[0])
return datetime(year, month, day)


def remove_first_line(log: str):
if is_valid_log:
try:
eol = log.index("\n")
log = log[eol + 1:]
log = log[eol + 1 :]
except ValueError:
# commit is empty or just contains one line
return ""
return log


def remove_header(log: str):
log = remove_first_line(log) # Hash
log = remove_first_line(log) # Author
log = remove_first_line(log) # Date
return log
return log
53 changes: 27 additions & 26 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,37 @@

# get the version (don't import mne here, so dependencies are not needed)
version = None
with open(op.join('git_quality_check', '_version.py'), 'r') as fid:
with open(op.join("git_quality_check", "_version.py"), "r") as fid:
for line in (line.strip() for line in fid):
if line.startswith('__version__'):
version = line.split('=')[1].strip().strip('\'')
if line.startswith("__version__"):
version = line.split("=")[1].strip().strip("'")
break
if version is None:
raise RuntimeError('Could not determine version')
raise RuntimeError("Could not determine version")

with open('README.md', 'r', encoding="utf8") as fid:
with open("README.md", "r", encoding="utf8") as fid:
long_description = fid.read()

setup(name='git-quality-check',
version=version,
description='A simple tool to evalute the quality of a git repository.',
url='https://github.com/gcattan/git-quality-check',
author='Gregoire Cattan',
author_email='[email protected]',
license='BSD (3-clause)',
packages=find_packages(),
long_description=long_description,
long_description_content_type='text/markdown',
project_urls={
'Documentation': 'https://github.com/gcattan/git-quality-check',
'Source': 'https://github.com/gcattan/git-quality-check',
'Tracker': 'https://github.com/gcattan/git-quality-check/issues/',
},
platforms='any',
python_requires=">=3.9",
install_requires=[],
# extras_require={'docs': ['sphinx-gallery', 'sphinx-bootstrap_theme', 'numpydoc', 'mne', 'seaborn'],
# 'tests': ['pytest', 'seaborn', 'flake8', 'mne', 'pooch', 'tqdm']},
zip_safe=False,
setup(
name="git-quality-check",
version=version,
description="A simple tool to evalute the quality of a git repository.",
url="https://github.com/gcattan/git-quality-check",
author="Gregoire Cattan",
author_email="[email protected]",
license="BSD (3-clause)",
packages=find_packages(),
long_description=long_description,
long_description_content_type="text/markdown",
project_urls={
"Documentation": "https://github.com/gcattan/git-quality-check",
"Source": "https://github.com/gcattan/git-quality-check",
"Tracker": "https://github.com/gcattan/git-quality-check/issues/",
},
platforms="any",
python_requires=">=3.9",
install_requires=[],
# extras_require={'docs': ['sphinx-gallery', 'sphinx-bootstrap_theme', 'numpydoc', 'mne', 'seaborn'],
# 'tests': ['pytest', 'seaborn', 'flake8', 'mne', 'pooch', 'tqdm']},
zip_safe=False,
)