-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathupdate_timestamp.py
More file actions
executable file
·130 lines (97 loc) · 3.95 KB
/
Copy pathupdate_timestamp.py
File metadata and controls
executable file
·130 lines (97 loc) · 3.95 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
自动更新 Shadowrocket 配置文件时间戳脚本
作者:Jojo
用途:在每次 git commit 时自动更新配置文件中的时间戳
"""
import os
import re
import sys
from datetime import datetime
import subprocess
def update_timestamp():
"""更新配置文件中的时间戳"""
config_file = "shadowrocket-rules.conf"
# 检查配置文件是否存在
if not os.path.exists(config_file):
print(f"错误: 找不到配置文件 {config_file}")
return False
try:
# 读取配置文件
with open(config_file, 'r', encoding='utf-8') as f:
content = f.read()
# 获取当前时间(北京时间 UTC+8)
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 定义要替换的模式:查找时间戳行
pattern = r'# 最后更新:\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \(自动生成\)'
replacement = f'# 最后更新:{current_time} (自动生成)'
if not re.search(pattern, content):
print("警告: 未找到时间戳行,可能配置文件格式有误")
return False
# 执行替换
new_content = re.sub(pattern, replacement, content)
# 写回文件
with open(config_file, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"✅ 成功更新时间戳为: {current_time}")
# 将更新后的文件添加到 git staging area
try:
subprocess.run(['git', 'add', config_file], check=True, capture_output=True)
print(f"✅ 已将 {config_file} 添加到 git staging area")
except subprocess.CalledProcessError as e:
print(f"警告: 无法将文件添加到 git staging area: {e}")
return True
except Exception as e:
print(f"错误: 更新时间戳失败 - {e}")
return False
def update_timestamp_simplified():
"""更新简化版配置文件中的时间戳"""
config_file = "shadowrocket-rules-simplified.conf"
if not os.path.exists(config_file):
return True # 文件不存在时不视为错误
try:
with open(config_file, 'r', encoding='utf-8') as f:
content = f.read()
current_date = datetime.now().strftime("%Y-%m-%d")
pattern = r'# 最后更新:\d{4}-\d{2}-\d{2} \(简化版本\)'
replacement = f'# 最后更新:{current_date} (简化版本)'
if not re.search(pattern, content):
print("警告: 简化版配置文件未找到时间戳行")
return False
new_content = re.sub(pattern, replacement, content)
with open(config_file, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"✅ 成功更新简化版时间戳为: {current_date}")
try:
subprocess.run(['git', 'add', config_file], check=True, capture_output=True)
print(f"✅ 已将 {config_file} 添加到 git staging area")
except subprocess.CalledProcessError as e:
print(f"警告: 无法将文件添加到 git staging area: {e}")
return True
except Exception as e:
print(f"错误: 更新简化版时间戳失败 - {e}")
return False
def main():
"""主函数"""
print("🔄 开始更新 Shadowrocket 配置文件时间戳...")
result = subprocess.run(
['git', 'diff', '--cached', '--name-only'],
capture_output=True, text=True
)
staged_files = set(result.stdout.strip().splitlines())
success = True
if 'shadowrocket-rules.conf' in staged_files:
if not update_timestamp():
success = False
if 'shadowrocket-rules-simplified.conf' in staged_files:
if not update_timestamp_simplified():
success = False
if success:
print("🎉 时间戳更新完成!")
sys.exit(0)
else:
print("❌ 时间戳更新失败!")
sys.exit(1)
if __name__ == "__main__":
main()