Skip to content

Instantly share code, notes, and snippets.

@sgraham
Created November 18, 2020 21:46
Show Gist options
  • Save sgraham/bd9ffee312f307d5f417019a9c0f0777 to your computer and use it in GitHub Desktop.
Save sgraham/bd9ffee312f307d5f417019a9c0f0777 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import json
import sys
def find_all(data, typ, val):
result = []
def rec(root):
if root.get("type") == typ and root.get("value") == val:
result.append(root)
child = root.get("child")
if child:
assert isinstance(child, list), child
for c in child:
rec(c)
rec(data)
return result
def main():
with open(sys.argv[1], "r") as f:
data = json.loads(f.read())
source_sets = find_all(data, "FUNCTION", "source_set")
for ss in source_sets:
# 0 is name of source_set, 1 is body.
body = ss["child"][1]
assert body["type"] == "BLOCK"
block_elems = body["child"]
# Change `deps = [...]` to `deps += [...]`.
for be in block_elems:
if be.get("type") == "BINARY" and be.get("value") == "=":
lhs = be.get("child")[0]
if lhs.get("type") == "IDENTIFIER" and lhs.get("value") == "deps":
be["value"] = "+="
with open(sys.argv[2], "w") as f:
json.dump(data, f)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment