diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f79987..3092b96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,7 +80,7 @@ if (BUILD_EXAMPLES) add_subdirectory("${CMAKE_SOURCE_DIR}/examples/cpp_poetry_indexer") if (BUILD_BINDINGS_PYTHON) - add_subdirectory("${CMAKE_SOURCE_DIR}/examples/python_binding_example") + add_subdirectory("${CMAKE_SOURCE_DIR}/examples/python_api_example") else() message(STATUS "Skip python binding sample. You can enable building it by setting 'BUILD_BINDINGS_PYTHON' to 'ON'.") endif() diff --git a/core/src/StorageTypes.cpp b/core/src/StorageTypes.cpp deleted file mode 100644 index 243bf61..0000000 --- a/core/src/StorageTypes.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* -* Copyright 2018 Coati Software KG -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include "SymbolKind.h" - -#include "NodeKind.h" - -namespace sourcetrail -{ - NodeKind symbolKindToNodeKind(SymbolKind v) - { - switch (v) - { - case SYMBOL_TYPE: - return NODE_TYPE; - case SYMBOL_BUILTIN_TYPE: - return NODE_BUILTIN_TYPE; - case SYMBOL_MODULE: - return NODE_MODULE; - case SYMBOL_NAMESPACE: - return NODE_NAMESPACE; - case SYMBOL_PACKAGE: - return NODE_PACKAGE; - case SYMBOL_STRUCT: - return NODE_STRUCT; - case SYMBOL_CLASS: - return NODE_CLASS; - case SYMBOL_INTERFACE: - return NODE_INTERFACE; - case SYMBOL_ANNOTATION: - return NODE_ANNOTATION; - case SYMBOL_GLOBAL_VARIABLE: - return NODE_GLOBAL_VARIABLE; - case SYMBOL_FIELD: - return NODE_FIELD; - case SYMBOL_FUNCTION: - return NODE_FUNCTION; - case SYMBOL_METHOD: - return NODE_METHOD; - case SYMBOL_ENUM: - return NODE_ENUM; - case SYMBOL_ENUM_CONSTANT: - return NODE_ENUM_CONSTANT; - case SYMBOL_TYPEDEF: - return NODE_TYPEDEF; - case SYMBOL_TEMPLATE_PARAMETER: - return NODE_TEMPLATE_PARAMETER; - case SYMBOL_TYPE_PARAMETER: - return NODE_TYPE_PARAMETER; - case SYMBOL_MACRO: - return NODE_MACRO; - case SYMBOL_UNION: - return NODE_UNION; - } - return NODE_UNKNOWN; - } -} diff --git a/examples/cpp_api_example/README.md b/examples/cpp_api_example/README.md new file mode 100644 index 0000000..31753b4 --- /dev/null +++ b/examples/cpp_api_example/README.md @@ -0,0 +1,24 @@ + +## C++ API Example + +### Requirements + +* CMake +* C++ Compiler + +### Building + +* Run cmake with `BUILD_EXAMPLES=ON` +* Build target `cpp_api_example` + +### Running from Command Line + +Invoke binary located in the build directory: + +``` +$ cpp_api_example path/to/database_file.srctrldb path/to/examples/cpp_api_example/data/example.cpp +``` + +### Running with Sourcetrail + +Open `cpp_api_example.srctrlprj` located in the build directory with Sourcetrail and index the project. diff --git a/examples/cpp_api_example/cpp_api_example.srctrlprj.in b/examples/cpp_api_example/cpp_api_example.srctrlprj.in index 4767ec6..71d5542 100644 --- a/examples/cpp_api_example/cpp_api_example.srctrlprj.in +++ b/examples/cpp_api_example/cpp_api_example.srctrlprj.in @@ -2,7 +2,7 @@ - ./cpp_api_example $DB_PATH $DB_VERSION $SOURCE_PATH + ./cpp_api_example $DB_PATH $SOURCE_PATH $DB_VERSION Custom Command Source Group .cpp diff --git a/examples/cpp_api_example/src/main.cpp b/examples/cpp_api_example/src/main.cpp index fe5b0b3..0830064 100644 --- a/examples/cpp_api_example/src/main.cpp +++ b/examples/cpp_api_example/src/main.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -7,39 +8,48 @@ int main(int argc, const char *argv[]) { sourcetrail::SourcetrailDBWriter dbWriter; - std::cout << "SourcetrailDB Poetry Indexer" << std::endl; + std::cout << "\nSourcetrailDB C++ API Example" << std::endl; std::cout << std::endl; std::cout << "SourcetrailDB version: " << dbWriter.getVersionString() << std::endl; std::cout << "Supported database version: " << dbWriter.getSupportedDatabaseVersion() << std::endl; std::cout << std::endl; - if (argc != 4) + if (argc < 3 || argc > 4) { - std::cout << "usage: poetry_indexer "; + std::cout << "usage: cpp_api_example "; } std::string dbPath = argv[1]; - int dbVersion = std::stoi(argv[2]); - std::string sourcePath = argv[3]; + std::string sourcePath = argv[2]; - if (dbVersion != dbWriter.getSupportedDatabaseVersion()) + int dbVersion = 0; + if (argc == 4) { - std::cout << "error: binary only supports database version: " << dbWriter.getSupportedDatabaseVersion() + char* end; + dbVersion = strtol(argv[3], &end, 10); + } + + if (dbVersion && dbVersion != dbWriter.getSupportedDatabaseVersion()) + { + std::cerr << "error: binary only supports database version: " << dbWriter.getSupportedDatabaseVersion() << ". Requested version: " << dbVersion << std::endl; return 1; } // open database by passing .srctrldb or .srctrldb_tmp path + std::cout << "Opening Database: " << dbPath << std::endl; if (!dbWriter.open(dbPath)) { - std::cout << "error: " << dbWriter.getLastError() << std::endl; + std::cerr << "error: " << dbWriter.getLastError() << std::endl; return 1; } + std::cout << "Starting Indexing..." << std::endl; + // start recording with faster speed if (!dbWriter.beginTransaction()) { - std::cout << "error: " << dbWriter.getLastError() << std::endl; + std::cerr << "error: " << dbWriter.getLastError() << std::endl; return 1; } @@ -108,22 +118,24 @@ int main(int argc, const char *argv[]) // record error - dbWriter.recordError("Really? You missed that \";\" again?", false, { fileId, 22, 1, 22, 1 }); + dbWriter.recordError("Really? You missed that \";\" again? (intentional error)", false, { fileId, 22, 1, 22, 1 }); // end recording if (!dbWriter.commitTransaction()) { - std::cout << "error: " << dbWriter.getLastError() << std::endl; + std::cerr << "error: " << dbWriter.getLastError() << std::endl; return 1; } // check for errors before finishing if (dbWriter.getLastError().size()) { - std::cout << "error: " << dbWriter.getLastError() << std::endl; + std::cerr << "error: " << dbWriter.getLastError() << std::endl; return 1; } + std::cout << "done!" << std::endl; + return 0; } diff --git a/examples/cpp_poetry_indexer/README.md b/examples/cpp_poetry_indexer/README.md new file mode 100644 index 0000000..df3dd20 --- /dev/null +++ b/examples/cpp_poetry_indexer/README.md @@ -0,0 +1,24 @@ + +## Poetry Indexer Example + +### Requirements + +* CMake +* C++ Compiler + +### Building + +* Run cmake with `BUILD_EXAMPLES=ON` +* Build target `poetry_indexer` + +### Running from Command Line + +Invoke binary located in the build directory: + +``` +$ poetry_indexer path/to/database_file.srctrldb path/to/examples/cpp_poetry_indexer/data/e_e_cummings_-_in_just-.txt +``` + +### Running with Sourcetrail + +Open `poetry_indexer.srctrlprj` located in the build directory with Sourcetrail and index the project. diff --git a/examples/cpp_poetry_indexer/poetry_indexer.srctrlprj.in b/examples/cpp_poetry_indexer/poetry_indexer.srctrlprj.in index adbf942..04a7a6d 100644 --- a/examples/cpp_poetry_indexer/poetry_indexer.srctrlprj.in +++ b/examples/cpp_poetry_indexer/poetry_indexer.srctrlprj.in @@ -2,7 +2,7 @@ - ./poetry_indexer $DB_PATH $DB_VERSION $SOURCE_PATH + ./poetry_indexer $DB_PATH $SOURCE_PATH $DB_VERSION @POETRY_INDEXER_DATA_PATH@/COPYRIGHT.txt diff --git a/examples/cpp_poetry_indexer/src/main.cpp b/examples/cpp_poetry_indexer/src/main.cpp index d11ddab..a781b5f 100644 --- a/examples/cpp_poetry_indexer/src/main.cpp +++ b/examples/cpp_poetry_indexer/src/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -23,51 +24,60 @@ int main(int argc, const char *argv[]) { sourcetrail::SourcetrailDBWriter dbWriter; - std::cout << "SourcetrailDB Poetry Indexer" << std::endl; + std::cout << "\nSourcetrailDB Poetry Indexer" << std::endl; std::cout << std::endl; std::cout << "SourcetrailDB version: " << dbWriter.getVersionString() << std::endl; std::cout << "Supported database version: " << dbWriter.getSupportedDatabaseVersion() << std::endl; std::cout << std::endl; - if (argc != 4) + if (argc < 3 || argc > 4) { - std::cout << "usage: poetry_indexer "; + std::cout << "usage: poetry_indexer "; } std::string dbPath = argv[1]; - int dbVersion = std::stoi(argv[2]); - std::string sourcePath = argv[3]; + std::string sourcePath = argv[2]; - if (dbVersion != dbWriter.getSupportedDatabaseVersion()) + int dbVersion = 0; + if (argc == 4) { - std::cout << "error: binary only supports database version: " << dbWriter.getSupportedDatabaseVersion() + char* end; + dbVersion = strtol(argv[3], &end, 10); + } + + if (dbVersion && dbVersion != dbWriter.getSupportedDatabaseVersion()) + { + std::cerr << "error: binary only supports database version: " << dbWriter.getSupportedDatabaseVersion() << ". Requested version: " << dbVersion << std::endl; return 1; } - if (!dbWriter.open(dbPath)) - { - std::cout << "error: " << dbWriter.getLastError() << std::endl; - return 1; - } - - if (!dbWriter.beginTransaction()) - { - std::cout << "error: " << dbWriter.getLastError() << std::endl; - return 1; - } - - int fileId = dbWriter.recordFile(sourcePath); - if (fileId == 0) - { - std::cout << "error: " << dbWriter.getLastError() << std::endl; - return 1; - } - std::ifstream fileStream(sourcePath.c_str()); if (!fileStream.good()) { - std::cout << "error: opening file failed: " << sourcePath << std::endl; + std::cerr << "error: opening file failed: " << sourcePath << std::endl; + return 1; + } + + std::cout << "Opening Database: " << dbPath << std::endl; + if (!dbWriter.open(dbPath)) + { + std::cerr << "error: " << dbWriter.getLastError() << std::endl; + return 1; + } + + std::cout << "Starting Indexing: " << sourcePath << std::endl; + + if (!dbWriter.beginTransaction()) + { + std::cerr << "error: " << dbWriter.getLastError() << std::endl; + return 1; + } + + int fileId = dbWriter.recordFile(sourcePath); + if (fileId == 0) + { + std::cerr << "error: " << dbWriter.getLastError() << std::endl; return 1; } @@ -191,15 +201,17 @@ int main(int argc, const char *argv[]) if (!dbWriter.commitTransaction()) { - std::cout << "error: " << dbWriter.getLastError() << std::endl; + std::cerr << "error: " << dbWriter.getLastError() << std::endl; return 1; } if (dbWriter.getLastError().size()) { - std::cout << "error: " << dbWriter.getLastError() << std::endl; + std::cerr << "error: " << dbWriter.getLastError() << std::endl; return 1; } + std::cout << "done!" << std::endl; + return 0; } diff --git a/examples/python_binding_example/CMakeLists.txt b/examples/python_api_example/CMakeLists.txt similarity index 92% rename from examples/python_binding_example/CMakeLists.txt rename to examples/python_api_example/CMakeLists.txt index 518e9db..4493176 100644 --- a/examples/python_binding_example/CMakeLists.txt +++ b/examples/python_api_example/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 2.6) -set(EXAMPLE_NAME "python_binding_example") +set(EXAMPLE_NAME "python_api_example") # --- Configure Sourcetrail Project File --- @@ -27,7 +27,7 @@ else() COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_BINDING_OUTPUT_DIR}/_sourcetraildb* ${CMAKE_CURRENT_BINARY_DIR} COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_BINDING_OUTPUT_DIR}/sourcetraildb.py ${CMAKE_CURRENT_BINARY_DIR} ) - + if (APPLE) add_custom_command( TARGET ${EXAMPLE_NAME} POST_BUILD @@ -37,7 +37,7 @@ else() endif() -set(PYTHON_BINDING_EXAMPLE_DATA_PATH "${CMAKE_CURRENT_SOURCE_DIR}/data") +set(PYTHON_API_EXAMPLE_DATA_PATH "${CMAKE_CURRENT_SOURCE_DIR}/data") configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE_NAME}.srctrlprj.in" diff --git a/examples/python_api_example/README.md b/examples/python_api_example/README.md new file mode 100644 index 0000000..2b65fdf --- /dev/null +++ b/examples/python_api_example/README.md @@ -0,0 +1,30 @@ + +## Python API Example + +### Requirements + +* CMake +* C++ Compiler +* Python +* SWIG + +### Running from Command Line + +* Run cmake with `BUILD_BINDINGS_PYTHON=ON` +* Build target `_sourcetraildb` +* Copy two files from the `bindings_python` build directory into `SourcetrailDB/examples/python_api_example/src`: + - `sourcetraildb.py` + - Windows: `_sourcetraildb.pyd` + - Linux: `_sourcetraildb.so` + - macOS: `_sourcetraildb.dylib` and rename to `_sourcetraildb.so` +* Run example from the directory `SourcetrailDB/examples/python_api_example/src` with: + +``` +$ python example.py --database-file-path=absolute/path/to/SourcetrailDB/examples/python_api_example/src/example.srctrldb --source-file-path=absolute/path/to/SourcetrailDB/examples/python_api_example/data/file.py +``` + +### Running with Sourcetrail + +* Run cmake with `BUILD_EXAMPLES=ON` and `BUILD_BINDINGS_PYTHON=ON` +* Build target `_sourcetraildb` and `python_api_example`. +* Open `python_api_exampl.srctrlprj` located in the build directory of `python_api_example` with Sourcetrail and index the project. diff --git a/examples/python_binding_example/data/file.py b/examples/python_api_example/data/file.py similarity index 100% rename from examples/python_binding_example/data/file.py rename to examples/python_api_example/data/file.py diff --git a/examples/python_binding_example/python_binding_example.srctrlprj.in b/examples/python_api_example/python_api_example.srctrlprj.in similarity index 72% rename from examples/python_binding_example/python_binding_example.srctrlprj.in rename to examples/python_api_example/python_api_example.srctrlprj.in index e93cc95..ca1c98d 100644 --- a/examples/python_binding_example/python_binding_example.srctrlprj.in +++ b/examples/python_api_example/python_api_example.srctrlprj.in @@ -2,13 +2,13 @@ - python3 example.py --database-file-path=$DB_PATH --source-file-path=$SOURCE_PATH + python example.py --database-file-path=$DB_PATH --source-file-path=$SOURCE_PATH Custom Command Source Group .py - @PYTHON_BINDING_EXAMPLE_DATA_PATH@/file.py + @PYTHON_API_EXAMPLE_DATA_PATH@/file.py enabled Custom Command Source Group diff --git a/examples/python_binding_example/src/example.py b/examples/python_api_example/src/example.py similarity index 55% rename from examples/python_binding_example/src/example.py rename to examples/python_api_example/src/example.py index 4dbafd9..dcea7a5 100644 --- a/examples/python_binding_example/src/example.py +++ b/examples/python_api_example/src/example.py @@ -3,23 +3,37 @@ import os import sourcetraildb as srctrl def main(): - parser = argparse.ArgumentParser(description="Index a Python source file and store the indexed data to a Sourcetrail database file.") - 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 = 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 all") + print("start indexing") srctrl.beginTransaction() fileId = srctrl.recordFile(sourceFilePath) @@ -27,45 +41,49 @@ def main(): if len(srctrl.getLastError()) > 0: print("ERROR: " + srctrl.getLastError()) + return 1 - symbolId = srctrl.recordSymbol('{ "name_delimiter": ".", "name_elements": [ { "prefix": "", "name": "MyType", "postfix": "" } ] }') + 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) - if len(srctrl.getLastError()) > 0: - print("ERROR: " + srctrl.getLastError()) - - memberId = srctrl.recordSymbol('{ "name_delimiter": ".", "name_elements": [ { "prefix": "", "name": "MyType", "postfix": "" }, { "prefix": "", "name": "my_member", "postfix": "" } ] }') + 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) - if len(srctrl.getLastError()) > 0: - print("ERROR: " + srctrl.getLastError()) - - methodId = srctrl.recordSymbol('{ "name_delimiter": ".", "name_elements": [ { "prefix": "", "name": "MyType", "postfix": "" }, { "prefix": "", "name": "my_method", "postfix": "" } ] }') + 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) - if len(srctrl.getLastError()) > 0: - print("ERROR: " + srctrl.getLastError()) - 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()) - - srctrl.commitTransaction() - print("end all") + return 1 if not srctrl.close(): print("ERROR: " + srctrl.getLastError()) + return 1 - if len(srctrl.getLastError()) > 0: - print("ERROR: " + srctrl.getLastError()) + print("done") + return 0 main()