-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_common.py
64 lines (52 loc) · 1.85 KB
/
setup_common.py
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
# This file should not depend on any repo python files outside of the top-level directory.
#
# We keep this file at the top-level directory, rather than inside py/, to avoid PYTHONPATH-related
# import issues for scripts that are executed outside of a docker container, such as run_docker.py
# and setup_wizard.py.
#
# For scripts that executed inside of a docker container, we can be sure that the PYTHONPATH is
# correctly set up to include the py/ directory.
import json
import os
from packaging import version
import subprocess
LOCAL_DOCKER_IMAGE = 'a0a'
DOCKER_HUB_IMAGE = 'dshin83/alphazeroarcade'
LATEST_DOCKER_HUB_IMAGE = f'{DOCKER_HUB_IMAGE}:latest'
MINIMUM_REQUIRED_IMAGE_VERSION = "2.1.0"
DIR = os.path.dirname(os.path.abspath(__file__))
ENV_JSON_FILENAME = os.path.join(DIR, '.env.json')
def update_env_json(mappings):
env = {}
if os.path.exists(ENV_JSON_FILENAME):
with open(ENV_JSON_FILENAME) as f:
env = json.load(f)
env.update(mappings)
with open(ENV_JSON_FILENAME, 'w') as f:
json.dump(env, f, indent=2)
def get_env_json():
env = {}
if os.path.exists(ENV_JSON_FILENAME):
with open(ENV_JSON_FILENAME) as f:
env = json.load(f)
return env
def get_image_label(image_name, label_key):
"""
Get the value of a specific label from a Docker image.
"""
result = subprocess.check_output(
["docker", "inspect",
f"--format={{{{index .Config.Labels \"{label_key}\"}}}}", image_name],
stderr=subprocess.STDOUT,
text=True,
).strip()
if not result:
return None
return result
def is_version_ok(version_str):
"""
Check if a given version string is at least the minimum required version.
"""
if version_str is None:
return False
return version.parse(version_str) >= version.parse(MINIMUM_REQUIRED_IMAGE_VERSION)