forked from ethyca/fides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
131 lines (99 loc) · 3.76 KB
/
noxfile.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
This file aggregates nox commands for various development tasks.
"""
import shutil
import sys
from os.path import isfile
from subprocess import PIPE, CalledProcessError, run
from typing import List
import nox
sys.path.append("noxfiles")
# pylint: disable=unused-wildcard-import, wildcard-import, wrong-import-position
from ci_nox import *
from dev_nox import *
from docker_nox import *
from docs_nox import *
from utils_nox import *
# pylint: enable=unused-wildcard-import, wildcard-import, wrong-import-position
REQUIRED_DOCKER_VERSION = "20.10.17"
# Sets the default session to `--list`
nox.options.sessions = []
nox.options.reuse_existing_virtualenvs = True
@nox.session()
def usage(session: nox.Session) -> None:
"""Prints the documentation for a nox session provided: `nox -s usage -- <session>`."""
if not session.posargs:
session.error("Please provide a session name, such as `clean`")
command = session.posargs[0]
if not command in globals():
session.error(
"Sorry, this isn't a valid nox session.\nExamples: `clean`, `build`, `pytest_ctl`"
)
session.log(globals()[command].__doc__)
def check_for_env_file() -> None:
"""Create a .env file if none exists."""
env_file_example = "example.env"
env_file = ".env"
if not isfile(env_file):
print(
f"Creating env file for local testing & development from {env_file_example}: {env_file}"
)
shutil.copy(env_file_example, env_file)
def convert_semver_to_list(semver: str) -> List[int]:
"""
Convert a standard semver string to a list of ints
'2.10.7' -> [2,10,7]
"""
return [int(x) for x in semver.split(".")]
def compare_semvers(version_a: List[int], version_b: List[int]) -> bool:
"""
Determine which semver-style list of integers is larger.
[2,10,3], [2,10,2] -> True
[3,1,3], [2,10,2] -> True
[2,10,2], [2,10,2] -> True
[1,1,3], [2,10,2] -> False
"""
major, minor, patch = [0, 1, 2]
# Compare Major Versions
if version_a[major] > version_b[major]:
return True
if version_a[major] < version_b[major]:
return False
# If Major Versions Match, Compare Minor Versions
if version_a[minor] > version_b[minor]:
return True
if version_a[minor] < version_b[minor]:
return False
# If Both Major & Minor Versions Match, Compare Patch Versions
if version_a[patch] < version_b[patch]:
return False
return True
def check_docker_version() -> bool:
"""Verify the Docker version."""
try:
raw = run("docker --version", stdout=PIPE, check=True, shell=True)
except CalledProcessError:
raise SystemExit("Error: Command 'docker' is not available.")
parsed = raw.stdout.decode("utf-8").rstrip("\n")
# We need to handle multiple possible version formats here
# Docker version 20.10.17, build 100c701
# Docker version 20.10.18+azure-1, build b40c2f6
docker_version = parsed.split("version ")[-1].split(",")[0].split("+")[0]
print(parsed)
split_docker_version = convert_semver_to_list(docker_version)
split_required_docker_version = convert_semver_to_list(REQUIRED_DOCKER_VERSION)
if len(split_docker_version) != 3:
raise SystemExit(
"Error: Docker version format is invalid, expecting semver format. Please upgrade to a more recent version and try again"
)
version_is_valid = compare_semvers(
split_docker_version, split_required_docker_version
)
if not version_is_valid:
raise SystemExit(
f"Error: Your Docker version (v{docker_version}) is not compatible, please update to at least version {REQUIRED_DOCKER_VERSION}!"
)
return version_is_valid
# Run startup checks
check_docker_version()
check_for_env_file()