-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrecode.py
82 lines (69 loc) · 2.23 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
# -*- coding: utf-8 -*-
import io, os, re, 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);
def find_git_config():
pwd = os.getcwd();
while os.path.isdir(pwd):
path = os.path.join(pwd, ".git", "config");
if os.path.isfile(path):
return path;
pwd = pwd + os.path.sep + "..";
return None;
def load_git_config():
git_config = find_git_config();
if git_config == None:
return None;
lines = open(git_config).readlines();
find_section = False;
for line in lines:
line = line.strip(" \t\r\n");
if line == "[remote \"origin\"]":
find_section = True;
elif find_section:
tokens = line.split("=");
return tokens[1].strip();
return None;
rep_name = load_git_config();
if rep_name == None:
sys.exit("没找到.git配置");
sign = list();
sign.append(u"/*");
sign.append(u"** repository: %s" % rep_name);
sign.append(u"** trumanzhao, %s, [email protected]" % today);
sign.append(u"*/");
sign.append(u"");
def sign_file(path):
recode(path);
lines = io.open(path, "r", encoding="utf-8-sig").readlines();
if len(lines) > 2 and re.match(".+repository.+github.+", lines[1]):
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);