python 统计所有的 仓库 提交者的提交次数

字典去重 YYDS

然后再写入excel 表 yyds

python 复制代码
#!/bin/env python3
from git.repo import Repo
import os
import pandas as pds

path = "/home/labstation/workqueue/sw"
url = "git@10.0.128.128"
date = [str(x) for x in range(202307, 202308)]
datefmt = "%Y%m"
counts = 0
AUTHORS = {}
AUTHORS_UNIQU = {}
DATE = {}
DATE_UNIQU = {}

for proj in os.scandir(path):
    if not os.access(path + "/" + proj.name + "/.git", os.F_OK):
        continue
    repo = Repo(path + "/" + proj.name)
    remoteUrl = repo.git.execute(["git", "remote", "-v"])
    if type(remoteUrl) is str:
        remoteUrl = map(lambda x: x.strip("\t"), remoteUrl.split("\n"))
        for checkUrl in remoteUrl:
            if checkUrl.find(url) > 0:
                continue
    res = repo.git.execute(["git", "branch", "-r"])
    if type(res) is str and len(res) != 0:
        branches = filter(
            lambda x: not x.startswith("origin/HEAD"),
            map(lambda x: x.strip(" "), res.split("\n")),
        )
        for it_branch in branches:
            for it in repo.iter_commits(it_branch):
                if it.authored_datetime.strftime(datefmt) in date:
                    AUTHORS[str(it)] = it.author.name
                    DATE[str(it)] = (
                        it_branch,
                        proj.name,
                        it.author.name,
                        it.authored_datetime.strftime(datefmt),
                    )

for _it in AUTHORS:
    AUTHORS_UNIQU[AUTHORS[_it]] = 0

for _it in AUTHORS_UNIQU:
    for _its in AUTHORS:
        if _it == AUTHORS[_its]:
            AUTHORS_UNIQU[_it] += 1

for _it in DATE:
    DATE_UNIQU[DATE[_it]] = 0

for _it in DATE_UNIQU:
    for _its in DATE:
        if _it == DATE[_its]:
            DATE_UNIQU[_it] += 1

TotalExcel = {"姓名": [], "次数": []}

for _it in AUTHORS_UNIQU:
    TotalExcel["姓名"].append(_it)
    TotalExcel["次数"].append(AUTHORS_UNIQU[_it])

Excel = {"分支": [], "仓库名称": [], "提交者": [], "提交日期": [], "提交次数": []}
for it in DATE_UNIQU:
    Excel["分支"].append(it[0])
    Excel["仓库名称"].append(it[1])
    Excel["提交者"].append(it[2])
    Excel["提交日期"].append(it[3])
    Excel["提交次数"].append(DATE_UNIQU[it])

writer = pds.ExcelWriter(date[0] + "~" + date[len(date) - 1] + ".xlsx")
excekWrute2 = pds.DataFrame(TotalExcel)
excekWrute2.to_excel(writer, sheet_name="统计总表")
execlWrite1 = pds.DataFrame(Excel)
execlWrite1.to_excel(writer, sheet_name="各仓库统计")
writer.close()

保存成excel 表格输出
YYDS ... ...
添加一个绘图功能

python 复制代码
#!/bin/env python3
from git.repo import Repo
import os
import pandas as pds
import matplotlib.pyplot as plt

path = "/home/code/workqueue"
url = "git@10.0.128.128"
date = [str(x) for x in range(20230701, 20230732)]
datefmt = "%Y%m%d"
counts = 0
AUTHORS = {}
AUTHORS_UNIQU = {}
DATE = {}
DATE_UNIQU = {}

for proj in os.scandir(path):
    if not os.access(path + "/" + proj.name + "/.git", os.F_OK):
        continue
    repo = Repo(path + "/" + proj.name)
    remoteUrl = repo.git.execute(["git", "remote", "-v"])
    if type(remoteUrl) is str:
        remoteUrl = map(lambda x: x.strip("\t"), remoteUrl.split("\n"))
        for checkUrl in remoteUrl:
            if checkUrl.find(url) > 0:
                continue
    res = repo.git.execute(["git", "branch", "-r"])
    if type(res) is str and len(res) != 0:
        branches = filter(
            lambda x: not x.startswith("origin/HEAD"),
            map(lambda x: x.strip(" "), res.split("\n")),
        )
        for it_branch in branches:
            for it in repo.iter_commits(it_branch):
                if it.authored_datetime.strftime(datefmt) in date:
                    AUTHORS[str(it)] = it.author.email
                    DATE[str(it)] = (
                        it_branch,
                        proj.name,
                        it.author.email,
                        it.authored_datetime.strftime(datefmt),
                    )

for _it in AUTHORS:
    AUTHORS_UNIQU[AUTHORS[_it]] = 0

for _it in AUTHORS_UNIQU:
    for _its in AUTHORS:
        if _it == AUTHORS[_its]:
            AUTHORS_UNIQU[_it] += 1

for _it in DATE:
    DATE_UNIQU[DATE[_it]] = 0

for _it in DATE_UNIQU:
    for _its in DATE:
        if _it == DATE[_its]:
            DATE_UNIQU[_it] += 1

TotalExcel = {"姓名": [], "次数": []}

for _it in AUTHORS_UNIQU:
    TotalExcel["姓名"].append(_it)
    TotalExcel["次数"].append(AUTHORS_UNIQU[_it])

Excel = {"仓库名称": [], "分支": [], "提交者": [], "提交日期": [], "提交次数": []}
for it in DATE_UNIQU:
    Excel["仓库名称"].append(it[1])
    Excel["分支"].append(it[0])
    Excel["提交者"].append(it[2])
    Excel["提交日期"].append(it[3])
    Excel["提交次数"].append(DATE_UNIQU[it])

repoSheet: dict = {}
for it in DATE_UNIQU:
    repoSheet[it[1]] = {"提交者": [], "分支": [], "提交次数": [], "提交日期": []}

for it in DATE_UNIQU:
    repoSheet[it[1]]["提交者"].append(it[2])
    repoSheet[it[1]]["分支"].append(it[0])
    repoSheet[it[1]]["提交次数"].append(DATE_UNIQU[it])
    repoSheet[it[1]]["提交日期"].append(it[3])

if os.path.exists(date[0] + "~" + date[len(date) - 1] + ".xlsx"):
    os.remove(date[0] + "~" + date[len(date) - 1] + ".xlsx")

writer = pds.ExcelWriter(date[0] + "~" + date[len(date) - 1] + ".xlsx")
excekWrute2 = pds.DataFrame(TotalExcel)
excekWrute2.to_excel(writer, sheet_name="统计总表")
execlWrite1 = pds.DataFrame(Excel)
execlWrite1.to_excel(writer, sheet_name="各仓库统计原始记录")
for it in repoSheet:
    t = pds.DataFrame(repoSheet[it])
    t.to_excel(writer, sheet_name=it)
writer.close()

Total = 0
ax = []
for it in pds.unique(Excel["仓库名称"]):
    Total += 1

_cnts = 0
fig = plt.figure()
for it in pds.unique(Excel["仓库名称"]):
    ax.append(fig.add_subplot(int(Total / 4) + 1, 4, _cnts + 1))
    ax[_cnts].set_title(it)
    # FIXME have some bug
    commitTimes = {}
    for _it in repoSheet[it]["提交者"]:
        commitTimes[_it] = 0

    for i in range(0, len(repoSheet[it]["提交者"])):
        commitTimes[repoSheet[it]["提交者"][i]] += repoSheet[it]["提交次数"][i]
    ax[_cnts].bar(
        list(x for x in commitTimes), list(commitTimes[x] for x in commitTimes)
    )
    _cnts += 1
plt.show()

很有趣的小工具 heartrate

实用的小脚本

python 复制代码
#!/bin/env python3
from git.repo import Repo
import os
import pandas as pds
import matplotlib.pyplot as plt
from rich.console import Console
from rich.table import Table
import time

path = "/home/code/workqueue"
url = "git@10.0.128.128"
date = [str(x) for x in range(20230101, 20230832)]
datefmt = "%Y%m%d"
counts = 0

AUTHORS = {}
AUTHORS_UNIQU = {}
DATE = {}
DATE_UNIQU = {}
SHOW_PLOT = False
SHOW_TABLE = True
SAVE_XLSX = False
AUTHOR_ONE = ["hong_da_yu@163.com"]

for proj in os.scandir(path):
    if not os.access(path + "/" + proj.name + "/.git", os.F_OK):
        continue
    repo = Repo(path + "/" + proj.name)
    remoteUrl = repo.git.execute(["git", "remote", "-v"])
    if type(remoteUrl) is str:
        remoteUrl = map(lambda x: x.strip("\t"), remoteUrl.split("\n"))
        for checkUrl in remoteUrl:
            if checkUrl.find(url) > 0:
                continue
    res = repo.git.execute(["git", "branch", "-r"])
    if type(res) is str and len(res) != 0:
        branches = filter(
            lambda x: not x.startswith("origin/HEAD"),
            map(lambda x: x.strip(" "), res.split("\n")),
        )
        for it_branch in branches:
            for it in repo.iter_commits(it_branch):
                if it.authored_datetime.strftime(datefmt) in date:
                    AUTHORS[str(it)] = it.author.email
                    DATE[str(it)] = (
                        it_branch,
                        proj.name,
                        it.author.email,
                        it.authored_datetime.strftime(datefmt),
                    )

for _it in AUTHORS:
    AUTHORS_UNIQU[AUTHORS[_it]] = 0

for _it in AUTHORS_UNIQU:
    for _its in AUTHORS:
        if _it == AUTHORS[_its]:
            AUTHORS_UNIQU[_it] += 1

for _it in DATE:
    DATE_UNIQU[DATE[_it]] = 0

for _it in DATE_UNIQU:
    for _its in DATE:
        if _it == DATE[_its]:
            DATE_UNIQU[_it] += 1

TotalExcel = {"姓名": [], "次数": []}

for _it in AUTHORS_UNIQU:
    TotalExcel["姓名"].append(_it)
    TotalExcel["次数"].append(AUTHORS_UNIQU[_it])

Excel = {"仓库名称": [], "分支": [], "提交者": [], "提交日期": [], "提交次数": []}
for it in DATE_UNIQU:
    Excel["仓库名称"].append(it[1])
    Excel["分支"].append(it[0])
    Excel["提交者"].append(it[2])
    Excel["提交日期"].append(it[3])
    Excel["提交次数"].append(DATE_UNIQU[it])

repoSheet: dict = {}
for it in DATE_UNIQU:
    repoSheet[it[1]] = {"提交者": [], "分支": [], "提交次数": [], "提交日期": []}

for it in DATE_UNIQU:
    repoSheet[it[1]]["提交者"].append(it[2])
    repoSheet[it[1]]["分支"].append(it[0])
    repoSheet[it[1]]["提交次数"].append(DATE_UNIQU[it])
    repoSheet[it[1]]["提交日期"].append(it[3])

if SAVE_XLSX:
    if os.path.exists(date[0] + "~" + date[len(date) - 1] + ".xlsx"):
        os.remove(date[0] + "~" + date[len(date) - 1] + ".xlsx")

    writer = pds.ExcelWriter(date[0] + "~" + date[len(date) - 1] + ".xlsx")
    excekWrute2 = pds.DataFrame(TotalExcel)
    excekWrute2.to_excel(writer, sheet_name="统计总表")
    execlWrite1 = pds.DataFrame(Excel)
    execlWrite1.to_excel(writer, sheet_name="各仓库统计原始记录")
    for it in repoSheet:
        t = pds.DataFrame(repoSheet[it])
        t.to_excel(writer, sheet_name=it)
    writer.close()

# NOTE: FIX BUG OKAY
if SHOW_PLOT:
    Total = 0
    ax = []
    for it in pds.unique(Excel["仓库名称"]):
        Total += 1
    _cnts = 0
    fig = plt.figure()
    for it in pds.unique(Excel["仓库名称"]):
        ax.append(fig.add_subplot(int(Total / 4) + 1, 4, _cnts + 1))
        ax[_cnts].set_title(it)
        commitTimes = {}
        for _it in repoSheet[it]["提交者"]:
            commitTimes[_it] = 0

        for i in range(0, len(repoSheet[it]["提交者"])):
            commitTimes[repoSheet[it]["提交者"][i]] += repoSheet[it]["提交次数"][i]
        ax[_cnts].bar(
            list(x for x in commitTimes), list(commitTimes[x] for x in commitTimes)
        )
        _cnts += 1
    plt.show()

if SHOW_TABLE:
    table = Table(title=time.strftime("%Y-%m-%d", time.localtime()))
    for it in Excel:
        table.add_column(it, style="cyan", no_wrap=True)

    for it in range(0, len(Excel["仓库名称"])):
        if Excel["提交者"][it] in AUTHOR_ONE:
            table.add_row(
                Excel["仓库名称"][it],
                Excel["分支"][it],
                Excel["提交者"][it],
                Excel["提交日期"][it],
                str(Excel["提交次数"][it]),
            )
    console = Console()
    console.print(table, justify="center")

终端打印 表格 😊

相关推荐
007php0074 小时前
linux服务器上CentOS的yum和Ubuntu包管理工具apt区别与使用实战
linux·运维·服务器·ubuntu·centos·php·ai编程
djykkkkkk4 小时前
ubuntu编译遇到的问题
linux·运维·ubuntu
qq_429856575 小时前
linux 查看服务是否开机自启动
linux·运维·服务器
7yewh6 小时前
Linux驱动开发 IIC I2C驱动 编写APP访问EEPROM AT24C02
linux·arm开发·驱动开发·嵌入式硬件·嵌入式
dessler6 小时前
Docker-Dockerfile讲解(三)
linux·运维·docker
KevinRay_7 小时前
命令行之巅:Linux Shell编程的至高艺术(中)
linux·运维·服务器·重定向·shell编程
程序员JerrySUN7 小时前
Yocto 项目 - 共享状态缓存 (Shared State Cache) 机制
linux·嵌入式硬件·物联网·缓存·系统架构
林农8 小时前
C05S16-MySQL高可用
linux·mysql·云计算
码中小白鼠8 小时前
Ubuntu系统部署Mysql8.0后设置不区分大小写
linux·mysql·ubuntu·adb
gz94568 小时前
Virtualbox安装ubuntu20虚拟机无法打开终端
java·linux·开发语言