Created
August 15, 2018 23:28
-
-
Save alexreg/bb5bc9150d705c3fc034f6b5850f982e to your computer and use it in GitHub Desktop.
Script for updating Rust binaries via Cargo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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