Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Python plugin #549

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
df1f17d
Python plugin introduced
rmfcnb Oct 30, 2020
97f7ff3
Fix python service and parser to compile
rmfcnb Nov 1, 2020
fd9cc1c
Set ids to odb objects manually.
rmfcnb Nov 8, 2020
29a4858
Python service getDocumentation implementation.
rmfcnb Nov 8, 2020
1d4344c
Introduce visibility in PythonEntity
rmfcnb Nov 10, 2020
9d384f2
Get entity by qualified name.
rmfcnb Nov 11, 2020
41f1cc0
Python plugin webgui scripts
rmfcnb Nov 11, 2020
f4cc31c
Fix compilation
rmfcnb Nov 14, 2020
8b7a3bd
Fix Persistence, and PythonService usage during web server
rmfcnb Nov 24, 2020
ffdf54c
Set missing Usage id's.
rmfcnb Dec 1, 2020
a48dfb2
Fix python service and webgui
rmfcnb Dec 13, 2020
ff6f8eb
Remove debug console writes and unused transactions.
rmfcnb Dec 14, 2020
beae53a
Remove debug console writes from pythonservice.
rmfcnb Dec 14, 2020
6d3921f
Add python scripts
rmfcnb Dec 14, 2020
14e3292
Add CodeBites option to the Python plugin.
rmfcnb Jan 14, 2021
ea021a6
Build Boost with python3 binary when creating tarballs.
mcserep Jan 15, 2021
a011a5c
Adding Python to CMAKE_PREFIX_PATH
mcserep Jan 28, 2021
4a0faac
Build Python with dynamic library.
mcserep Jan 28, 2021
3e88b28
Load Boost::Python only for pythonparser.
mcserep Jan 29, 2021
976445c
Add correct libgit2 path to LD_LIBRARY_PATH for Ubuntu. Add postgreql…
mcserep Mar 11, 2021
980c359
Use FindPython3 instead of deprecated FindPythonLibs.
mcserep Mar 12, 2021
048e997
Added RTLD_GLOBAL to dlopen.
mcserep Apr 20, 2021
fe6ecf5
Set PYTHONHOME in cc-env to properly handle multiple Python installat…
mcserep Apr 20, 2021
f2b9c21
Handle Python 3.9 subscript (ast.Index and ast.ExtSlice are deprecated)
rmfcnb Apr 21, 2021
dfcf3e5
Fix libmagic download path in tarball building script
mcserep Apr 13, 2021
d771412
Handle symlinks in python parser.
rmfcnb May 16, 2021
0ee367f
Handle symlinks in python parser.
rmfcnb May 16, 2021
e4ce9b6
Tarball CI: updated outdated Boost download URL
mcserep May 9, 2021
84ca41e
Merge branch 'master' into pythonplugin
intjftw Jul 6, 2021
304427e
Use file and directory exceptions.
rmfcnb Jul 28, 2021
d09fd15
Use sets instead of vector.
rmfcnb Oct 31, 2021
f69520b
Merge branch 'master' into pythonplugin
mcserep Nov 3, 2021
8ce9002
Merge branch 'pythonplugin-symlink' of https://github.com/rmfcnb/Code…
mcserep Nov 3, 2021
a550b0e
Merge branch 'rmfcnb-pythonplugin-symlink' into pythonplugin
mcserep Nov 3, 2021
9b84d9b
Tarball CI: per branch caching of dependencies
mcserep Nov 21, 2021
67ae92b
Merge pull request #3 from Ericsson/pythonplugin
rmfcnb Nov 27, 2021
97d5b3b
Use config.json in main.py to parse projects.
rmfcnb Nov 27, 2021
811470c
Use qualified name in imports too.
rmfcnb Nov 27, 2021
e7b2db1
Handle qualified name in ImportData on c++ side.
rmfcnb Nov 27, 2021
d6cc131
Check PythonAstNode if already exists during persisting.
rmfcnb Nov 29, 2021
2856131
Update GitLab CI from upstream
mcserep Feb 23, 2022
07f72e6
Merge branch 'master' into pythonplugin-symlink
mcserep Feb 23, 2022
b70b911
Use logutil
rmfcnb Apr 23, 2022
5737715
Store AstNodes in set
rmfcnb May 15, 2022
27d4303
Extend dependencies with python
rmfcnb May 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitlab/build-codecompass.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ export LDFLAGS="-Wl,-rpath-link,$DEPS_INSTALL_RUNTIME_DIR/openssl-install/lib "\
"-Wl,-rpath-link,$DEPS_INSTALL_RUNTIME_DIR/postgresql-install/lib"

export LD_LIBRARY_PATH=$DEPS_INSTALL_RUNTIME_DIR/odb-install/lib\
:$DEPS_INSTALL_RUNTIME_DIR/python-install/lib\
:$LD_LIBRARY_PATH

export CMAKE_PREFIX_PATH=$DEPS_INSTALL_RUNTIME_DIR/libgit2-install\
:$DEPS_INSTALL_RUNTIME_DIR/odb-install\
:$DEPS_INSTALL_RUNTIME_DIR/thrift-install\
:$DEPS_INSTALL_RUNTIME_DIR/boost-install\
:$DEPS_INSTALL_RUNTIME_DIR/llvm-install\
:$DEPS_INSTALL_RUNTIME_DIR/python-install\
:$DEPS_INSTALL_RUNTIME_DIR/openldap-install

export GTEST_ROOT=$DEPS_INSTALL_BUILD_DIR/gtest-install
Expand Down
5 changes: 4 additions & 1 deletion .gitlab/build-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,14 @@ export LD_LIBRARY_PATH=$DEPS_INSTALL_RUNTIME_DIR/openssl-install/lib:$LD_LIBRARY
--quiet \
--prefix=$DEPS_INSTALL_RUNTIME_DIR/python-install \
--with-openssl=$DEPS_INSTALL_RUNTIME_DIR/openssl-install \
--enable-shared \
--enable-optimizations
make install --quiet --jobs $(nproc)
rm -f $PACKAGES_DIR/Python-3.9.0.tar.xz

export PATH=$DEPS_INSTALL_RUNTIME_DIR/python-install/bin:$PATH
export LD_LIBRARY_PATH=$DEPS_INSTALL_RUNTIME_DIR/python-install/lib:$LD_LIBRARY_PATH
# --enabled-shared: build libpython.so instead of libpython.a, must be on LD_LIBRARY_PATH

##############
# LLVM/Clang #
Expand Down Expand Up @@ -313,7 +316,7 @@ if [ ! -f $DEPS_INSTALL_RUNTIME_DIR/boost-install/lib/libboost_program_options.s

./bootstrap.sh \
--prefix=$DEPS_INSTALL_RUNTIME_DIR/boost-install \
--with-python=$DEPS_INSTALL_RUNTIME_DIR/python-install/bin/python
--with-python=$DEPS_INSTALL_RUNTIME_DIR/python-install/bin/python3
./b2 -j $(nproc) install

rm -f $PACKAGES_DIR/boost_1_74_0.tar.gz
Expand Down
4 changes: 4 additions & 0 deletions .gitlab/cc-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DEPS_INSTALL_DIR=$ROOT_DIR/deps-runtime
CC_INSTALL_DIR=$ROOT_DIR/cc-install

export LD_LIBRARY_PATH=$DEPS_INSTALL_DIR/libgit2-install/lib64\
:$DEPS_INSTALL_DIR/libgit2-install/lib\
:$DEPS_INSTALL_DIR/openssl-install/lib\
:$DEPS_INSTALL_DIR/graphviz-install/lib\
:$DEPS_INSTALL_DIR/libmagic-install/lib\
Expand All @@ -17,8 +18,10 @@ export LD_LIBRARY_PATH=$DEPS_INSTALL_DIR/libgit2-install/lib64\
:$DEPS_INSTALL_DIR/llvm-install/lib\
:$DEPS_INSTALL_DIR/libtool-install/lib\
:$DEPS_INSTALL_DIR/postgresql-install/lib\
:$DEPS_INSTALL_DIR/python-install/lib\
:$DEPS_INSTALL_DIR/openldap-install/lib\
:$LD_LIBRARY_PATH
# Note: libgit2-install/lib required on Ubuntu; libgit2-install/lib64 on SUSE

export PATH=$DEPS_INSTALL_DIR/jdk-install/bin\
:$DEPS_INSTALL_DIR/ctags-install/bin\
Expand All @@ -30,6 +33,7 @@ export PATH=$DEPS_INSTALL_DIR/jdk-install/bin\

export MAGIC=$DEPS_INSTALL_DIR/libmagic-install/share/misc/magic.mgc
export JAVA_HOME=$DEPS_INSTALL_DIR/jdk-install
export PYTHONHOME=$DEPS_INSTALL_DIR/python-install

if [ -f $ROOT_DIR/ccdb-tool/venv/bin/activate ]; then
source $ROOT_DIR/ccdb-tool/venv/bin/activate
Expand Down
6 changes: 3 additions & 3 deletions .gitlab/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ tarball suse-15:
extends: .tarball
image: opensuse/leap:15
cache:
key: "leap"
key: "leap-$CI_COMMIT_REF_SLUG"
variables:
GCC_VERSION: 9.3.0
ODB_VERSION: 2.5.0
Expand All @@ -85,7 +85,7 @@ tarball suse-42.1:
extends: .tarball
image: opensuse/archive:42.1
cache:
key: "malachite"
key: "malachite-$CI_COMMIT_REF_SLUG"
variables:
GCC_VERSION: 5.5.0
ODB_VERSION: 2.4.0
Expand All @@ -111,7 +111,7 @@ tarball ubuntu-16.04:
extends: .tarball
image: ubuntu:16.04
cache:
key: "xenial"
key: "xenial-$CI_COMMIT_REF_SLUG"
variables:
GCC_VERSION: 5.5.0
ODB_VERSION: 2.4.0
Expand Down
3 changes: 2 additions & 1 deletion doc/deps.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ be installed from the official repository of the given Linux distribution.
- **`libgtest-dev`**: For testing CodeCompass.
***See [Known issues](#known-issues)!***
- **`libldap2-dev`**: For LDAP authentication.
- **`python3.8`**: For python parsing.

## Quick guide

Expand All @@ -52,7 +53,7 @@ known issues.
sudo apt install git cmake make g++ gcc-7-plugin-dev libboost-all-dev \
llvm-10-dev clang-10 libclang-10-dev \
default-jdk libssl1.0-dev libgraphviz-dev libmagic-dev libgit2-dev ctags doxygen \
libldap2-dev libgtest-dev npm
libldap2-dev libgtest-dev npm python3.8
```

#### Ubuntu 20.04 ("Focal Fossa") LTS
Expand Down
1 change: 1 addition & 0 deletions model/include/model/buildaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct BuildAction
{
Compile,
Link,
Interpret,
Other
};

Expand Down
1 change: 1 addition & 0 deletions plugins/cpp/webgui/js/cppInfoTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ function (model, viewHandler, util) {
}

var cppInfoTree = {
id: 'cpp-info-tree',
render : function (elementInfo) {
var ret = [];

Expand Down
5 changes: 5 additions & 0 deletions plugins/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_subdirectory(parser)
add_subdirectory(model)
add_subdirectory(service)

install_webplugin(webgui)
17 changes: 17 additions & 0 deletions plugins/python/model/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
set(ODB_SOURCES
include/model/pythonastnode.h
include/model/pythonclass.h
include/model/pythondocumentation.h
include/model/pythonentity.h
include/model/pythonfunction.h
include/model/pythonimport.h
include/model/pythoninheritance.h
include/model/pythontype.h
include/model/pythonvariable.h)

generate_odb_files("${ODB_SOURCES}")

add_odb_library(pythonmodel ${ODB_CXX_SOURCES})
target_link_libraries(pythonmodel model)

install_sql()
164 changes: 164 additions & 0 deletions plugins/python/model/include/model/pythonastnode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#ifndef CC_MODEL_PYTHONASTNODE_H
#define CC_MODEL_PYTHONASTNODE_H

#include <string>

#include <odb/nullable.hxx>
#include <odb/core.hxx>
#include <odb/lazy-ptr.hxx>

#include <model/fileloc.h>

#include <util/hash.h>

namespace cc
{
namespace model
{

typedef std::uint64_t PythonAstNodeId;

#pragma db object
struct PythonAstNode
{
enum class SymbolType
{
Variable,
Function,
Class,
Module,
Other
};

enum class AstType
{
Declaration,
Usage,
Other
};

virtual ~PythonAstNode() {}

#pragma db id
PythonAstNodeId id = 0;

std::string astValue;

std::string qualifiedName;

#pragma db null
FileLoc location;

SymbolType symbolType = SymbolType::Other;

AstType astType = AstType::Other;

std::string toString() const;

bool operator< (const PythonAstNode& other) const { return id < other.id; }
bool operator==(const PythonAstNode& other) const { return id == other.id; }
};

typedef std::shared_ptr<PythonAstNode> PythonAstNodePtr;

inline std::string symbolTypeToString(PythonAstNode::SymbolType type_)
{
switch (type_)
{
case PythonAstNode::SymbolType::Variable: return "Variable";
case PythonAstNode::SymbolType::Function: return "Function";
case PythonAstNode::SymbolType::Class: return "Class";
case PythonAstNode::SymbolType::Module: return "Module";
case PythonAstNode::SymbolType::Other: return "Other";
}

return std::string();
}

inline std::string astTypeToString(PythonAstNode::AstType type)
{
switch (type) {
case PythonAstNode::AstType::Usage: return "Usage";
case PythonAstNode::AstType::Declaration: return "Declaration";
case PythonAstNode::AstType::Other: return "Other";
}

return std::string();
}

inline std::string PythonAstNode::toString() const
{
return std::string("PythonAstNode")
.append("\nid = ").append(std::to_string(id))
.append("\nastValue = ").append(astValue)
.append("\nqualifiedName = ").append(qualifiedName)
.append("\nlocation = ").append(location.file->path).append(" (")
.append(std::to_string(
static_cast<signed>(location.range.start.line))).append(":")
.append(std::to_string(
static_cast<signed>(location.range.start.column))).append(" - ")
.append(std::to_string(
static_cast<signed>(location.range.end.line))).append(":")
.append(std::to_string(
static_cast<signed>(location.range.end.column))).append(")")
.append("\nsymbolType = ").append(symbolTypeToString(symbolType))
.append("\nastType = ").append(astTypeToString(astType));
}

inline std::uint64_t createIdentifier(const PythonAstNode& astNode_)
{
using SymbolTypeInt
= std::underlying_type<model::PythonAstNode::SymbolType>::type;
using AstTypeInt
= std::underlying_type<model::PythonAstNode::AstType>::type;

std::string res;

res
.append(astNode_.astValue).append(":")
.append(astNode_.qualifiedName).append(":")
.append(std::to_string(static_cast<SymbolTypeInt>(astNode_.symbolType))).append(":")
.append(std::to_string(static_cast<AstTypeInt>(astNode_.astType))).append(":");

if (astNode_.location.file){
res
.append(std::to_string(astNode_.location.file->id)).append(":")
.append(std::to_string(astNode_.location.range.start.line)).append(":")
.append(std::to_string(astNode_.location.range.start.column)).append(":")
.append(std::to_string(astNode_.location.range.end.line)).append(":")
.append(std::to_string(astNode_.location.range.end.column)).append(":");
} else {
res.append("null");
}

return util::fnvHash(res);
}

inline std::uint64_t createAstNodeInfoEntityHash(const PythonAstNode& astNode_)
{
return util::fnvHash(astNode_.qualifiedName);
}

#pragma db view \
object(PythonAstNode) object(File = LocFile : PythonAstNode::location.file) \
query ((?) + "GROUP BY" + LocFile::id + "ORDER BY" + LocFile::id)
struct PythonAstCountGroupByFiles
{
#pragma db column(LocFile::id)
FileId file;

#pragma db column("count(" + PythonAstNode::id + ")")
std::size_t count;
};

#pragma db view object(PythonAstNode)
struct PythonAstCount
{
#pragma db column("count(" + PythonAstNode::id + ")")
std::size_t count;
};

}
}

#endif
77 changes: 77 additions & 0 deletions plugins/python/model/include/model/pythonclass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef CC_MODEL_PYTHONCLASS_H
#define CC_MODEL_PYTHONCLASS_H

#include "pythonentity.h"

namespace cc
{
namespace model
{

#pragma db object
struct PythonClass : PythonEntity
{
std::string toString() const
{
return std::string("PythonClass")
.append("\nid = ").append(std::to_string(id))
.append("\nqualifiedName = ").append(qualifiedName);
}
};

typedef std::shared_ptr<PythonClass> PythonClassPtr;

#pragma db object
struct PythonClassMember
{
enum Kind
{
Attribute,
Method,
Class
};

#pragma db id auto
int id;

#pragma db unique
PythonAstNodeId astNodeId;

PythonEntityId memberId;
PythonEntityId classId;

Kind kind;
bool staticMember = false;

std::string toString() const
{
return std::string("PythonClassMember")
.append("\nid = ").append(std::to_string(id))
.append("\nmemberId = ").append(std::to_string(memberId))
.append("\nclassId = ").append(std::to_string(classId))
.append("\nstaticMember = ").append(std::to_string(staticMember))
.append("\nkind = ").append(kind == Attribute ? "Attribute" :
kind == Method ? "Method" : "Class");
}
};

typedef std::shared_ptr<PythonClassMember> PythonClassMemberPtr;

#pragma db view object(PythonClass)
struct PythonClassCount
{
#pragma db column("count(" + PythonClass::id + ")")
std::size_t count;
};

#pragma db view object(PythonClassMember)
struct PythonClassMemberCount
{
#pragma db column("count(" + PythonClassMember::id + ")")
std::size_t count;
};

}
}

#endif
Loading