-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator-copyright.py
217 lines (157 loc) · 6.92 KB
/
generator-copyright.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-08-26
# @copyright by hoojo@2018
# @changelog Added Generator Copyright Information to file python
#===============================================================================
# Generator Copyright Information
#===============================================================================
# 描述:生成 copyright 信息,插入到文件中
#-------------------------------------------------------------------------------
import time
import os
#-------------------------------------------------------------------------------
# global env
#-------------------------------------------------------------------------------
'''
targetFolder="F:\\Example Exercise\\Python"
fiilter="py"
COPYRIGHT_INFORMATION = ''#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
%s
# @copyright by hoojo@2018
# @changelog %s
'''
targetFolder="F:\\Example Exercise\\Bash"
fiilter="sh"
COPYRIGHT_INFORMATION = '''#!/bin/bash
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: %s
# @copyright by hoojo@2018
# @changelog %s
'''
#-------------------------------------------------------------------------------
# fetch comment keyword
#-------------------------------------------------------------------------------
SKIP_PREFIXS = [ "#!/usr", "# -*-", "#-*-", "# encoding", "# @author", "#!/bin/" ]
def fetchCommentKeyword():
for comment in COPYRIGHT_INFORMATION.splitlines():
keywords = comment.split(" ")
if len(keywords) >= 2:
SKIP_PREFIXS.append(keywords[1])
#-------------------------------------------------------------------------------
# fetch file latest update date
#-------------------------------------------------------------------------------
def fetchUpdatedDate(file):
st = os.stat(file)
#print('st_ctime: ', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(st.st_ctime)))
#print('st_mtime: ', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(st.st_mtime)))
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(st.st_ctime))
#-------------------------------------------------------------------------------
# skip comment line content
#-------------------------------------------------------------------------------
def skipLine(line):
for item in SKIP_PREFIXS:
if line.find(item) != -1:
return True
return False
#-------------------------------------------------------------------------------
# Read a file content, filter comment
#-------------------------------------------------------------------------------
def read(targetFile):
contentLines = []
with open(targetFile, "r", encoding=u'utf-8') as file:
text = file.readline()
while text != "":
if text.startswith("#"):
if skipLine(text):
#print("remove text: %s" % text)
pass
else:
contentLines.append(text)
else:
contentLines.append(text)
text = file.readline()
file.close()
return contentLines
#-------------------------------------------------------------------------------
# Write transform content to file
#-------------------------------------------------------------------------------
def write(targetFile, contentLines):
with open(targetFile, "w", encoding=u'utf-8') as file:
file.writelines(contentLines)
file.close()
#-------------------------------------------------------------------------------
# clean comment after empty line
#-----------------------------------------------------------------------------
def cleanEmptyLine(contentLines):
emptyLine, skip = 0, False
for line in contentLines:
if (line == "\n" or line == "\r" or line == "\r\n") and skip == False:
emptyLine = emptyLine + 1
else:
skip = True
if emptyLine >= 3:
return contentLines[emptyLine - 3:]
return contentLines
#-------------------------------------------------------------------------------
# insert comment to read file line arrays
#-------------------------------------------------------------------------------
def insertComment(targetFile, contentLines):
updateDate = fetchUpdatedDate(targetFile)
changelog = generatorChangelog(targetFile)
copyright = COPYRIGHT_INFORMATION % (updateDate, changelog)
contentLines.insert(0, copyright)
return contentLines
#-------------------------------------------------------------------------------
# generator changelog to read file line arrays
#-------------------------------------------------------------------------------
def generatorChangelog(targetFile):
changelog = "Added python3 `%s` example"
latestName = "learn"
if targetFile.find("examples_") != -1:
latestName = targetFile.split("examples_")[1]
elif targetFile.find("samples_") != -1:
latestName = targetFile.split("samples_")[1]
else:
latestName = targetFile
latestName = latestName.replace("_", " ").replace("\/", "->").replace("\\", "->").replace(".py", "").replace(".sh", "")
changelog = changelog % latestName
return changelog
#-------------------------------------------------------------------------------
# each target folder, add comment to python file
#-------------------------------------------------------------------------------
def eachFolder(targetFolder):
for root, dirs, files in os.walk(targetFolder, topdown=False):
print(root)
for name in files:
targetFile = os.path.join(root, name)
if targetFile.find("samples_") == -1 and targetFile.find("example") == -1:
continue
if targetFile.find("pycache") != -1:
continue
if targetFile.endswith(fiilter) == False:
continue
print('files: %s' % targetFile)
contentLines = read(targetFile)
contentLines = cleanEmptyLine(contentLines)
contentLines = insertComment(targetFile, contentLines)
write(targetFile, contentLines)
print("----------------------------------------------")
for name in dirs:
folder = os.path.join(root, name)
print('dirs: %s' % folder)
eachFolder(folder)
#-------------------------------------------------------------------------------
# run application
#-------------------------------------------------------------------------------
fetchCommentKeyword()
eachFolder(targetFolder)