-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathContextSearch.py
More file actions
161 lines (118 loc) · 4.73 KB
/
ContextSearch.py
File metadata and controls
161 lines (118 loc) · 4.73 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
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
#!/usr/bin/python3 -u
# Note that running python with the `-u` flag is required on Windows,
# in order to ensure that stdin and stdout are opened in binary, rather
# than text, mode.
import json
import sys
import struct
import os
import logging
__version__ = "2.20"
BINARY_URL = "https://raw.githubusercontent.com/ssborbis/ContextSearch-Native-App/master/ContextSearch.py"
VERSION_URL = "https://raw.githubusercontent.com/ssborbis/ContextSearch-Native-App/master/version.json"
logging.basicConfig(
filename='python.log',
encoding='utf-8',
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
# Read a message from stdin and decode it.
def get_message():
raw_length = sys.stdin.buffer.read(4)
if not raw_length:
sys.exit(0)
message_length = struct.unpack('=I', raw_length)[0]
message = sys.stdin.buffer.read(message_length).decode("utf-8")
return json.loads(message)
# Encode a message for transmission, given its content.
def encode_message(message_content):
encoded_content = json.dumps(message_content).encode("utf-8")
encoded_length = struct.pack('=I', len(encoded_content))
# use struct.pack("10s", bytes), to pack a string of the length of 10 characters
return {'length': encoded_length, 'content': struct.pack(str(len(encoded_content))+"s",encoded_content)}
# Send an encoded message to stdout.
def send_message(encoded_message):
sys.stdout.buffer.write(encoded_message['length'])
sys.stdout.buffer.write(encoded_message['content'])
sys.stdout.buffer.flush()
def check_for_update():
import urllib.request
response = urllib.request.urlopen(VERSION_URL)
js = json.loads(response.read().decode("utf-8"))
latest_version = js["version"]
if ( float(latest_version) > float(__version__)):
return latest_version
else:
return False
def update():
import urllib.request
response = urllib.request.urlopen(BINARY_URL)
remote_script = response.read().decode("utf-8")
with open(os.path.realpath(__file__), 'w') as f:
f.write(remote_script);
def download(url, dest):
import urllib.request
import urllib.error
import cgi
remotefile = urllib.request.urlopen(url)
content = remotefile.info()['Content-Disposition']
if content:
value, params = cgi.parse_header(content)
filename = os.path.join(dest, params["filename"])
else:
filename = os.path.join(dest, os.path.basename(url))
urllib.request.urlretrieve(url, filename)
return filename
try:
# receive nativeMessage
message = get_message()
if message.get("verify"):
send_message(encode_message(True))
sys.exit(0)
elif message.get("version"):
send_message(encode_message(__version__))
sys.exit(0)
elif message.get("checkForUpdate"):
send_message(encode_message(check_for_update()))
sys.exit(0)
elif message.get("update"):
update()
send_message(encode_message(True))
sys.exit(0)
# use python to fetch remote content
if message.get("downloadURL"):
tmpdir = None
if message.get("downloadFolder") and os.path.isdir(os.path.expanduser(message.get("downloadFolder"))):
tmpdir = os.path.expanduser(message.get("downloadFolder"))
else:
import tempfile
tmpdir = tempfile.gettempdir()
filename = download(message.get("downloadURL"), tmpdir)
message["path"] = message["path"].replace("{download_url}", filename)
# execute shell command
if message.get("path"):
import subprocess
message["path"] = message["path"].replace("$", "\$")
cwd = message.get("cwd") or os.getcwd()
cwd = os.path.expanduser(cwd)
import shlex
cmd = shlex.split(message["path"])
if message["return_stdout"]:
output = subprocess.check_output(message["path"], cwd=cwd, shell=True).decode()
send_message(encode_message(output))
else:
if sys.platform == "win32":
CREATE_NEW_PROCESS_GROUP = 0x00000200
DETACHED_PROCESS = 0x00000008
CREATE_NEW_CONSOLE = 0x00000010
CREATE_BREAKAWAY_FROM_JOB = 0x01000000
subprocess.run(message["path"], cwd=cwd, shell=True, creationflags=CREATE_BREAKAWAY_FROM_JOB, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL )
else:
subprocess.run(message["path"], cwd=cwd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
send_message(encode_message(True))
sys.exit(0)
# no valid requests
send_message(encode_message(False))
sys.exit(1)
except Exception as e:
logging.error(e)