From 06e39997aad16e48f589ad32867e8726b739b723 Mon Sep 17 00:00:00 2001 From: Chirayu Kapoor Date: Fri, 17 May 2024 15:50:52 +0530 Subject: [PATCH] refactor: Add unbuffer option to print python logs * Fixed issues in workflow python script * Implemented the use of semver * Removed format_version and get_max_version function * Removed unused variable and import statments Signed-off-by: Chirayu Kapoor --- .github/workflows/add_new_versions.yml | 4 +- workflow_scripts/check-for-new-versions.py | 1 - workflow_scripts/gen-new-version-files.py | 55 +++++----------------- workflow_scripts/requirements.txt | 1 + 4 files changed, 16 insertions(+), 45 deletions(-) diff --git a/.github/workflows/add_new_versions.yml b/.github/workflows/add_new_versions.yml index bca712e..8d3f8f0 100644 --- a/.github/workflows/add_new_versions.yml +++ b/.github/workflows/add_new_versions.yml @@ -31,7 +31,7 @@ jobs: - name: Check if new versions available id: check-versions run: | - python workflow_scripts/check-for-new-versions.py + python -u workflow_scripts/check-for-new-versions.py env: EXCLUDED_VERSIONS: "v20.10.x,v23.0.x" @@ -54,7 +54,7 @@ jobs: env: NEW_VERSIONS: ${{ env.NEW_VERSIONS }} run: | - python workflow_scripts/gen-new-version-files.py + python -u workflow_scripts/gen-new-version-files.py - name: Create branch, commit and push if: ${{ env.pr_exist == 'false' && env.PR_TITLE != '' }} diff --git a/workflow_scripts/check-for-new-versions.py b/workflow_scripts/check-for-new-versions.py index 6c14ed0..97a531d 100644 --- a/workflow_scripts/check-for-new-versions.py +++ b/workflow_scripts/check-for-new-versions.py @@ -1,5 +1,4 @@ import os -import subprocess import requests # Constants diff --git a/workflow_scripts/gen-new-version-files.py b/workflow_scripts/gen-new-version-files.py index 3861e68..c1aedae 100644 --- a/workflow_scripts/gen-new-version-files.py +++ b/workflow_scripts/gen-new-version-files.py @@ -1,48 +1,29 @@ import os import subprocess -import requests +import semver # Constants DIST_FOLDER = './dist' NEW_VERSIONS = os.environ.get("NEW_VERSIONS","") +SCRIPT_EXTENSION=".sh" if NEW_VERSIONS == "": print("no new versions available, NEW_VERSIONS env variable is empty") exit(1) -def get_max_version(ver1, ver2): - if ver1.startswith('v'): - ver1 = ver1[1:] - if ver2.startswith('v'): - ver2 = ver2[1:] - - ver1_tuple = tuple(map(int, ver1.split('.'))) - ver2_tuple = tuple(map(int, ver2.split('.'))) - if ver1 > ver2: - return ver1 - return ver2 - -def format_version(v): - if v.startswith('v'): - return v[1:] - return v - def get_last_added_version(files_dir): max_modification_time = 0.0 - last_added_version = "" + last_added_version = "0.0.0" for file in os.listdir(files_dir): - if file.endswith('.sh') and file.count('.') == 3: + if file.endswith(SCRIPT_EXTENSION) and file.count('.') == 3: file_path = os.path.join(files_dir, file) modification_time = os.path.getmtime(file_path) - file_version = 'v' + file[:-3] + file_version = file.removesuffix(SCRIPT_EXTENSION) if modification_time > max_modification_time: max_modification_time = modification_time last_added_version = file_version elif modification_time == max_modification_time: - if last_added_version == "": - last_added_version = file_version - else: - last_added_version = get_max_version(last_added_version,file_version) + last_added_version = semver.max_ver(last_added_version,file_version) return last_added_version @@ -59,16 +40,9 @@ def get_version_dict(versions): version_dict = {} for version in versions: - version_parts = version.split('.') - major_minor = version_parts[0] + '.' + version_parts[1] - - if major_minor in version_dict: - current_version = tuple(map(int, version_parts[2])) - max_version = tuple(map(int, version_dict[major_minor].split('.')[2])) - if current_version > max_version: - version_dict[major_minor] = version - else: - version_dict[major_minor] = version + version_parts = semver.parse(version) + major_minor = f"{version_parts['major']}.{version_parts['minor']}" + version_dict[major_minor] = semver.max_ver(version_dict.setdefault(major_minor, version), version) return version_dict @@ -77,19 +51,16 @@ def main(): last_added_version = get_last_added_version(DIST_FOLDER) print("Last added version:",last_added_version) - new_versions = NEW_VERSIONS.split(',') - formatted_new_versions = list(map(format_version,new_versions)) - print("Formatted new versions: ", formatted_new_versions) + version_list = [version.removeprefix('v') for version in NEW_VERSIONS.split(',')] + print("Formatted new versions: ", version_list) - for version in formatted_new_versions: + for version in version_list: generate_diffs(last_added_version, version) - versions_string = ",".join(new_versions) - print("running generate script") subprocess.run(["bash", "./scripts/generate"], check=True) - version_dict = get_version_dict(formatted_new_versions) + version_dict = get_version_dict(version_list) print("version dictionary for symlink: ",version_dict) for major_minor,version in version_dict.items(): diff --git a/workflow_scripts/requirements.txt b/workflow_scripts/requirements.txt index 9688b8e..f0a1374 100644 --- a/workflow_scripts/requirements.txt +++ b/workflow_scripts/requirements.txt @@ -1 +1,2 @@ Requests==2.31.0 +semver==3.0.2