added README to examples and improved database version check when used from Sourcetrail
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
cmake_minimum_required (VERSION 2.6)
|
||||
|
||||
set(EXAMPLE_NAME "python_api_example")
|
||||
|
||||
# --- Configure Sourcetrail Project File ---
|
||||
|
||||
add_custom_target(
|
||||
${EXAMPLE_NAME} ALL
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/src/example.py ${CMAKE_CURRENT_BINARY_DIR}/example.py
|
||||
)
|
||||
|
||||
if (MSVC)
|
||||
STRING(REGEX REPLACE "/" "\\\\" BACKSLASHED_PYTHON_BINDING_OUTPUT_DIR ${PYTHON_BINDING_OUTPUT_DIR})
|
||||
STRING(REGEX REPLACE "/" "\\\\" BACKSLASHED_CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
add_custom_command(
|
||||
TARGET ${EXAMPLE_NAME} POST_BUILD
|
||||
COMMAND xcopy \"${BACKSLASHED_PYTHON_BINDING_OUTPUT_DIR}\\$(Configuration)\\_sourcetraildb*\" \"${BACKSLASHED_CMAKE_CURRENT_BINARY_DIR}\" /Y
|
||||
)
|
||||
add_custom_command(
|
||||
TARGET ${EXAMPLE_NAME} POST_BUILD
|
||||
COMMAND xcopy \"${BACKSLASHED_PYTHON_BINDING_OUTPUT_DIR}\\sourcetraildb.py\" \"${BACKSLASHED_CMAKE_CURRENT_BINARY_DIR}\" /Y
|
||||
)
|
||||
else()
|
||||
add_custom_command(
|
||||
TARGET ${EXAMPLE_NAME} POST_BUILD
|
||||
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
|
||||
COMMAND ${CMAKE_COMMAND} -E rename ${CMAKE_CURRENT_BINARY_DIR}/_sourcetraildb.dylib ${CMAKE_CURRENT_BINARY_DIR}/_sourcetraildb.so
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
|
||||
set(PYTHON_API_EXAMPLE_DATA_PATH "${CMAKE_CURRENT_SOURCE_DIR}/data")
|
||||
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE_NAME}.srctrlprj.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_NAME}.srctrlprj"
|
||||
)
|
||||
@@ -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.
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
class MyType:
|
||||
|
||||
my_member = true
|
||||
|
||||
def my_method():
|
||||
return my_member
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<config>
|
||||
<source_groups>
|
||||
<source_group_78efd997-84bf-4db6-9736-f5ddd702053c>
|
||||
<custom_command>python example.py --database-file-path=$DB_PATH --source-file-path=$SOURCE_PATH</custom_command>
|
||||
<name>Custom Command Source Group</name>
|
||||
<source_extensions>
|
||||
<source_extension>.py</source_extension>
|
||||
</source_extensions>
|
||||
<source_paths>
|
||||
<source_path>@PYTHON_API_EXAMPLE_DATA_PATH@/file.py</source_path>
|
||||
</source_paths>
|
||||
<status>enabled</status>
|
||||
<type>Custom Command Source Group</type>
|
||||
</source_group_78efd997-84bf-4db6-9736-f5ddd702053c>
|
||||
</source_groups>
|
||||
<version>7</version>
|
||||
</config>
|
||||
@@ -0,0 +1,89 @@
|
||||
import argparse
|
||||
import os
|
||||
import sourcetraildb as srctrl
|
||||
|
||||
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
|
||||
main()
|
||||
Reference in New Issue
Block a user