From 7bfa20198afce7830ff2daccb4400f03c9f40e0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 11 Sep 2018 21:49:16 +0100 Subject: [PATCH] fetchcargo: add type checking to cargo-vendor-normalise.py --- .../rust/cargo-vendor-normalise.py | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/pkgs/build-support/rust/cargo-vendor-normalise.py b/pkgs/build-support/rust/cargo-vendor-normalise.py index 194636968564..2d7a18957184 100755 --- a/pkgs/build-support/rust/cargo-vendor-normalise.py +++ b/pkgs/build-support/rust/cargo-vendor-normalise.py @@ -1,32 +1,41 @@ #!/usr/bin/env python -import toml import sys -def escape(s): - return '"'+s.replace('"', r'\"').replace("\n", r"\n").replace("\\", "\\\\")+'"' - -data = toml.load(sys.stdin) - -assert list(data.keys()) == [ "source" ] - -# this value is non deterministic -data["source"]["vendored-sources"]["directory"] = "@vendor@" - -result = "" -inner = data["source"] -for source in sorted(inner.keys()): - result += '[source.{}]\n'.format(escape(source)) - if source == "vendored-sources": - result += '"directory" = "@vendor@"\n' - else: - for key in sorted(inner[source].keys()): - result += '{} = {}\n'.format(escape(key), escape(inner[source][key])) - result += "\n" - -real = toml.loads(result) -assert real == data, "output = {} while input = {}".format(real, data) - -print(result) +import toml +def quote(s: str) -> str: + escaped = s.replace('"', r"\"").replace("\n", r"\n").replace("\\", "\\\\") + return '"{}"'.format(escaped) + + +def main() -> None: + data = toml.load(sys.stdin) + + assert list(data.keys()) == ["source"] + + # this value is non deterministic + data["source"]["vendored-sources"]["directory"] = "@vendor@" + + lines = [] + inner = data["source"] + for source, attrs in sorted(inner.items()): + lines.append("[source.{}]".format(quote(source))) + if source == "vendored-sources": + lines.append('"directory" = "@vendor@"\n') + else: + for key, value in sorted(attrs.items()): + attr = "{} = {}".format(quote(key), quote(value)) + lines.append(attr) + lines.append("") + + result = "\n".join(lines) + real = toml.loads(result) + assert real == data, "output = {} while input = {}".format(real, data) + + print(result) + + +if __name__ == "__main__": + main()