著作权代码收集工具

使用Python编写,可以设置源码目录,排除目录,收集文件类型,输出目标文件,是否允许空白,编码等。

使用示例:

python zhuzuoquan.py -s D:\pythoncode\projectname \
-e debug,release \
-t .h,.cpp \
-en gb18030 \
-o result.txt

zhuzuoquan.py 代码

#!/usr/bin/python
# -*- coding: utf-8 -*-

import argparse
import os

parser = argparse.ArgumentParser(
    description='著作权代码收集工具', usage='''
    zhuzuoquan.py -s 源码目录 -e 排除目录 -t 文件类型 -o 输出文件
    ''')
parser.add_argument('-s', help='源文件目录', required=True)
parser.add_argument('-e', help='排除目录,多个用半角逗号分割', )
parser.add_argument('-t', help='扩展名,多个用半角逗号分割', required=True)
parser.add_argument('-o', help='输出文件')
parser.add_argument('-el', help='允许空白行')
parser.add_argument('-en', help='编码', default='utf-8')

def filter_files(root_dir, excluded_dirs, allow_types):
    valid_files = []
    for root, dirs, files in os.walk(root_dir):

        for f in files:
            excluded = False
            p = os.path.join(root,f)
            d = p[root_dir.__len__():]
            for ex in excluded_dirs:
                if d.startswith(ex):
                    excluded = True

            allow = False
            for t in allow_types:
                if f.endswith(t):
                    allow = True
            if allow and not excluded:
                filepath = os.path.join(root, f)
                valid_files.append(filepath)
    return valid_files

if __name__ == '__main__':
    args = parser.parse_args()

    if args.e:
        excluded_dirs = args.e.split(',')
    else:
        excluded_dirs = []

    allow_types = args.t.split(',')

    root_dir = args.s

    output_file = args.o

    allow_empty_line = args.el

    encoding = args.en

    files = filter_files(root_dir, excluded_dirs, allow_types)
    total_files = total_lines = 0
    if output_file:
        with open(output_file, encoding=encoding, mode='w') as op:
            op.write("")

    for f in files:
        total_files = total_files + 1
        try:
            with open(f, encoding=encoding) as fh:
                content = fh.read()
                if not allow_empty_line:
                    content = "\n".join([s for s in content.splitlines() if s.strip()])
                lines = len(content.splitlines())
                relative_path = f[root_dir.__len__():]
                print(relative_path, lines)
                total_lines = total_lines + lines
                if output_file:
                    fh.seek(0)
                    with open(output_file, encoding=encoding, mode='a+') as op:
                        op.write(relative_path + ":\n\n" + content + "\n\n\n")
        except:
            pass

    print("Total: files %d, lines %d" % (total_files, total_lines))