File size: 1,904 Bytes
cd55154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()