From 677102e6769556c25c8e60805969405b52ba4e8c Mon Sep 17 00:00:00 2001 From: Matt Joyce Date: Thu, 30 May 2024 09:02:55 +1000 Subject: [PATCH 1/2] Script to check local vs published versions This script is for use with Github workflows --- .../scripts/check_version_has_incremented.py | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 .github/scripts/check_version_has_incremented.py diff --git a/.github/scripts/check_version_has_incremented.py b/.github/scripts/check_version_has_incremented.py new file mode 100644 index 0000000..87e5cc6 --- /dev/null +++ b/.github/scripts/check_version_has_incremented.py @@ -0,0 +1,88 @@ +""" +checks local verions against published verions. + +Usage: + +python .github\scripts\check_version_has_incremented.py js .\apps\js-sdk\firecrawl @mendable/firecrawl-js +Local version: 0.0.22 +Published version: 0.0.21 +true + +python .github\scripts\check_version_has_incremented.py python .\apps\python-sdk\firecrawl firecrawl-py +Local version: 0.0.11 +Published version: 0.0.11 +false + +""" +import json +import os +import re +import sys +from pathlib import Path + +import requests +from packaging.version import Version +from packaging.version import parse as parse_version + + +def get_python_version(file_path: str) -> str: + """Extract version string from Python file.""" + version_file = Path(file_path).read_text() + version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M) + if version_match: + return version_match.group(1).strip() + raise RuntimeError("Unable to find version string.") + +def get_pypi_version(package_name: str) -> str: + """Get latest version of Python package from PyPI.""" + response = requests.get(f"https://pypi.org/pypi/{package_name}/json") + version = response.json()['info']['version'] + return version.strip() + +def get_js_version(file_path: str) -> str: + """Extract version string from package.json.""" + with open(file_path, 'r') as file: + package_json = json.load(file) + if 'version' in package_json: + return package_json['version'].strip() + raise RuntimeError("Unable to find version string in package.json.") + +def get_npm_version(package_name: str) -> str: + """Get latest version of JavaScript package from npm.""" + response = requests.get(f"https://registry.npmjs.org/{package_name}/latest") + version = response.json()['version'] + return version.strip() + +def is_version_incremented(local_version: str, published_version: str) -> bool: + """Compare local and published versions.""" + local_version_parsed: Version = parse_version(local_version) + published_version_parsed: Version = parse_version(published_version) + return local_version_parsed > published_version_parsed + +if __name__ == "__main__": + package_type = sys.argv[1] + package_path = sys.argv[2] + package_name = sys.argv[3] + + if package_type == "python": + # Get current version from __init__.py + current_version = get_python_version(os.path.join(package_path, '__init__.py')) + # Get published version from PyPI + published_version = get_pypi_version(package_name) + elif package_type == "js": + # Get current version from package.json + current_version = get_js_version(os.path.join(package_path, 'package.json')) + # Get published version from npm + published_version = get_npm_version(package_name) + else: + raise ValueError("Invalid package type. Use 'python' or 'js'.") + + # Print versions for debugging + print(f"Local version: {current_version}") + print(f"Published version: {published_version}") + + # Compare versions and print result + if is_version_incremented(current_version, published_version): + print("true") + else: + print("false") From d54ec94bfdf44f46e0dbf3fe6a4e7de997df7787 Mon Sep 17 00:00:00 2001 From: rafaelsideguide <150964962+rafaelsideguide@users.noreply.github.com> Date: Tue, 4 Jun 2024 16:01:21 -0300 Subject: [PATCH 2/2] lgtm. Added some docs and requirements.txt --- .github/scripts/check_version_has_incremented.py | 12 +++++++++++- .github/scripts/requirements.txt | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .github/scripts/requirements.txt diff --git a/.github/scripts/check_version_has_incremented.py b/.github/scripts/check_version_has_incremented.py index 87e5cc6..345d088 100644 --- a/.github/scripts/check_version_has_incremented.py +++ b/.github/scripts/check_version_has_incremented.py @@ -1,14 +1,24 @@ """ checks local verions against published verions. -Usage: +# Usage: +Unix: +python .github/scripts/check_version_has_incremented.py js ./apps/js-sdk/firecrawl @mendable/firecrawl-js + +Windows: python .github\scripts\check_version_has_incremented.py js .\apps\js-sdk\firecrawl @mendable/firecrawl-js + Local version: 0.0.22 Published version: 0.0.21 true +Unix: +python .github/scripts/check_version_has_incremented.py python ./apps/python-sdk/firecrawl firecrawl-py + +Windows: python .github\scripts\check_version_has_incremented.py python .\apps\python-sdk\firecrawl firecrawl-py + Local version: 0.0.11 Published version: 0.0.11 false diff --git a/.github/scripts/requirements.txt b/.github/scripts/requirements.txt new file mode 100644 index 0000000..0bfc676 --- /dev/null +++ b/.github/scripts/requirements.txt @@ -0,0 +1,2 @@ +requests +packaging \ No newline at end of file