Repository: bitcoin/bitcoin
File: contrib/verify-binaries/verify.py (Python)

Code before:

def indent(output: str) -> str:
    return textwrap.indent(output, '  ')


def bool_from_env(key, default=False) -> bool:
    if key not in os.environ:
        return default
    raw = os.environ[key]

    if raw.lower() in ('1', 'true'):
        return True
    elif raw.lower() in ('0', 'false'):
        return False
    raise ValueError(f"Unrecognized environment value {key}={raw!r}")


VERSION_FORMAT = "<major>.<minor>[.<patch>][-rc[0-9]][-platform]"
VERSION_EXAMPLE = "22.0-x86_64 or 23.1-rc1-darwin"

def parse_version_string(version_str):
    parts = version_str.split('-')
    version_base = parts[0]
    version_rc = ""
    version_os = ""
    if len(parts) == 2:  # "<version>-rcN" or "version-platform"
        if "rc" in parts[1]:
            version_rc = parts[1]
        else:
            version_os = parts[1]
    elif len(parts) == 3:  # "<version>-rcN-platform"
        version_rc = parts[1]
        version_os = parts[2]

    return version_base, version_rc, version_os


def download_with_wget(remote_file, local_file):
    result = subprocess.run(['wget', '-O', local_file, remote_file],
                            stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
    return result.returncode == 0, result.stdout.decode().rstrip()


def download_lines_with_urllib(url) -> tuple[bool, list[str]]:
    """Get (success, text lines of a file) over HTTP."""
    try:
        return (True, [
            line.strip().decode() for line in urllib.request.urlopen(url).readlines()])
    except urllib.error.HTTPError as e:
        log.warning(f"HTTP request to {url} failed (HTTPError): {e}")
    except Exception as e:

Review comment (suggestion):
nit: I think using `startswith` is a bit more intuitive here.

The entire logic can be simplified too, I think:

<details>
<summary>git diff on a2fc9ddee9</summary>

```diff
diff --git a/contrib/verify-binaries/verify.py b/contrib/verify-binaries/verify.py
index b7f64d4336..d09ae0dbfe 100755
--- a/contrib/verify-binaries/verify.py
+++ b/contrib/verify-binaries/verify.py
@@ -100,13 +100,10 @@ VERSION_FORMAT = "<major>.<minor>[.<patch>][-rc[0-9]][-platform]"
 VERSION_EXAMPLE = "22.0 or 23.1-rc1-darwin.dmg or 27.0-x86_64-linux-gnu"
 
 def parse_version_string(version_str):
-    parts = version_str.split('-')
-    version_base = parts[0]
+    version_base, _, version_os = version_str.partition('-')
     version_rc = ""
-    version_os = "-".join(parts[1:]) # "<version>" or "<version>-platform"
-    if "-rc" in "-".join(parts[:2]): # "<version>-rcN[-platform]"
-        version_rc = parts[1]
-        version_os = "-".join(parts[2:])
+    if version_os.startswith("rc"):
+        version_rc, _, version_os = version_os.partition('-')
 
     return version_base, version_rc, version_os
 

```
</details>


Code after:

def indent(output: str) -> str:
    return textwrap.indent(output, '  ')


def bool_from_env(key, default=False) -> bool:
    if key not in os.environ:
        return default
    raw = os.environ[key]

    if raw.lower() in ('1', 'true'):
        return True
    elif raw.lower() in ('0', 'false'):
        return False
    raise ValueError(f"Unrecognized environment value {key}={raw!r}")


VERSION_FORMAT = "<major>.<minor>[.<patch>][-rc[0-9]][-platform]"
VERSION_EXAMPLE = "22.0 or 23.1-rc1-darwin.dmg or 27.0-x86_64-linux-gnu"

def parse_version_string(version_str):
    # "<version>[-rcN][-platform]"
    version_base, _, platform = version_str.partition('-')
    rc = ""
    if platform.startswith("rc"): # "<version>-rcN[-platform]"
        rc, _, platform = platform.partition('-')
    # else "<version>" or "<version>-platform"

    return version_base, rc, platform


def download_with_wget(remote_file, local_file):
    result = subprocess.run(['wget', '-O', local_file, remote_file],
                            stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
    return result.returncode == 0, result.stdout.decode().rstrip()


def download_lines_with_urllib(url) -> tuple[bool, list[str]]:
    """Get (success, text lines of a file) over HTTP."""
    try:
        return (True, [
            line.strip().decode() for line in urllib.request.urlopen(url).readlines()])
    except urllib.error.HTTPError as e:
        log.warning(f"HTTP request to {url} failed (HTTPError): {e}")
    except Exception as e:
        log.warning(f"HTTP request to {url} failed ({e})")
    return (False, [])


def verify_with_gpg(
    filename,