Skip to content

Instantly share code, notes, and snippets.

@alexreg
Created August 15, 2018 23:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexreg/bb5bc9150d705c3fc034f6b5850f982e to your computer and use it in GitHub Desktop.
Save alexreg/bb5bc9150d705c3fc034f6b5850f982e to your computer and use it in GitHub Desktop.
Script for updating Rust binaries via Cargo
#!/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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment