-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
102 lines (80 loc) · 2.47 KB
/
main.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
import os
from sublime import (
get_sublime_path,
get_sublime_exec_path,
get_sublime_exec_bak_path,
get_sublime_version,
apply_license
)
from patterns import (
read_patterns,
has_patterns,
write_patterns
)
_DIR = os.path.dirname(__file__)
_OS_NAME = "windows" if os.name == "nt" else "linux"
def main():
# Gathering Sublime Text info
sublime_dir = get_sublime_path()
sublime_exec = get_sublime_exec_path()
sublime_exec_bak = get_sublime_exec_bak_path()
if not os.path.exists(sublime_exec):
print("[Error] Unable to locate Sublime Text executable path in {}".format(
sublime_dir
))
return
sublime_version = get_sublime_version()
print("[Sublime Text %s]\nLocation: %s\n" % (
sublime_version,
sublime_exec
))
# Loading HEX patterns
patterns_file = os.path.join(_DIR, "patterns.json")
print("Loading HEX patterns for Sublime Text... ", end="")
try:
patterns = read_patterns(patterns_file)
except FileNotFoundError:
print("Failed")
return
print("OK")
if sublime_version not in patterns or (_OS_NAME not in patterns[sublime_version]):
print("[Error] Patterns for Sublime Text %s (%s) not found" % (
sublime_version, _OS_NAME
))
return
_patterns = patterns[sublime_version][_OS_NAME]
print("Verifying patterns for Sublime Text %s... " % sublime_version, end="")
with open(sublime_exec, "rb") as file:
data = file.read()
if not has_patterns(data, list(_patterns.keys())):
if has_patterns(data, list(_patterns.values())):
print("OK")
print("Sublime Text executable already has patches applied")
return
print("Failed")
return
print("OK")
print("Backing up %s... " % sublime_exec, end="")
try:
with open(sublime_exec_bak, "wb") as file:
file.write(data)
except PermissionError:
print("Permission denied")
return
print("OK")
print("Patching %s..." % sublime_exec)
data_new = write_patterns(data, _patterns)
print("Saving changes... ", end="")
try:
with open(sublime_exec, "wb") as file:
file.write(data_new)
except PermissionError:
print("Permission denied")
return
print("OK")
print("Applying license... ", end="")
apply_license()
print("OK")
print("Done.")
if __name__ == '__main__':
main()