Skip to content

Instantly share code, notes, and snippets.

@dylanscott
Created August 12, 2022 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanscott/698b25c64e7c0f37d9960e11f8c37595 to your computer and use it in GitHub Desktop.
Save dylanscott/698b25c64e7c0f37d9960e11f8c37595 to your computer and use it in GitHub Desktop.
Canonicalize content order in poetry.lock [package.extras]
import tomlkit
import tomlkit.items
from pathlib import Path
def canonicalize_extras(entry):
canonicalized = {}
for key in sorted(entry.keys()):
canonicalized[key] = sorted(entry[key])
return canonicalized
def main():
poetry_lock = Path(__file__).parent / "poetry.lock"
parsed = tomlkit.parse(poetry_lock.read_text())
for package in parsed["package"]:
if "extras" in package:
package["extras"] = canonicalize_extras(package["extras"])
package["extras"].append(None, tomlkit.items.Whitespace("\n"))
poetry_lock.write_text(tomlkit.dumps(parsed))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment