flash2 / tunner.py
sonygod's picture
first
cd55154
from pathlib import Path
import subprocess
from importlib import import_module
import time
import sys
message = """
- Ready!
- Open VSCode on your laptop and open the command prompt
- Select: 'Remote-Tunnels: Connect to Tunnel' to connect to colab
""".strip()
dir_path = "/etc/noteable/project"
local_folder = '/etc/noteable/project'
def start_tunnel() -> None:
command = f"cd {dir_path} &&./code tunnel --accept-server-license-terms --random-name"
p = subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
show_outputs = False
while True:
line = p.stdout.readline().decode("utf-8")
if show_outputs:
print(line.strip())
if "To grant access to the server" in line:
print(line.strip())
if "Open this link" in line:
print("Starting the tunnel")
time.sleep(5)
print(message)
print("Logs:")
show_outputs = True
line = ""
if line == "" and p.poll() is not None:
break
return None
def run(command: str) -> None:
process = subprocess.run(command.split())
if process.returncode == 0:
print(f"Ran: {command}")
def connect() -> None:
# Create a folder to store all the code files
Path(local_folder).mkdir(parents=True, exist_ok=True)
print("Installing python libraries...")
run("pip3 install --user flake8 black ipywidgets twine")
run("pip3 install -U ipykernel")
run("apt install htop -y")
print("Installing vscode-cli...")
run(
f"curl -Lk https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64 -o {dir_path}/vscode_cli.tar.gz"
)
run(f"tar -xf {dir_path}/vscode_cli.tar.gz -C {dir_path}")
print("Starting the tunnel")
start_tunnel()
# To run the function, simply call: connect()
connect()