python 用于将标头添加到源文件顶部的Python脚本。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用于将标头添加到源文件顶部的Python脚本。相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python3

# add headers to .h, .cpp, .c and .ino files automatically.
# the script will first list the files missing headers, then
# prompt for confirmation. The modification is made inplace.
#
# usage:
#   add-headers.py <header file> <root dir>
#
# The script will first read the header template in <header file>,
# then scan for source files recursively from <root dir>.

import sys, os
import os.path as path
import fileinput

src_extensions = ['.h', '.cpp', '.c', '.ino']

def is_src_file(f):
    results = [f.endswith(ext) for ext in src_extensions]
    return True in results

def is_header_missing(f):
    with open(f) as reader:
        lines = reader.read().lstrip().splitlines()
        if len(lines) > 0: return not lines[0].startswith("/**")
        return True

def get_src_files(dirname):
    src_files = []
    for cur, _dirs, files in os.walk(dirname):
        [src_files.append(path.join(cur,f)) for f in files if is_src_file(f)]

    return [f for f in src_files if is_header_missing(f)]

def add_headers(files, header):
    for line in fileinput.input(files, inplace=True):
        if fileinput.isfirstline():
            [ print(h) for h in header.splitlines() ]
        print(line, end="")



if __name__ == "__main__":

    if len(sys.argv) < 3:
        print("usage: %s <header file> <root dir>" % sys.argv[0])
        exit()

    args = [path.abspath(arg) for arg in sys.argv]
    root_path = path.abspath(args[2])

    header = open(args[1]).read()
    files = get_src_files(root_path)

    print("Files with missing headers:")
    [print("  - %s" % f) for f in files]

    print()
    print("Header: ")
    print(header)

    confirm = input("proceed ? [y/N] ")
    if confirm is not "y": exit(0)

    add_headers(files, header)

以上是关于python 用于将标头添加到源文件顶部的Python脚本。的主要内容,如果未能解决你的问题,请参考以下文章

C# 创建 MIME 标头信息? (内容类型等)

如何将标头(用于 jenkins crumb)添加到 github webhook?

位于顶部的 UITableView 标头

我应该将哪个 Python intrpetor 放入 .py 标头中?

Guzzle HTTP - 将授权标头直接添加到请求中

如何使用GhostScript避免tiff上的标头截断。将ps转换为tiff