Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
131 lines (115 loc) · 4.36 KB
/
Copy pathcode.py
File metadata and controls
131 lines (115 loc) · 4.36 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
import os
import subprocess
from pyngrok import ngrok
try:
from google.colab import drive
colab_env = True
except ImportError:
colab_env = False
EXTENSIONS = ["ms-python.python", "jithurjacob.nbpreviewer","vscodevim.vim"]
import sys
class Connector:
def __init__(self, port,option,args):
self.port = port
self.option = option
self.args = args
self.connection = None
def connect(self):
if self.option == "ngrok":
url = ngrok.connect(port=self.port, options={"bind_tls": True})
print(f"Code Server can be accessed on: {url}")
elif self.option == "localtunnel":
self.connection = subprocess.Popen(
"lt --port 10000 " + f"{self.args}",
stdout=subprocess.PIPE,
shell=True)
def disconnect(self):
if self.option == "localtunnel":
try:
self.connection.kill()
except:
pass
elif self.option == "ngrok":
active_tunnels = ngrok.get_tunnels()
for tunnel in active_tunnels:
public_url = tunnel.public_url
ngrok.disconnect(public_url)
def get_url(self):
if self.option == "localtunnel":
for line in iter(self.connection.stdout.readline, ''):
print(line.rstrip().decode('utf-8'))
break
class ColabCode:
def __init__(self, port=10000, password=None, mount_drive=False,
option="localtunnel", add_extensions=None,args='',tunnel_args='',code_args=[]):
self.port = port
self.args = args
self._start_up()
self._install_localtunnel()
print(option)
self.connection = Connector(self.port, option,args=tunnel_args)
self.password = password
self._mount = mount_drive
self._install_code(code_args)
self.extensions = EXTENSIONS
if add_extensions is not None and add_extensions != []:
if isinstance(add_extensions, list) and isinstance(
add_extensions[0], str):
self.extensions += add_extensions
self.extensions.reverse()
else:
raise TypeError(
"You need to pass a list of string(s) e.g. ['ms-python.python']")
self._install_extensions()
self._start_server()
self.connection.get_url()
self._run_code()
def _install_code(self,args):
subprocess.run(
["wget", "https://code-server.dev/install.sh"],
stdout=subprocess.PIPE
)
subprocess.run(["sh", "install.sh"] + args, stdout=subprocess.PIPE)
def _install_localtunnel(self):
subprocess.run(
["npm", "install", "-g", "localtunnel"], stdout=subprocess.PIPE
)
def _start_up(self):
subprocess.run(
["wget", "https://gist.githubusercontent.com/infinex/8cd88fbadd36fe8d0764c3bb36928aa3/raw/d78c708f3507ec8d74fc82fc03d56e2bf7d4c7a9/code_server.sh"],
stdout=subprocess.PIPE
)
subprocess.run(
["bash", "code_server.sh"], stdout=subprocess.PIPE
)
def _install_extensions(self):
for ext in self.extensions:
print(f'Install Extension {ext}')
if ext.startswith('http'):
d = subprocess.call(f'wget {ext}',shell=True)
print(f'downloading {ext}')
ext = os.path.basename(ext)
if d:
print(f'Unable to install {ext}')
continue
subprocess.run(["code-server", "--install-extension", f"{ext}"])
def _start_server(self):
self.connection.disconnect()
self.connection.connect()
def _run_code(self):
os.system(f"fuser -n tcp -k {self.port}")
if self._mount and colab_env:
drive.mount("/content/drive")
if self.password:
code_cmd = f"PASSWORD={self.password} code-server --port {self.port}" + self.args
else:
code_cmd = f"code-server --port {self.port} --auth none " + self.args
with subprocess.Popen(
[code_cmd],
shell=True,
stdout=subprocess.PIPE,
bufsize=1,
universal_newlines=True,
) as proc:
for line in proc.stdout:
print(line, end="")