forked from dearlulu/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecode.py
61 lines (50 loc) · 1.62 KB
/
recode.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import io, os, shutil, sys
import chardet
import codecs
import datetime
#注意需要安装chardet模块
today = datetime.datetime.now().strftime("%Y-%m-%d");
#检测文件编码,如果不是的话,统一改为utf-8
#将table转换为space
def recode(path):
raw = open(path, 'rb').read();
if raw.startswith(codecs.BOM_UTF8):
encoding = 'utf-8-sig'
else:
result = chardet.detect(raw)
encoding = result['encoding']
lines = io.open(path, "r", encoding=encoding).readlines();
for i in range(0, len(lines)):
lines[i] = lines[i].rstrip().expandtabs(4) + "\n";
io.open(path, "w", encoding="utf-8-sig").writelines(lines);
sign = list();
sign.append(u"/*");
sign.append(u"** repository: https://github.com/trumanzhao/luna");
sign.append(u"** trumanzhao, %s, [email protected]" % today);
sign.append(u"*/");
sign.append(u"");
def signed(lines):
if len(lines) < len(sign):
return False;
if lines[0].rstrip() == sign[0] and lines[1].rstrip() == sign[1]:
return True;
return False;
def sign_file(path):
recode(path);
lines = io.open(path, "r", encoding="utf-8-sig").readlines();
if signed(lines):
print("%s 已签名!" % path);
return;
for i in range(0, len(sign)):
lines.insert(i, sign[i] + u"\n");
print("签名: %s" % path);
io.open(path, "w", encoding="utf-8").writelines(lines);
root = ".";
items = os.listdir(root);
for item in items:
path = os.path.join(root, item);
ext = os.path.splitext(path)[1].lower();
if ext == ".cpp" or ext == ".h":
sign_file(path);