From b8cfcd5c8fb9c714b9f5724e0b85bd21a56c37be Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Thu, 20 Oct 2022 14:20:19 -0500 Subject: [PATCH] fix: handle a few corner cases - Check if `git` is installed. - Ensure proper working directory when used in another project. - Use the version of the active branch. - Improved error messages if tag retrieval fails. --- sdk/python/flet/version.py | 44 +++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/sdk/python/flet/version.py b/sdk/python/flet/version.py index e5d96807c5..ca35f635d5 100644 --- a/sdk/python/flet/version.py +++ b/sdk/python/flet/version.py @@ -1,15 +1,23 @@ -import subprocess as sp +"""Provide the current Flet version.""" + -from pkg_resources import parse_version +import os +import subprocess as sp +from pathlib import Path +import flet from flet.utils import which + # this value will be replaced by CI version = "" def update_version(): - in_repo = sp.run( + """Return the current version or default.""" + working = Path().absolute() + os.chdir(Path(flet.__file__).absolute().parent) + in_repo = which("git") and sp.run( ["git", "status"], capture_output=True, text=True, @@ -18,15 +26,31 @@ def update_version(): if in_repo: # NOTE: this may break if there is a tag name starting with # "v" that isn't a version number - tags = sp.run( - ["git", "tag"], + class RepositoryError(OSError): + pass + + git_p = sp.run( + ["git", "describe", "--abbrev=0"], capture_output=True, text=True, - ).stdout.splitlines() - versions = filter(lambda t: t.startswith("v"), tags) - return sorted(versions, key=parse_version)[-1][1:] - return "0.1.60" + ) + err = git_p.stderr.strip() + + if "cannot describe anything" in err: + msg = "You may be using a repo cloned from a fork. " + msg += "If so please clone the original Flet repo" + raise RepositoryError(msg) + + if err: + msg = "Unknown error while fetching the version: {err}" + raise RepositoryError(msg) + version = git_p.stdout.strip()[1:] + + else: + version = "0.1.60" + os.chdir(working) + return version -if not globals().get("version", None): +if not version: version = update_version()