patch_toml_package_name.py 530 B

12345678910111213141516171819202122232425
  1. import os
  2. import pathlib
  3. import sys
  4. import tomlkit
  5. if len(sys.argv) < 2:
  6. print("Specify toml file and a new package name")
  7. sys.exit(1)
  8. current_dir = pathlib.Path(os.getcwd())
  9. toml_path = current_dir.joinpath(current_dir, sys.argv[1])
  10. package_name = sys.argv[2]
  11. print(f"Patching TOML file {toml_path} to {package_name}")
  12. # read
  13. with open(toml_path, "r") as f:
  14. t = tomlkit.parse(f.read())
  15. # patch name
  16. t["tool"]["poetry"]["name"] = package_name
  17. # save
  18. with open(toml_path, "w") as f:
  19. f.write(tomlkit.dumps(t))