cornsnake.util_dependencies

Functions for checking the versions of Python and Git. It ensures that the major and minor versions of these dependencies meet the required criteria.

Documentation

 1"""
 2Functions for checking the versions of Python and Git. It ensures that the major and minor versions of these dependencies meet the required criteria.
 3
 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_dependencies.html)
 5"""
 6
 7import os
 8import sys
 9
10from . import util_git
11from . import util_print
12from . import util_log
13
14logger = util_log.getLogger(__name__)
15
16PY_MAJOR = 3
17PY_MIN_MINOR = 12
18
19GIT_MAJOR = 2
20GIT_MIN_MINOR = 42
21
22
23def _raise_versions_error(program, message):
24    """
25    Helper function to raise a SystemExit error with a specific message.
26    """
27    raise SystemExit(
28        f"DEPENDENCIES ERROR: {message}. Please check your version of {program}"
29    )
30
31
32def _check_major_versions_equal(program, actual, expected):
33    """
34    Check if the major version of a program matches the expected version.
35    """
36    if actual != expected:
37        _raise_versions_error(
38            program,
39            f"{program} major version must be {expected} - but you are using {actual}",
40        )
41
42
43def _check_minor_version_greater_than_or_equal(
44    program, actual, expected_major, expected_minor
45):
46    """
47    Check if the minor version of a program is greater than or equal to the expected version.
48    """
49    if actual < expected_minor:
50        _raise_versions_error(
51            program,
52            f"{program} version must be at least {expected_major}.{expected_minor} - but you are using {expected_major}.{actual}",
53        )
54
55
56def _dump_current_version(program, version):
57    """
58    Print the current version of a program.
59    """
60    util_print.print_custom_with_logger(f"Using {program} version {version}", logger)
61
62
63def check_python_version():
64    """
65    Check the version of Python being used.
66    """
67    version = sys.version_info
68    program = "Python"
69    _dump_current_version(program, str(version))
70    _check_major_versions_equal(program, version.major, PY_MAJOR)
71    _check_minor_version_greater_than_or_equal(
72        program, version.minor, PY_MAJOR, PY_MIN_MINOR
73    )
74
75
76def check_git_version():
77    """
78    Check the version of Git being used.
79    """
80    # example: 'git version 2.42.0.windows.1'
81    result_parts = util_git.execute_command("--version", [], os.getcwd()).split(" ")
82    version = result_parts[-1]
83    version_parts = version.split(".")
84    actual_major = int(version_parts[0])
85    actual_minor = int(version_parts[1])
86    program = "git"
87    _dump_current_version(program, version)
88    _check_major_versions_equal(program, actual_major, GIT_MAJOR)
89    _check_minor_version_greater_than_or_equal(
90        program, actual_minor, GIT_MAJOR, GIT_MIN_MINOR
91    )
logger = <Logger cornsnake.util_dependencies (INFO)>
PY_MAJOR = 3
PY_MIN_MINOR = 12
GIT_MAJOR = 2
GIT_MIN_MINOR = 42
def check_python_version():
64def check_python_version():
65    """
66    Check the version of Python being used.
67    """
68    version = sys.version_info
69    program = "Python"
70    _dump_current_version(program, str(version))
71    _check_major_versions_equal(program, version.major, PY_MAJOR)
72    _check_minor_version_greater_than_or_equal(
73        program, version.minor, PY_MAJOR, PY_MIN_MINOR
74    )

Check the version of Python being used.

def check_git_version():
77def check_git_version():
78    """
79    Check the version of Git being used.
80    """
81    # example: 'git version 2.42.0.windows.1'
82    result_parts = util_git.execute_command("--version", [], os.getcwd()).split(" ")
83    version = result_parts[-1]
84    version_parts = version.split(".")
85    actual_major = int(version_parts[0])
86    actual_minor = int(version_parts[1])
87    program = "git"
88    _dump_current_version(program, version)
89    _check_major_versions_equal(program, actual_major, GIT_MAJOR)
90    _check_minor_version_greater_than_or_equal(
91        program, actual_minor, GIT_MAJOR, GIT_MIN_MINOR
92    )

Check the version of Git being used.