-
Notifications
You must be signed in to change notification settings - Fork 563
/
Copy pathpatch_cursor_get_machine_id.py
302 lines (236 loc) · 8.35 KB
/
patch_cursor_get_machine_id.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import logging
import os
import platform
import re
import shutil
import sys
import tempfile
from typing import Tuple
# 配置日志
def setup_logging() -> logging.Logger:
"""配置并返回logger实例"""
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s - %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
logger = setup_logging()
def get_cursor_paths() -> Tuple[str, str]:
"""
根据不同操作系统获取 Cursor 相关路径
Returns:
Tuple[str, str]: (package.json路径, main.js路径)的元组
Raises:
OSError: 当找不到有效路径或系统不支持时抛出
"""
system = platform.system()
paths_map = {
"Darwin": {
"base": "/Applications/Cursor.app/Contents/Resources/app",
"package": "package.json",
"main": "out/main.js",
},
"Windows": {
"base": os.path.join(
os.getenv("LOCALAPPDATA", ""), "Programs", "Cursor", "resources", "app"
),
"package": "package.json",
"main": "out/main.js",
},
"Linux": {
"bases": ["/opt/Cursor/resources/app", "/usr/share/cursor/resources/app"],
"package": "package.json",
"main": "out/main.js",
},
}
if system not in paths_map:
raise OSError(f"不支持的操作系统: {system}")
if system == "Linux":
for base in paths_map["Linux"]["bases"]:
pkg_path = os.path.join(base, paths_map["Linux"]["package"])
if os.path.exists(pkg_path):
return (pkg_path, os.path.join(base, paths_map["Linux"]["main"]))
raise OSError("在 Linux 系统上未找到 Cursor 安装路径")
base_path = paths_map[system]["base"]
return (
os.path.join(base_path, paths_map[system]["package"]),
os.path.join(base_path, paths_map[system]["main"]),
)
def check_system_requirements(pkg_path: str, main_path: str) -> bool:
"""
检查系统要求
Args:
pkg_path: package.json 文件路径
main_path: main.js 文件路径
Returns:
bool: 检查是否通过
"""
for file_path in [pkg_path, main_path]:
if not os.path.isfile(file_path):
logger.error(f"文件不存在: {file_path}")
return False
if not os.access(file_path, os.W_OK):
logger.error(f"没有文件写入权限: {file_path}")
return False
return True
def version_check(version: str, min_version: str = "", max_version: str = "") -> bool:
"""
版本号检查
Args:
version: 当前版本号
min_version: 最小版本号要求
max_version: 最大版本号要求
Returns:
bool: 版本号是否符合要求
"""
version_pattern = r"^\d+\.\d+\.\d+$"
try:
if not re.match(version_pattern, version):
logger.error(f"无效的版本号格式: {version}")
return False
def parse_version(ver: str) -> Tuple[int, ...]:
return tuple(map(int, ver.split(".")))
current = parse_version(version)
if min_version and current < parse_version(min_version):
logger.error(f"版本号 {version} 小于最小要求 {min_version}")
return False
if max_version and current > parse_version(max_version):
logger.error(f"版本号 {version} 大于最大要求 {max_version}")
return False
return True
except Exception as e:
logger.error(f"版本检查失败: {str(e)}")
return False
def modify_main_js(main_path: str) -> bool:
"""
修改 main.js 文件
Args:
main_path: main.js 文件路径
Returns:
bool: 修改是否成功
"""
try:
# 获取原始文件的权限和所有者信息
original_stat = os.stat(main_path)
original_mode = original_stat.st_mode
original_uid = original_stat.st_uid
original_gid = original_stat.st_gid
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp_file:
with open(main_path, "r", encoding="utf-8") as main_file:
content = main_file.read()
# 执行替换
patterns = {
r"async getMachineId\(\)\{return [^??]+\?\?([^}]+)\}": r"async getMachineId(){return \1}",
r"async getMacMachineId\(\)\{return [^??]+\?\?([^}]+)\}": r"async getMacMachineId(){return \1}",
}
for pattern, replacement in patterns.items():
content = re.sub(pattern, replacement, content)
tmp_file.write(content)
tmp_path = tmp_file.name
# 使用 shutil.copy2 保留文件权限
shutil.copy2(main_path, main_path + ".old")
shutil.move(tmp_path, main_path)
# 恢复原始文件的权限和所有者
os.chmod(main_path, original_mode)
if os.name != "nt": # 在非Windows系统上设置所有者
os.chown(main_path, original_uid, original_gid)
logger.info("文件修改成功")
return True
except Exception as e:
logger.error(f"修改文件时发生错误: {str(e)}")
if "tmp_path" in locals():
os.unlink(tmp_path)
return False
def backup_files(pkg_path: str, main_path: str) -> bool:
"""
备份原始文件
Args:
pkg_path: package.json 文件路径(未使用)
main_path: main.js 文件路径
Returns:
bool: 备份是否成功
"""
try:
# 只备份 main.js
if os.path.exists(main_path):
backup_main = f"{main_path}.bak"
shutil.copy2(main_path, backup_main)
logger.info(f"已备份 main.js: {backup_main}")
return True
except Exception as e:
logger.error(f"备份文件失败: {str(e)}")
return False
def restore_backup_files(pkg_path: str, main_path: str) -> bool:
"""
恢复备份文件
Args:
pkg_path: package.json 文件路径(未使用)
main_path: main.js 文件路径
Returns:
bool: 恢复是否成功
"""
try:
# 只恢复 main.js
backup_main = f"{main_path}.bak"
if os.path.exists(backup_main):
shutil.copy2(backup_main, main_path)
logger.info(f"已恢复 main.js")
return True
logger.error("未找到备份文件")
return False
except Exception as e:
logger.error(f"恢复备份失败: {str(e)}")
return False
def patch_cursor_get_machine_id(restore_mode=False) -> None:
"""
主函数
Args:
restore_mode: 是否为恢复模式
"""
logger.info("开始执行脚本...")
try:
# 获取路径
pkg_path, main_path = get_cursor_paths()
# 检查系统要求
if not check_system_requirements(pkg_path, main_path):
sys.exit(1)
if restore_mode:
# 恢复备份
if restore_backup_files(pkg_path, main_path):
logger.info("备份恢复完成")
else:
logger.error("备份恢复失败")
return
# 获取版本号
try:
with open(pkg_path, "r", encoding="utf-8") as f:
version = json.load(f)["version"]
logger.info(f"当前 Cursor 版本: {version}")
except Exception as e:
logger.error(f"无法读取版本号: {str(e)}")
sys.exit(1)
# 检查版本
if not version_check(version, min_version="0.45.0"):
logger.error("版本不符合要求(需 >= 0.45.x)")
sys.exit(1)
logger.info("版本检查通过,准备修改文件")
# 备份文件
if not backup_files(pkg_path, main_path):
logger.error("文件备份失败,终止操作")
sys.exit(1)
# 修改文件
if not modify_main_js(main_path):
sys.exit(1)
logger.info("脚本执行完成")
except Exception as e:
logger.error(f"执行过程中发生错误: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
patch_cursor_get_machine_id()