sonygod/flash2
0
1from pathlib import Path2import subprocess3from importlib import import_module4import time5import sys6 7message = """8- Ready!9- Open VSCode on your laptop and open the command prompt10- Select: 'Remote-Tunnels: Connect to Tunnel' to connect to colab11""".strip()12 13dir_path = "/etc/noteable/project"14local_folder = '/etc/noteable/project'15 16def start_tunnel() -> None:17 command = f"cd {dir_path} &&./code tunnel --accept-server-license-terms --random-name"18 p = subprocess.Popen(19 command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT20 )21 22 show_outputs = False23 while True:24 line = p.stdout.readline().decode("utf-8")25 if show_outputs:26 print(line.strip())27 if "To grant access to the server" in line:28 print(line.strip())29 if "Open this link" in line:30 print("Starting the tunnel")31 time.sleep(5)32 print(message)33 print("Logs:")34 show_outputs = True35 line = ""36 if line == "" and p.poll() is not None:37 break38 return None39 40 41def run(command: str) -> None:42 process = subprocess.run(command.split())43 if process.returncode == 0:44 print(f"Ran: {command}")45 46def connect() -> None:47 # Create a folder to store all the code files48 Path(local_folder).mkdir(parents=True, exist_ok=True)49 50 print("Installing python libraries...")51 run("pip3 install --user flake8 black ipywidgets twine")52 run("pip3 install -U ipykernel")53 run("apt install htop -y")54 55 print("Installing vscode-cli...")56 run(57 f"curl -Lk https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64 -o {dir_path}/vscode_cli.tar.gz"58 )59 run(f"tar -xf {dir_path}/vscode_cli.tar.gz -C {dir_path}")60 61 print("Starting the tunnel")62 start_tunnel()63 64 65# To run the function, simply call: connect()66 67connect()