def processIndexCommand(args): workingDirectory = os.getcwd() if not indexer.isSourcetrailDBVersionCompatible(True): return databaseFilePath = args.database_file_path if not os.path.isabs(databaseFilePath): databaseFilePath = os.path.join(workingDirectory, databaseFilePath) sourceFilePath = args.source_file_path if not os.path.isabs(sourceFilePath): sourceFilePath = os.path.join(workingDirectory, sourceFilePath) environmentPath = args.environment_path if environmentPath is not None and not os.path.isabs(environmentPath): environmentPath = os.path.join(workingDirectory, environmentPath) if not srctrl.open(databaseFilePath): print('ERROR: ' + srctrl.getLastError()) if args.clear: if args.verbose: print('INFO: Clearing database...') if not srctrl.clear(): print('ERROR: ' + srctrl.getLastError()) else: if args.verbose: print('INFO: Clearing done.') if args.verbose: if srctrl.isEmpty(): print('INFO: Loaded database is empty.') else: print('INFO: Loaded database contains data.') srctrl.beginTransaction() indexSourceFile(sourceFilePath, environmentPath, workingDirectory, args.verbose, args.shallow) srctrl.commitTransaction() if not srctrl.close(): print('ERROR: ' + srctrl.getLastError())
def __init__(self, client, sourceFilePath, sourceFileContent=None, sysPath=None): self.client = client self.sourceFilePath = sourceFilePath if sourceFilePath != _virtualFilePath: self.sourceFilePath = os.path.abspath(self.sourceFilePath) self.sourceFileName = os.path.split(self.sourceFilePath)[-1] self.sourceFileContent = sourceFileContent packageRootPath = os.path.dirname(self.sourceFilePath) while os.path.exists(os.path.join(packageRootPath, '__init__.py')): packageRootPath = os.path.dirname(packageRootPath) self.sysPath = [packageRootPath] if sysPath is not None: self.sysPath.extend(sysPath) # else: # baseSysPath = evaluator.project._get_base_sys_path(self.environment) # baseSysPath.sort(reverse=True) # self.sysPath.extend(baseSysPath) self.sysPath = list(filter(None, self.sysPath)) self.contextStack = [] self.referenceKindStack = [] fileId = self.client.recordFile(self.sourceFilePath) if fileId == 0: print('ERROR: ' + srctrl.getLastError()) self.client.recordFileLanguage(fileId, 'python') self.contextStack.append( ContextInfo(fileId, ContextType.FILE, self.sourceFilePath, None)) moduleNameHierarchy = self.getNameHierarchyFromModuleFilePath( self.sourceFilePath) if moduleNameHierarchy is not None: moduleId = self.client.recordSymbol(moduleNameHierarchy) self.client.recordSymbolDefinitionKind(moduleId, srctrl.DEFINITION_EXPLICIT) self.client.recordSymbolKind(moduleId, srctrl.SYMBOL_MODULE) self.contextStack.append( ContextInfo(moduleId, ContextType.MODULE, moduleNameHierarchy.getDisplayString(), None))
def main(): parser = argparse.ArgumentParser(description="SourcetrailDB JS API Example") parser.add_argument("--database-file-path", help="path to the generated Sourcetrail database file", type=str, required=True) parser.add_argument("--source-file-path", help="path to the source file to index", type=str, required=True) parser.add_argument("--database-version", help="database version of the invoking Sourcetrail binary", type=int, required=False, default=0) args = parser.parse_args() databaseFilePath = args.database_file_path sourceFilePath = args.source_file_path.replace("\\", "/") dbVersion = args.database_version print("SourcetrailDB JS API Example") print("Supported database version: " + str(srctrl.getSupportedDatabaseVersion())) if dbVersion > 0 and dbVersion != srctrl.getSupportedDatabaseVersion(): print("ERROR: Only supports database version: " + str(srctrl.getSupportedDatabaseVersion()) + ". Requested version: " + str(dbVersion)) return 1 if not srctrl.open(databaseFilePath): print("ERROR: " + srctrl.getLastError()) return 1 print("Clearing loaded database now...") srctrl.clear() print("start indexing") srctrl.beginTransaction() fileId = srctrl.recordFile(sourceFilePath) # srctrl.recordFileLanguage(fileId, "python") if len(srctrl.getLastError()) > 0: # Early exit print("ERROR: " + srctrl.getLastError()) return 1 with open('../data/ast.json', 'r') as f: ast = json.load(f) for body in ast['body']: if body["type"] == "FunctionDeclaration": symbolId = srctrl.recordSymbol( '{ "name_delimiter": ".", "name_elements": [ ' '{ "prefix": "", "name": "foo", "postfix": "" } ' '] }') # srctrl.recordSymbolDefinitionKind(symbolId, srctrl.DEFINITION_EXPLICIT) # srctrl.recordSymbolKind(symbolId, srctrl.SYMBOL_CLASS) # srctrl.recordSymbolLocation(symbolId, fileId, 2, 7, 2, 12) # srctrl.recordSymbolScopeLocation(symbolId, fileId, 2, 1, 7, 1) # memberId = srctrl.recordSymbol( # '{ "name_delimiter": ".", "name_elements": [ ' # '{ "prefix": "", "name": "MyType", "postfix": "" }, ' # '{ "prefix": "", "name": "my_member", "postfix": "" } ' # '] }') # srctrl.recordSymbolDefinitionKind(memberId, srctrl.DEFINITION_EXPLICIT) # srctrl.recordSymbolKind(memberId, srctrl.SYMBOL_FIELD) # srctrl.recordSymbolLocation(memberId, fileId, 4, 2, 4, 10) # methodId = srctrl.recordSymbol( # '{ "name_delimiter": ".", "name_elements": [ ' # '{ "prefix": "", "name": "MyType", "postfix": "" }, ' # '{ "prefix": "", "name": "my_method", "postfix": "" } ' # '] }') # srctrl.recordSymbolDefinitionKind(methodId, srctrl.DEFINITION_EXPLICIT) # srctrl.recordSymbolKind(methodId, srctrl.SYMBOL_METHOD) # srctrl.recordSymbolLocation(methodId, fileId, 6, 6, 6, 14) # srctrl.recordSymbolScopeLocation(methodId, fileId, 6, 1, 7, 1) # useageId = srctrl.recordReference(methodId, memberId, srctrl.REFERENCE_USAGE) # srctrl.recordReferenceLocation(useageId, fileId, 7, 10, 7, 18) srctrl.commitTransaction() if len(srctrl.getLastError()) > 0: print("ERROR: " + srctrl.getLastError()) return 1 if not srctrl.close(): print("ERROR: " + srctrl.getLastError()) return 1 print("done") return 0
def main(): parser = argparse.ArgumentParser(description="SourcetrailDB Python API Example") parser.add_argument("--database-file-path", help="path to the generated Sourcetrail database file", type=str, required=True) parser.add_argument("--source-file-path", help="path to the source file to index", type=str, required=True) parser.add_argument("--database-version", help="database version of the invoking Sourcetrail binary", type=int, required=False, default=0) args = parser.parse_args() databaseFilePath = args.database_file_path sourceFilePath = args.source_file_path dbVersion = args.database_version print("SourcetrailDB Python API Example") print("Supported database version: " + str(srctrl.getSupportedDatabaseVersion())) if dbVersion > 0 and dbVersion != srctrl.getSupportedDatabaseVersion(): print("ERROR: Only supports database version: " + str(srctrl.getSupportedDatabaseVersion()) + ". Requested version: " + str(dbVersion)) return 1 if not srctrl.open(databaseFilePath): print("ERROR: " + srctrl.getLastError()) return 1 if srctrl.isEmpty(): print("Loaded database is empty.") else: print("Loaded database contains data.") print("start indexing") srctrl.beginTransaction() fileId = srctrl.recordFile(sourceFilePath) srctrl.recordFileLanguage(fileId, "python") if len(srctrl.getLastError()) > 0: print("ERROR: " + srctrl.getLastError()) return 1 symbolId = srctrl.recordSymbol( '{ "name_delimiter": ".", "name_elements": [ ' '{ "prefix": "", "name": "MyType", "postfix": "" } ' '] }') srctrl.recordSymbolDefinitionKind(symbolId, srctrl.DEFINITION_EXPLICIT) srctrl.recordSymbolKind(symbolId, srctrl.SYMBOL_CLASS) srctrl.recordSymbolLocation(symbolId, fileId, 2, 7, 2, 12) srctrl.recordSymbolScopeLocation(symbolId, fileId, 2, 1, 7, 1) memberId = srctrl.recordSymbol( '{ "name_delimiter": ".", "name_elements": [ ' '{ "prefix": "", "name": "MyType", "postfix": "" }, ' '{ "prefix": "", "name": "my_member", "postfix": "" } ' '] }') srctrl.recordSymbolDefinitionKind(memberId, srctrl.DEFINITION_EXPLICIT) srctrl.recordSymbolKind(memberId, srctrl.SYMBOL_FIELD) srctrl.recordSymbolLocation(memberId, fileId, 4, 2, 4, 10) methodId = srctrl.recordSymbol( '{ "name_delimiter": ".", "name_elements": [ ' '{ "prefix": "", "name": "MyType", "postfix": "" }, ' '{ "prefix": "", "name": "my_method", "postfix": "" } ' '] }') srctrl.recordSymbolDefinitionKind(methodId, srctrl.DEFINITION_EXPLICIT) srctrl.recordSymbolKind(methodId, srctrl.SYMBOL_METHOD) srctrl.recordSymbolLocation(methodId, fileId, 6, 6, 6, 14) srctrl.recordSymbolScopeLocation(methodId, fileId, 6, 1, 7, 1) useageId = srctrl.recordReference(methodId, memberId, srctrl.REFERENCE_USAGE) srctrl.recordReferenceLocation(useageId, fileId, 7, 10, 7, 18) srctrl.commitTransaction() if len(srctrl.getLastError()) > 0: print("ERROR: " + srctrl.getLastError()) return 1 if not srctrl.close(): print("ERROR: " + srctrl.getLastError()) return 1 print("done") return 0