Skip to content

Instantly share code, notes, and snippets.

@arjunattam
Created March 14, 2019 23:54
Show Gist options
  • Save arjunattam/96517f6c334192c8cdf43fd0ea8de895 to your computer and use it in GitHub Desktop.
Save arjunattam/96517f6c334192c8cdf43fd0ea8de895 to your computer and use it in GitHub Desktop.
# This script finds vertex ids in an LSIF output file that have been referenced
# in edge declarations (as outV or inV), before being declared as a vertex.
#
# Usage
# To run on lsif.json file:
# $ python validate-lsif.py lsif.json
#
import json
import sys
def get_v_id(v_json):
return v_json['id']
def get_e_ids(e_json):
return [e_json['outV'], e_json['inV']]
def read_file_lines(name):
with open(name) as f:
return f.readlines()
def trim_braces(contents):
return contents[1:-1]
def clean_string(line_str):
l = line_str.strip()
if l[-1] == ',':
return l[:-1]
else:
return l
def find_invalid_ids(lines):
vertex_ids = []
for to_parse in lines:
line = json.loads(clean_string(to_parse))
if line['type'] == 'vertex':
vertex_ids.append(get_v_id(line))
elif line['type'] == 'edge':
[i, o] = get_e_ids(line)
if i not in vertex_ids:
print(i)
if o not in vertex_ids:
print(o)
if __name__ == '__main__':
contents = trim_braces((read_file_lines(sys.argv[1])))
find_invalid_ids(contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment