3
0
Fork 0
forked from mirrors/nixpkgs

fetchcargo: add type checking to cargo-vendor-normalise.py

This commit is contained in:
Jörg Thalheim 2018-09-11 21:49:16 +01:00 committed by Symphorien Gibol
parent f20b229aa1
commit 7bfa20198a

View file

@ -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()