forked from piaw/google-gtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_gentags.py
executable file
·140 lines (114 loc) · 3.85 KB
/
local_gentags.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python2.4
#
# Copyright 2007 Google Inc. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""A stripped down local version of gentags.
Outputs the name of a file in /tmp directory that contains the tag file that
can be loaded by local gtags server.
"""
__author__ = '[email protected] (Stephen Chen)'
import commands
import optparse
import os
option_parser = optparse.OptionParser()
option_parser.add_option(
'--etags',
dest='etags',
default='/usr/pubsw/bin/etags',
help='Path to etags binary')
option_parser.add_option(
'--etags_to_tags',
dest='etags_to_tags',
default='./etags_to_tags.py',
help='Path to etags_to_tags binary')
option_parser.add_option(
'--rtags',
dest='rtags',
default='./rtags.py',
help='Path to rtags binary')
option_parser.add_option(
'--callgraph',
dest='callgraph',
default=False,
help='Whether to generate callgraph tags.')
option_parser.add_option(
"--output_file",
dest="output_file",
default="",
help="Tags output file.")
option_parser.add_option(
'--tmp_file_prefix',
dest='tmp_file_prefix',
default='/tmp/gtags.',
help='Prefix for temp files.')
(options, argv) = option_parser.parse_args()
def GenerateUUID():
"""Call unix uuid command to generate a universal unique id.
Returns: string containing the output of uuidgen program.
"""
return commands.getoutput('uuidgen')
def GenerateTmpFileName():
"""Generate a filename in /tmp dir that does not conflict with any existing
files.
Returns: string containing a filename in /tmp dir that is safe to write to.
"""
while True:
uuid = GenerateUUID()
filename = options.tmp_file_prefix + uuid
if not os.path.exists(filename):
return filename
def Index(files, callgraph, output_file):
"""Call etags/rtags and etags_to_tags on the specified files.
Outputs results to the specified output_file.
Args:
files: list of files to be indexed.
callgraph: whether to generate callgraph tags.
output_file: file to output to.
Returns: name of a file that contains the tags.
"""
tmp_output = GenerateTmpFileName()
if callgraph:
cmd = '%s --output=%s --append %s' % (options.rtags, tmp_output,
' '.join(files))
callers_string = '--callers'
else:
cmd = '%s -o %s -a %s' % (options.etags, tmp_output,
' '.join(files))
callers_string = '--nocallers'
print cmd
(stdin, stdout, stderr) = os.popen3(cmd)
(pid, status) = os.wait()
if status != 0:
print 'tags generation failed: %s' % stderr.read()
return ''
trans_cmd = ('%s --input_file=%s --output_file=%s --guess_language %s' %
(options.etags_to_tags, tmp_output, output_file, callers_string))
print trans_cmd
(stdin, stdout, stderr) = os.popen3(trans_cmd)
(pid, status) = os.wait()
if status != 0:
print 'tags translation failed: %s' % stderr.read()
return ''
os.remove(tmp_output)
return output_file
def main():
if options.output_file:
output_file = options.output_file
else:
output_file = GenerateTmpFileName()
print Index(argv, options.callgraph, output_file)
if __name__ == '__main__':
main()