【问题标题】:Is there a tool that shows a distribution of lines of code per file of a folder?是否有一个工具可以显示文件夹中每个文件的代码行分布?
【发布时间】:2022-01-07 22:23:14
【问题描述】:

我想知道我的存储库中文件的代码行数有多大,以了解存储库的“健康状况”。

为了回答这个问题,我想查看特定范围(可以是 1)的文件数量的分布(可视化与否):

#lines of code   #files   
 1-10             1
11-20             23
etc...

(这样的直方图会很好)

是否有快速获取此信息的原因,例如使用 cloc 或任何其他(命令行)工具?

【问题讨论】:

    标签: visualization cloc


    【解决方案1】:

    cloc 和 Pandas 的组合可以处理这个问题。首先,使用--by-file--csv 开关将cloc 的行数捕获到一个csv 文件中,例如

    cloc --by-file --csv --out data.csv curl-7.80.0.tar.bz2
    

    然后使用下面的 Python 程序按文件夹聚合和分箱数据:

    ./aggregate_by_folder.py data.csv
    

    aggregate_by_folder.py 的代码是

    #!/usr/bin/env python
    import sys
    import os.path
    import pandas as pd
    def add_folder(df):
        """
        Return a Pandas dataframe with an additional 'folder' column
        containing each file's parent directory
        """
        header = 'github.com/AlDanial/cloc'
        df = df.drop(df.columns[df.columns.str.contains(header)], axis=1)
        df['folder'] = df['filename'].dropna().apply(os.path.dirname)
        return df
    
    def bin_by_folder(df):
        bins = list(range(0,1000,50))
        return df.groupby('folder')['code'].value_counts(bins=bins).sort_index()
    
    def file_count_by_folder(df):
        df_files = pd.pivot_table(df, index=['folder'], aggfunc='count')
        file_counts = df_files.rename(columns={'blank':'file count'})
        return file_counts[['file count']]
    
    def main():
        if len(sys.argv) != 2:
            print(f"Usage:  {sys.argv[0]} data.csv")
            print("     where the .csv file is created with")
            print("       cloc --by-file --csv --out data.csv my_code_base")
            raise SystemExit
        pd.set_option('display.max_rows', None)
        pd.set_option('display.width', None)
        pd.set_option('display.max_colwidth', -1)
        df = add_folder(pd.read_csv(sys.argv[1]))
        print(pd.pivot_table(df, index=['folder'], aggfunc='sum'))
        print('-' * 50)
        print(file_count_by_folder(df))
        print('-' * 50)
        print(bin_by_folder(df))
    
    if __name__ == "__main__": main()
    

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      相关资源
      最近更新 更多