forked from sakura-editor/sakura
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddDoxygenFileComment.py
More file actions
73 lines (61 loc) · 1.86 KB
/
Copy pathaddDoxygenFileComment.py
File metadata and controls
73 lines (61 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
import os
import sys
import re
import codecs
# チェック対象の拡張子リスト
extensions = (
".cpp",
".h",
)
# チェック対象の拡張子か判断する
def checkExtension(file):
base, ext = os.path.splitext(file)
if ext in extensions:
return True
else:
return False
# 引数で指定したフォルダ以下のすべての対象ファイルを yield で返す
def checkAll(topDir):
for rootdir, dirs, files in os.walk(topDir):
for file in files:
if checkExtension(file):
full = os.path.join(rootdir, file)
yield full
# 引数で指定した文字列から改行コードを取り除く
def clipEndOfLine(line):
return line.rstrip('\r\n')
# 引数で指定したファイルに対して @file コメントがあるかチェックする
def hasFileComment(file):
with codecs.open(file, "r", "utf_8_sig") as fin:
for line in fin:
match = re.search(r'(\\|@)file\b', line)
if match:
return True
return False
# 引数で指定したファイルに対して @file コメントをつける
def addFileComment(file):
fileComment = "/*! @file */"
endOfLine = "\r\n"
tmp_file = file + ".tmp"
with codecs.open(tmp_file, "w", "utf_8_sig") as fout:
with codecs.open(file, "r", "utf_8_sig") as fin:
# ファイル先頭に @file コメントをつける
fout.write(fileComment + endOfLine)
for line in fin:
text = clipEndOfLine(line)
fout.write(text + endOfLine)
os.remove(file)
os.rename(tmp_file, file)
# 対象のファイルをすべて処理する
# (@file コメントをつける)
def processFiles(files):
for file in files:
if hasFileComment(file) == False:
print ("processing " + file)
addFileComment(file)
if __name__ == '__main__':
if len(sys.argv) < 2:
print ("usage: " + os.path.basename(sys.argv[0]) + " <top dir>")
sys.exit(1)
processFiles(checkAll(sys.argv[1]))