import argparse
import subprocess
import sys
from _utils import build_cargo_metadata, build_cargo_tree_dict
def _do_updates(explicit_dependencies, cargo_tree):
assert frozenset(explicit_dependencies.keys()) == frozenset(
cargo_tree.keys()
), "in Cargo.toml but not in 'cargo tree' output: %s, vice-versa: %s" % (
frozenset(explicit_dependencies.keys()) - frozenset(cargo_tree.keys()),
frozenset(cargo_tree.keys()) - frozenset(explicit_dependencies.keys()),
)
update_failed = False
for (crate, version) in explicit_dependencies.items():
current_version = cargo_tree[crate]
try:
subprocess.run(
[
"cargo",
"update",
"-p",
"%s:%s" % (crate, current_version),
"--precise=%s" % version,
],
check=True,
)
except subprocess.CalledProcessError as err:
update_failed = True
print(err, file=sys.stderr)
return "Failed to set the versions of some crates" if update_failed else None
def main():
parser = argparse.ArgumentParser(
description=(
"Lowers all dependencies in Cargo.lock to the versions specified "
"in Cargo.toml."
)
)
parser.parse_args()
explicit_dependencies = build_cargo_metadata()
current_dependencies = build_cargo_tree_dict()
return _do_updates(explicit_dependencies, current_dependencies)
if __name__ == "__main__":
sys.exit(main())