/
index_source_files.py
71 lines (53 loc) · 1.53 KB
/
index_source_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!env python
from elasticsearch import Elasticsearch
import ctags
import commands as c
from ctags import CTags, TagEntry
import sys
import os
try:
ctags = sys.argv[1]
projectdir = sys.argv[2]
except IndexError:
print "Need a exurbitant ctags binary and a project dir"
sys.exit(1)
stat, output = c.getstatusoutput("%s -f /tmp/ctags-blub -R %s" % (ctags, projectdir))
if stat != 0:
print "Error when running ctags: %s" % output
sys.exit(1)
try:
tagfile = CTags("/tmp/ctags-blub")
except:
print "Error when reading ctags file"
sys.exit(1)
# collect all tags for a sorted tag
fileCollection = dict()
entry = TagEntry()
status = tagfile.first(entry)
fileCollection[entry['file']] = [(entry['name'], entry['kind'], entry['lineNumber'], entry['pattern'])]
while tagfile.next(entry):
addEntry = [(entry['name'], entry['kind'], entry['lineNumber'], entry['pattern'])]
try:
fileCollection[entry['file']] += addEntry
except KeyError:
fileCollection[entry['file']] = addEntry
es = Elasticsearch()
for file in fileCollection:
f = open(file, "r")
tags = []
for tag in fileCollection[file]:
print tag
tags += [{
"tag": tag[0],
"kind":tag[1],
"sourceline":tag[2],
"pattern":tag[3]
}]
es.create(index="documents", doc_type="doc", body={
"path":file,
"content":f.read(),
"type": "sourcefile",
"path": file,
"ctags": tags
# TODO source tags
})