Skip to content

Instantly share code, notes, and snippets.

@alexreg
Created August 15, 2018 23:28

Revisions

  1. alexreg created this gist Aug 15, 2018.
    29 changes: 29 additions & 0 deletions cargo-update-binaries.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/usr/bin/env python3

    import io
    import json
    from packaging import version
    import re
    import subprocess

    def get_crate_info(crate):
    with subprocess.Popen(["cargo", "info", "--json", crate], stdout = subprocess.PIPE) as proc:
    return json.load(proc.stdout)

    with subprocess.Popen(["cargo", "install", "--list"], stdout = subprocess.PIPE) as proc:
    re_crate = re.compile(r"(\w+) v([\d.]+):")
    for line in io.TextIOWrapper(proc.stdout, encoding = "utf-8"):
    line = line.rstrip()
    match = re_crate.fullmatch(line)
    if match:
    crate = match[1]
    installed_version = version.parse(match[2])

    crate_info = get_crate_info(crate)
    latest_version = version.parse(crate_info["crate"]["max_version"])

    print("Found crate '{}' v{} (latest v{})".format(crate, installed_version, latest_version))

    if latest_version > installed_version:
    print("Updating crate '{}'...".format(crate))
    subprocess.run(["cargo", "install", "--force", crate])