except: sys.exit(1) entry = TagEntry() status = tagFile.setSortType(ctags.TAG_SORTED) status = tagFile.first(entry) print(tagFile["name"]) print(tagFile["author"]) print(tagFile["format"]) if status: print(entry["name"]) print(entry["kind"]) if tagFile.find(entry, b"find", ctags.TAG_PARTIALMATCH | ctags.TAG_IGNORECASE): print("found") print(entry["lineNumber"]) print(entry["pattern"]) print(entry["kind"]) status = tagFile.findNext(entry) if status: print(entry["lineNumber"]) print(entry["pattern"]) print(entry["kind"]) if tagFile.next(entry): print(entry["lineNumber"]) print(entry["pattern"]) print(entry["kind"])
def parse_ctags(tagFile): tf = CTags(tagFile) e = TagEntry() results = [] s = tf.first(e) while s: eP = {'name': e['name'], 'file': e['file'], 'pattern': e['pattern'], 'lineNumber': e['lineNumber'], 'kind': e['kind'], 'fileScope': e['fileScope']} results.append(eP) s = tf.next(e) return sorted(results, cmp = lambda x, y: cmp(x['file'].lower(), y['file'].lower()) if x['file'] != y['file'] else cmp(x['lineNumber'], y['lineNumber']))
def parse_ctags(): '''Returns a list of all the functions and their arguments found by ctags''' try: tf = CTags('tags') except: print "Unable to find tags file!" sys.exit(-1) entry = TagEntry() l = [] if 0 == tf.first(entry): return [] while True: l.append(parse_pattern(entry['pattern'])) if 0 == tf.next(entry): break return l
def _getFunctionLines(self, file_layout_src, file_commit): ''' computes the line numbers of each function in the file ''' ''' - Input - file_name: original name of the file, used only to determine the programming language (ie. file.c is a c-language file) file_layout_scr: dictionary with key=line number value = line of code file_commit: fileCommit instance where the results will be stored - Description - The file_layout is used to construct a source code file that can be parsed by ctags to generate a ctags file. The ctags file is then accessed to extract the function tags and line numbers to be save in the fileCommit object ''' # grab the file extension to determine the language of the file fileExt = os.path.splitext(file_commit.filename)[1] # temporary file where we write transient data needed for ctags srcFile = tempfile.NamedTemporaryFile(suffix=fileExt) tagFile = tempfile.NamedTemporaryFile() # generate a source code file from the file_layout_src dictionary # and save it to a temporary location for line in file_layout_src: srcFile.write(line) srcFile.flush() # run ctags analysis on the file to create a tags file cmd = "ctags-exuberant -f {0} --fields=nk {1}".format( tagFile.name, srcFile.name).split() output = execute_command(cmd).splitlines() # parse ctags try: tags = CTags(tagFile.name) except: log.critical("failure to load ctags file") raise Error("failure to load ctags file") # locate line numbers and structure names entry = TagEntry() funcLines = {} # select the language structures we are interested in identifying # f = functions, s = structs, c = classes, n = namespace # p = function prototype, g = enum, d = macro, t= typedef, u = union structures = ["f", "s", "c", "n", "p", "g", "d", "t", "u"] # TODO: investigate other languages and how ctags assigns the structure # tags, we may need more languages specific assignments in # addition to java and c# files, use "ctags --list-kinds" to # see all tag meanings per language if fileExt in (".java", ".j", ".jav", ".cs", ".js"): structures.append("m") # methods structures.append("i") # interface elif fileExt in (".php"): structures.append("i") # interface structures.append("j") # functions elif fileExt in (".py"): structures.append("m") # class members while (tags.next(entry)): if entry['kind'] in structures: funcLines[int(entry['lineNumber'])] = entry['name'] # clean up temporary files srcFile.close() tagFile.close() # save result to the file commit instance file_commit.setFunctionLines(funcLines)
def update_type_map(type_map, ctags_fname): logger.debug("processing %s ...", ctags_fname) tag = CTags(ctags_fname.encode()) tag.setSortType(ctags.TAG_SORTED) entry = TagEntry() # First traverse all entries status = tag.first(entry) while status: name = entry["name"].decode() kind = entry["kind"].decode() typeref = (entry[b"typeref"] or b"").decode() pattern = entry["pattern"].decode() # TODO: Check multiple declaration if name in type_map: status = tag.next(entry) continue # TODO: handle macro properly. currently, assume macros are integers. # Also, skip allocation. if kind == "macro" or "=" in pattern: status = tag.next(entry) continue if kind in ["func", "proc", "function", "procedure", "method"]: ret_type = "func" elif kind.startswith("enum"): ret_type = "enum" elif kind == "union": ret_type = "union" elif kind.startswith("struct"): ret_type = "struct" elif kind.startswith("class"): ret_type = "struct" elif kind == "label": ret_type = ret_type elif kind in ["label", "typedef", "member", "variable"]: if typeref: ret_type = typeref.split(":")[0] else: ret_type = pattern[:pattern.rindex(name)].rstrip() else: status = tag.next(entry) continue ret_type = sanitize(ret_type) if "(" in ret_type: ret_type = "func" if "*" in ret_type: ret_type + " *" type_map[name] = ret_type status = tag.next(entry) # Now check until no update exists while True: is_updated = False status = tag.first(entry) while status: name = entry["name"].decode() kind = entry["kind"].decode() typeref = (entry[b"typeref"] or b"").decode() pattern = entry["pattern"].decode() # No need to handle a macro as it is already replaced by a constant. # Also, skip allocation. if kind == "macro" or "=" in pattern: status = tag.next(entry) continue if name not in type_map: ret_type = "int" else: ret_type = type_map[name] ret_type = ret_type.split()[0] # remove pointer for here while ret_type in type_map and ret_type != type_map[ret_type]: ret_type = ret_type.split()[0] # remove pointer for here ret_type = type_map[ret_type] if ret_type not in type_map: ret_type = "int" # use a single '*' for denoting a pointer if "*" not in ret_type and "*" in type_map[name]: ret_type = ret_type + " *" if ret_type != type_map[name]: type_map[name] = ret_type is_updated = True status = tag.next(entry) if is_updated == False: break return None
def entities(): tagFile = CTags('TAGS') entry = TagEntry() while tagFile.next(entry): yield entry
def _getFunctionLines(self, file_layout_src, file_commit): ''' computes the line numbers of each function in the file ''' ''' - Input - file_name: original name of the file, used only to determine the programming language (ie. file.c is a c-language file) file_layout_scr: dictionary with key=line number value = line of code file_commit: fileCommit instance where the results will be stored - Description - The file_layout is used to construct a source code file that can be parsed by ctags to generate a ctags file. The ctags file is then accessed to extract the function tags and line numbers to be save in the fileCommit object ''' # grab the file extension to determine the language of the file fileExt = os.path.splitext(file_commit.filename)[1] # temporary file where we write transient data needed for ctags srcFile = tempfile.NamedTemporaryFile(suffix=fileExt) tagFile = tempfile.NamedTemporaryFile() # generate a source code file from the file_layout_src dictionary # and save it to a temporary location for line in file_layout_src: srcFile.write(line) srcFile.flush() # run ctags analysis on the file to create a tags file cmd = "ctags-exuberant -f {0} --fields=nk {1}".format(tagFile.name, srcFile.name).split() output = execute_command(cmd).splitlines() # parse ctags try: tags = CTags(tagFile.name) except: log.critical("failure to load ctags file") raise Error("failure to load ctags file") # locate line numbers and structure names entry = TagEntry() funcLines = {} # select the language structures we are interested in identifying # f = functions, s = structs, c = classes, n = namespace # p = function prototype, g = enum, d = macro, t= typedef, u = union structures = ["f", "s", "c", "n", "p", "g", "d", "t", "u"] # TODO: investigate other languages and how ctags assigns the structure # tags, we may need more languages specific assignments in # addition to java and c# files, use "ctags --list-kinds" to # see all tag meanings per language if fileExt in (".java", ".j", ".jav", ".cs", ".js"): structures.append("m") # methods structures.append("i") # interface elif fileExt in (".php"): structures.append("i") # interface structures.append("j") # functions elif fileExt in (".py"): structures.append("m") # class members while(tags.next(entry)): if entry['kind'] in structures: funcLines[int(entry['lineNumber'])] = entry['name'] # clean up temporary files srcFile.close() tagFile.close() # save result to the file commit instance file_commit.setFunctionLines(funcLines)
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],
tagFile = CTags('tags') except: sys.exit(1) entry = TagEntry() patterns = {} files = {} # TAG_PARTIALMATCH - begin with # TAG_FULLMATCH - full length matching # TAG_IGNORECASE - disable binary search # TAG_OBSERVECASE - case sensitive and allowed binary search to perform # Find the next tag matching the name and options supplied to the # most recent call to tagFile.find(). (replace the entry if found) status = tagFile.next(entry) while status: fileName, pattern = entry['file'], entry['name'] if fileName not in files: files[fileName] = [pattern] else: files[fileName] += [pattern] if pattern not in patterns: patterns[pattern] = [fileName] else: patterns[pattern] += [fileName] status = tagFile.next(entry) patternGraph = pgv.AGraph()
#!/usr/bin/env python import ctags from ctags import CTags, TagEntry import sys try: tagFile = CTags('tags') except: print 'Error on open tags' sys.exit(1) entry = TagEntry() if len(sys.argv) == 2: if tagFile.find(entry, sys.argv[1], ctags.TAG_FULLMATCH): print entry['file'], entry['lineNumber'] else: print 'Symbol', sys.argv[1], 'not found' else: stat = tagFile.first(entry) while stat: print entry['name'], entry['file'], entry['lineNumber'], entry['kind'] stat = tagFile.next(entry)
entry = TagEntry() status = tagFile.setSortType(ctags.TAG_SORTED) status = tagFile.first(entry) print(tagFile['name']) print(tagFile['author']) print(tagFile['format']) if status: print(entry['name']) print(entry['kind']) if tagFile.find(entry, b'find', ctags.TAG_PARTIALMATCH | ctags.TAG_IGNORECASE): print('found') print(entry['lineNumber']) print(entry['pattern']) print(entry['kind']) status = tagFile.findNext(entry) if status: print(entry['lineNumber']) print(entry['pattern']) print(entry['kind']) if tagFile.next(entry): print(entry['lineNumber']) print(entry['pattern']) print(entry['kind'])