File size: 2,183 Bytes
40a04d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# Do everything freaking necessary to get all Vertex-AI-related logins properly
# connected and refreshed, all IPython-crash-related garbage worked around, and
# in general produce a happy world.
import vertexai
import os
import subprocess
import sys
from pathlib import Path

PROJECT_ID = "frozone-475719"
REGION = "us-central1"
LOCATION = "us-central1"
ZONE = "us-central1-c"

vertexai.init(project=PROJECT_ID, location=LOCATION)

def run_quiet(cmd):
    try:
        result = subprocess.run(
            cmd,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            check=False,
        )
        return result.returncode == 0
    except FileNotFoundError:
        print(f"Command not found: {' '.join(cmd)}", file=sys.stderr)
        return False


def ensure_gcloud_user_auth():
    if not run_quiet(["gcloud", "auth", "print-access-token"]):
        print("No gcloud user auth found. Launching browser login...")
        subprocess.check_call(["gcloud", "auth", "login"])


def ensure_adc():
    if run_quiet(["gcloud", "auth", "application-default", "print-access-token"]):
        return

    creds_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", "")
    if creds_path and Path(creds_path).is_file():
        print(
            f"ADC via GOOGLE_APPLICATION_CREDENTIALS is set to: {creds_path}"
        )
        return

    print(
        "No ADC found. "
        "Launching browser login for Application Default Credentials..."
    )
    subprocess.check_call(["gcloud", "auth", "application-default", "login"])


# This is the main function to call from other scripts to make sure auth + ADC
# are set up.
def ensure_gcloud():
    try:
        ensure_gcloud_user_auth()
        ensure_adc()
        print("(Python: gcloud user auth and ADC are ready.)")
    except subprocess.CalledProcessError as e:
        print(f"Command failed with exit code {e.returncode}", file=sys.stderr)
        sys.exit(e.returncode)

# <UGGH I HATE LIFE>
import IPython.display as _ipd
import IPython.core.display as _ipcd
if not hasattr(_ipcd, "display"):
    _ipcd.display = _ipd.display
# </UGGH I HATE LIFE>

ensure_gcloud()