-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.py
More file actions
executable file
·206 lines (169 loc) · 6.1 KB
/
bridge.py
File metadata and controls
executable file
·206 lines (169 loc) · 6.1 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
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
#!/usr/bin/env python3
'''
Used by the chrome extension for running commands on the host
'''
from __future__ import print_function
import struct
import sys
import threading
import os
import errno
import re
import json
import platform
import traceback
from subprocess import Popen, PIPE
EXTENSION_ID = 'knldjmfmopnpolahpmmgbagdohdnhkik'
LOG_FILE = open(sys.argv[0] + '.out', 'w+')
def log(*args, **kwargs):
'''Logs to file'''
print(*args, file=LOG_FILE, **kwargs)
LOG_FILE.flush()
def run(argv, cwd=None, logger=None):
'''
Runs the command, returns exit status.
Each line of output (stdout/stder are merged) is passed to logger()
'''
process = Popen('%s 2>&1' % ' '.join(argv), stdout=PIPE,
cwd=cwd, shell=True, preexec_fn=os.setsid)
while True:
line = process.stdout.readline()
if line == b'' and process.poll() is not None:
break
logger(line.decode('utf-8'))
code = process.poll()
if code != 0:
raise Exception('command failed with exit status %i: %s' %
(code, ' '.join(argv)))
return code
def mkdir_p(path):
'''Like unix `mkdir -p`'''
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno != errno.EEXIST or not os.path.isdir(path):
raise
def start_project(git_url, ide_command):
'''
Launches command, updates the extension on status
'''
try:
def logger(line):
log(line)
send(git_url, 'output', line.strip())
cmd = ide_command % git_url
send(git_url, 'status', 'starting %s' % cmd)
run(['bash', '-c', "'%s'" % cmd], logger=logger)
send(git_url, 'status', 'complete')
except Exception as exc:
log(traceback.format_exc())
send(git_url, 'output', str(exc))
send(git_url, 'status', 'error')
def start_project_with_clone(git_url, ide_command, code_location):
'''
Clones repo, launches command, updates the extension on status
'''
try:
def logger(line):
log(line)
send(git_url, 'output', line.strip())
match = re.compile(r'^.*/([^ .]+)\.git$').match(git_url)
if not match:
raise Exception('unrecognised git url format: %s' % git_url)
repo_name = match.group(1)
src_parent = code_location.replace('%s', os.path.expanduser('~'))
git_dir = os.path.join(src_parent, repo_name)
if not os.path.exists(git_dir):
send(git_url, 'status', 'cloning...')
mkdir_p(src_parent)
run(["git", "clone", git_url], cwd=src_parent, logger=logger)
cmd = ide_command % git_dir
send(git_url, 'status', 'starting %s' % cmd)
run(['bash', '-c', "'%s'" % cmd], logger=logger)
send(git_url, 'status', 'complete')
except Exception as exc:
send(git_url, 'output', str(exc))
send(git_url, 'status', 'error')
def send(target, name, value):
''' Send a message to the extension '''
try:
msg = json.dumps({"target": target, "data": {
name: value}}).encode('utf-8')
sys.stdout.buffer.write(struct.pack("I", len(msg)))
sys.stdout.buffer.write(msg)
sys.stdout.buffer.flush()
except IOError:
log(traceback.format_exc())
def serve():
''' Serves requests from the extension '''
while True:
text_length_bytes = sys.stdin.buffer.read(4)
if len(text_length_bytes) == 0:
break
text_length = struct.unpack('i', text_length_bytes)[0]
text = json.loads(sys.stdin.buffer.read(text_length))
git_url = text['url']
ide_command = text['ideCommand']
# log(f'git_url={git_url} ide_command={ide_command}')
thread = threading.Thread(target=start_project, args=(
git_url, ide_command))
# FIXME thread.daemon = True
thread.start()
def register_script_with_extension(chrome_dir):
'''yes that'''
manifest_template = '''{
"name": "org.hfdom.chrome_github_cloner",
"description": "Clone a repo and Start your IDE from github pages",
"path": "%s",
"type": "stdio",
"allowed_origins": [
"chrome-extension://%s/"
]
}
'''
def generic_install(script_path, manifest_path):
print('Copying self to %s' % script_path)
with open(sys.argv[0], 'rb') as this_script:
content = this_script.read()
with open(script_path, 'wb') as script_copy:
script_copy.write(content)
os.chmod(script_path, 0o700)
manifest = manifest_template % (script_path, EXTENSION_ID)
print('Creating manifest %s\nContent:\n%s' % (manifest_path, manifest))
with open(manifest_path, 'wb') as f:
f.write(manifest.encode('utf-8'))
print('You may now delete the extension directory, unless you intend on making changes')
def unix():
script_path = '%s/.chrome-github-cloner.py' % os.environ['HOME']
manifest_path = '%s/NativeMessagingHosts/org.hfdom.chrome_github_cloner.json' % chrome_dir
generic_install(script_path, manifest_path)
system = platform.system()
registrar = {
'Darwin': unix,
'Linux': unix,
}[system]
if not registrar:
msg = 'does not know how to register script on platform "%s", submit PR' % system
print(msg, file=sys.stderr)
log(msg)
sys.exit(1)
registrar()
if len(sys.argv) == 3 and sys.argv[1] == 'install':
chrome_dir = sys.argv[2]
test_path = '%s/Default' % chrome_dir
if not os.path.isdir(test_path):
print('provided path is probably incorrect, as %s does not exist' % test_path,
file=sys.stderr)
sys.exit(2)
register_script_with_extension(chrome_dir)
sys.exit(0)
if len(sys.argv) == 1 or not sys.argv[1].startswith('chrome-extension:'):
print('Usage: %s install USER_CHROME_CONFIG_DIR\n Example: %s install ~/.config/google-chrome' % (sys.argv[0], sys.argv[0]),
file=sys.stderr)
sys.exit(2)
try:
log('serving')
serve()
except Exception:
log(traceback.format_exc())
sys.exit(1)