mass-rename polkadot node crates
As this would have been unbelievably tedious to do by hand, I wrote a little script to simplify the process: ```python3 from pathlib import Path import toml import subprocess def polkadot_node_crates(polkadot_path: Path): """ yield a sequence of polkadot node crate names """ for cargo_toml in polkadot_path.glob("node/**/Cargo.toml"): with cargo_toml.open() as cargo_toml_file: data = toml.load(cargo_toml_file) yield (cargo_toml.parent, data["package"]["name"]) def underscored(s: str) -> str: return s.replace("-", "_") def rename(polkadot_path: Path, crate: str, crate_name: str) -> None: ok = False while not ok: print() print(f'Choose a new name for "{crate_name}" ({crate})') print("(leave blank to skip)") new_name = input("new name> ") if new_name.strip() == "": return replacements = [ (crate_name, new_name), (underscored(crate_name), underscored(new_name)), ] print() for (old, new) in replacements: print(f"replacing {old} with {new}") print() ok = input("ok? [yN]> ") ok = ok.lstrip().lower().startswith("y") for (old, new) in replacements: if "/" in old or "/" in new: print(f"patterns cannot contain slashes") return output = subprocess.run( ["rg", "--files-with-matches", old, polkadot_path], stdout=subprocess.PIPE, text=True, ) if output.stdout != "": # generally, we want to check the return code. # however, rg always exits 1 when no matches are discovered, and # there appears to be no way to change that behavior via flags. # therefore, only check it if we have some lines of output. output.check_returncode() for file in output.stdout.splitlines(): file = file.strip() if file == "": continue output = subprocess.run(["sed", "-i", "-E", "-e", f"s/{old}/{new}/g", file]) output.check_returncode() def main(): import argparse parser = argparse.ArgumentParser( description="interactively rename polkadot node crates" ) parser.add_argument("PATH", type=Path, help="path to the polkadot repo") args = parser.parse_args() for (crate, crate_name) in sorted(polkadot_node_crates(args.PATH)): rename(args.PATH, crate, crate_name) if __name__ == "__main__": main() ```
parent
4fdbf977
Please register or sign in to comment