Skip to content

Commit c39d73c

Browse files
committed
Add comments
1 parent f6491af commit c39d73c

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

contrib/get_supported_version_csi-sidecar.py

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
import shutil
77
from dateutil.relativedelta import relativedelta
88

9-
# Check that the `gh` command is in the path
109
def check_gh_command():
10+
"""
11+
Pretty much everything is processed from `gh`
12+
Check that the `gh` command is in the path before anything else
13+
"""
1114
if not shutil.which('gh'):
1215
print("Error: The `gh` command is not available in the PATH.")
1316
print("Please install the GitHub CLI (https://cli.github.com/) and try again.")
1417
exit(1)
1518

16-
# humanize duration outputs
1719
def duration_ago(dt):
20+
"""
21+
Humanize duration ouputs
22+
"""
1823
delta = relativedelta(datetime.datetime.now(), dt)
1924
if delta.years > 0:
2025
return f"{delta.years} year{'s' if delta.years > 1 else ''} ago"
@@ -30,16 +35,29 @@ def duration_ago(dt):
3035
return "just now"
3136

3237
def parse_version(version):
33-
# version pattern
38+
"""
39+
Parse version assuming it is in the form of v1.2.3
40+
"""
3441
pattern = r"v(\d+)\.(\d+)\.(\d+)"
3542
match = re.match(pattern, version)
3643
if match:
3744
major, minor, patch = map(int, match.groups())
3845
return (major, minor, patch)
3946

40-
# Calculate the end of life date for a minor release version
41-
# according to : https://kubernetes-csi.github.io/docs/project-policies.html#support
4247
def end_of_life_grouped_versions(versions):
48+
"""
49+
Calculate the end of life date for a minor release version according to : https://kubernetes-csi.github.io/docs/project-policies.html#support
50+
51+
The input is an array of tuples of:
52+
* grouped versions (e.g. 1.0, 1.1)
53+
* array of that contains all versions and their release date (e.g. 1.0.0, 01-01-2013)
54+
55+
versions structure example :
56+
[((3, 5), [('v3.5.0', datetime.datetime(2023, 4, 27, 22, 28, 6))]),
57+
((3, 4),
58+
[('v3.4.1', datetime.datetime(2023, 4, 5, 17, 41, 15)),
59+
('v3.4.0', datetime.datetime(2022, 12, 27, 23, 43, 41))])]
60+
"""
4361
supported_versions = []
4462
# Prepare dates for later calculation
4563
now = datetime.datetime.now()
@@ -48,14 +66,9 @@ def end_of_life_grouped_versions(versions):
4866

4967
# get the newer versions on top
5068
sorted_versions_list = sorted(versions.items(), key=lambda x: x[0], reverse=True)
51-
# structure example :
52-
# [((3, 5), [('v3.5.0', datetime.datetime(2023, 4, 27, 22, 28, 6))]),
53-
# ((3, 4),
54-
# [('v3.4.1', datetime.datetime(2023, 4, 5, 17, 41, 15)),
55-
# ('v3.4.0', datetime.datetime(2022, 12, 27, 23, 43, 41))])]
56-
latest = sorted_versions_list.pop(0)
5769

5870
# the latest version is always supported no matter the release date
71+
latest = sorted_versions_list.pop(0)
5972
supported_versions.append(latest[1][-1])
6073

6174
for v in sorted_versions_list:
@@ -70,12 +83,21 @@ def end_of_life_grouped_versions(versions):
7083
return supported_versions
7184

7285
def get_release_docker_image(repo, version):
86+
"""
87+
Extract docker image name from the relase page documentation
88+
"""
7389
output = subprocess.check_output(['gh', 'release', '-R', repo, 'view', version], text=True)
74-
match = re.search(r"`docker pull (.*)`", output)
75-
docker_image = match.group(1)
90+
#Extract matching image name excluding `
91+
match = re.search(r"docker pull ([\.\/\-\:\w\d]*)", output)
92+
docker_image = match.group(1) if match else ''
7693
return((version, docker_image))
7794

7895
def get_versions_from_releases(repo):
96+
"""
97+
Using `gh` cli get the github releases page details then
98+
create a list of grouped version on major.minor
99+
and for each give all major.minor.patch with release dates
100+
"""
79101
# Run the `gh release` command to get the release list
80102
output = subprocess.check_output(['gh', 'release', '-R', repo, 'list'], text=True)
81103
# Parse the output and group by major and minor version numbers
@@ -95,15 +117,15 @@ def get_versions_from_releases(repo):
95117

96118

97119
def main():
98-
# Argument parser
99-
parser = argparse.ArgumentParser(description='Get the currently supported versions for a GitHub repository.')
100-
parser.add_argument(
101-
'--repo',
102-
'-R', required=True,
103-
action='append', dest='repos',
104-
help='''The name of the repository in the format owner/repo. You can specify multiple -R repo to query multiple repositories e.g.:\n
105-
python -R kubernetes-csi/external-attacher -R kubernetes-csi/external-provisioner -R kubernetes-csi/external-resizer -R kubernetes-csi/external-snapshotter -R kubernetes-csi/livenessprobe -R kubernetes-csi/node-driver-registrar -R kubernetes-csi/external-health-monitor'''
106-
)
120+
manual = """
121+
This script lists the supported versions Github releases according to https://kubernetes-csi.github.io/docs/project-policies.html#support
122+
It has been designed to help to update the tables from : https://kubernetes-csi.github.io/docs/sidecar-containers.html\n\n
123+
It can take multiple repos as argument, for all CSI sidecars details you can run:
124+
./get_supported_version_csi-sidecar.py -R kubernetes-csi/external-attacher -R kubernetes-csi/external-provisioner -R kubernetes-csi/external-resizer -R kubernetes-csi/external-snapshotter -R kubernetes-csi/livenessprobe -R kubernetes-csi/node-driver-registrar -R kubernetes-csi/external-health-monitor\n
125+
With the output you can then update the documentation manually.
126+
"""
127+
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=manual)
128+
parser.add_argument('--repo', '-R', required=True, action='append', dest='repos', help='The name of the repository in the format owner/repo.')
107129
parser.add_argument('--display', '-d', action='store_true', help='(default) Display EOL versions with their dates', default=True)
108130
parser.add_argument('--doc', '-D', action='store_true', help='Helper to https://kubernetes-csi.github.io/docs/ that prints Docker image for each EOL version')
109131

@@ -126,9 +148,9 @@ def main():
126148
if args.doc:
127149
print("\nSupported Versions with docker images for each end of life version:\n")
128150
for version in eol_versions:
129-
_, image = get_release_docker_image(args.repo, version[0])
151+
_, image = get_release_docker_image(repo, version[0])
130152
print(f"{version[0]}\t{image}")
131153
print()
132154

133155
if __name__ == '__main__':
134-
main()
156+
main()

0 commit comments

Comments
 (0)