-
Notifications
You must be signed in to change notification settings - Fork 2
Feat/python dependency integration #203
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
Open
christian-spahn-mw
wants to merge
3
commits into
main
Choose a base branch
from
feat/python-dependency-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
253 changes: 105 additions & 148 deletions
253
...kotlin/de/maibornwolff/dependacharta/pipeline/analysis/analyzers/python/PythonAnalyzer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,193 +1,150 @@ | ||
| package de.maibornwolff.dependacharta.pipeline.analysis.analyzers.python | ||
|
|
||
| import de.maibornwolff.dependacharta.pipeline.analysis.analyzers.LanguageAnalyzer | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.analyzers.common.utils.nodeAsString | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.analyzers.common.utils.withoutFileSuffix | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.analyzers.python.queries.PythonDefinitionsQuery | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.analyzers.python.queries.PythonImportQuery | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.analyzers.python.queries.PythonTypeAttributeQuery | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.analyzers.python.queries.PythonTypeIdentifierQuery | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.model.* | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.analyzers.toNodeType | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.model.Dependency | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.model.FileInfo | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.model.FileReport | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.model.Node | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.model.NodeType | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.model.Path | ||
| import de.maibornwolff.dependacharta.pipeline.analysis.model.Type | ||
| import de.maibornwolff.dependacharta.pipeline.shared.SupportedLanguage | ||
| import org.treesitter.TSNode | ||
| import org.treesitter.TSParser | ||
| import org.treesitter.TreeSitterPython | ||
| import de.maibornwolff.treesitter.excavationsite.api.Declaration | ||
| import de.maibornwolff.treesitter.excavationsite.api.DeclarationType | ||
| import de.maibornwolff.treesitter.excavationsite.api.ImportDeclaration | ||
| import de.maibornwolff.treesitter.excavationsite.api.ImportKind | ||
| import de.maibornwolff.treesitter.excavationsite.api.Language | ||
| import de.maibornwolff.treesitter.excavationsite.api.TreeSitterDependencies | ||
| import de.maibornwolff.treesitter.excavationsite.api.UsedType | ||
|
|
||
| class PythonAnalyzer( | ||
| private val fileInfo: FileInfo | ||
| ) : LanguageAnalyzer { | ||
| private val python = TreeSitterPython() | ||
| private val definitionsQuery = PythonDefinitionsQuery(python) | ||
| private val importQuery = PythonImportQuery(python) | ||
| private val identifierQuery = PythonTypeIdentifierQuery(python) | ||
| private val attributeQuery = PythonTypeAttributeQuery(python) | ||
| private companion object { | ||
| const val INIT = "__init__" | ||
| const val PY_SUFFIX = "py" | ||
| const val SEPARATOR = "." | ||
| } | ||
|
|
||
| override fun analyze(): FileReport { | ||
| val rootNode = parseCode(fileInfo.content) | ||
| val filePathWithSlashes = fileInfo.physicalPathAsPath() | ||
| val pathWithoutFileEnding = filePathWithSlashes.withoutFileSuffix("py") | ||
| val modulePath = pathWithoutFileEnding.parts | ||
|
|
||
| val definitions = definitionsQuery.execute(rootNode) | ||
| val result = TreeSitterDependencies.analyze(fileInfo.content, Language.PYTHON) | ||
| val modulePath = fileInfo.physicalPathAsPath().withoutFileSuffix(PY_SUFFIX).parts | ||
|
|
||
| val importsFrom = importQuery.executeImportsFrom(rootNode, fileInfo.content, modulePath) | ||
| val wildcardImportsFrom = importQuery.executeWildcardImportsFrom(rootNode, fileInfo.content, modulePath) | ||
| val aliasedImportsFrom = importQuery.executeAliasedImportsFrom(rootNode, fileInfo.content, modulePath) | ||
| val resolvedImports = result.imports.map { it.resolveRelative(modulePath) } | ||
| val regularFromImports = resolvedImports.filter { it.kind == ImportKind.IMPORT_FROM && !it.isAliased && !it.isWildcard } | ||
| val wildcardFromImports = resolvedImports.filter { it.kind == ImportKind.IMPORT_FROM && it.isWildcard } | ||
| val aliasedFromImports = resolvedImports.filter { it.kind == ImportKind.IMPORT_FROM && it.isAliased } | ||
| val standardImports = resolvedImports.filter { it.kind == ImportKind.STANDARD } | ||
|
|
||
| val imports = importQuery.executeImports(rootNode, fileInfo.content) | ||
| val aliasedImports = importQuery.executeAliasedImports(rootNode, fileInfo.content, modulePath) | ||
| val regularFromTwins = regularFromImports.flatMap { it.twinPair() } | ||
| val wildcardFromTwins = wildcardFromImports.flatMap { it.twinPair() } | ||
| val basePreloadedDeps = regularFromTwins + wildcardFromTwins | ||
|
|
||
| val nodes = mutableListOf<Node>() | ||
|
|
||
| if (modulePath.last() == "__init__") { | ||
| nodes.addAll( | ||
| importsFrom.filter { dependency -> "__init__" !in dependency.path.parts }.map { dependency -> | ||
| Node( | ||
| pathWithName = Path(modulePath + dependency.path.parts.last()), | ||
| physicalPath = fileInfo.physicalPath, | ||
| language = SupportedLanguage.PYTHON, | ||
| nodeType = NodeType.UNKNOWN, | ||
| dependencies = importsFrom.toSet(), | ||
| usedTypes = setOf(Type.simple(dependency.path.parts.last())) | ||
| ) | ||
| } | ||
| ) | ||
| if (modulePath.lastOrNull() == INIT) { | ||
| nodes.addAll(initFileReexportNodes(regularFromImports, regularFromTwins.toSet(), modulePath)) | ||
|
christian-spahn-mw marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (definitions.isNotEmpty()) { | ||
| nodes.addAll( | ||
| definitions.map { | ||
| extractNodeFromDefinition( | ||
| modulePath, | ||
| it, | ||
| importsFrom + wildcardImportsFrom, | ||
| aliasedImportsFrom, | ||
| imports, | ||
| aliasedImports, | ||
| ) | ||
| } | ||
| ) | ||
| result.declarations.forEach { declaration -> | ||
| nodes.add(buildDeclarationNode(declaration, modulePath, basePreloadedDeps, aliasedFromImports, standardImports)) | ||
| } | ||
|
|
||
| return FileReport(nodes) | ||
| } | ||
|
|
||
| private fun parseCode(pythonCode: String): TSNode { | ||
| val parser = TSParser() | ||
| parser.language = python | ||
| val tree = parser.parseString(null, pythonCode) | ||
| return tree.rootNode | ||
| } | ||
| private fun initFileReexportNodes( | ||
| regularFromImports: List<ImportDeclaration>, | ||
| regularFromTwins: Set<Dependency>, | ||
| modulePath: List<String> | ||
| ): List<Node> = | ||
| regularFromImports.map { import -> | ||
| val name = import.path.last() | ||
| Node( | ||
| pathWithName = Path(modulePath + name), | ||
| physicalPath = fileInfo.physicalPath, | ||
| language = SupportedLanguage.PYTHON, | ||
| nodeType = NodeType.UNKNOWN, | ||
| dependencies = regularFromTwins, | ||
| usedTypes = setOf(Type.simple(name)) | ||
| ) | ||
| } | ||
|
|
||
| private fun extractNodeFromDefinition( | ||
| private fun buildDeclarationNode( | ||
| declaration: Declaration, | ||
| modulePath: List<String>, | ||
| definition: TSNode, | ||
| importFromDependencies: List<Dependency>, | ||
| aliasedImportsFrom: Map<String, String>, | ||
| imports: List<String>, | ||
| aliasedImports: Map<String, String>, | ||
| basePreloadedDeps: List<Dependency>, | ||
| aliasedFromImports: List<ImportDeclaration>, | ||
| standardImports: List<ImportDeclaration> | ||
| ): Node { | ||
| val mutableImportFromDependencies = importFromDependencies.toMutableList() | ||
| val nodeBody = nodeAsString(definition, fileInfo.content) | ||
| val definitionNode = parseCode(nodeBody).getChild(0) | ||
| val pathWithName = Path(modulePath + declaration.name) | ||
| val nodeType = declaration.type.toNodeType() | ||
|
|
||
| if (definition.type == "identifier") { | ||
| if (declaration.type == DeclarationType.VARIABLE) { | ||
| return Node( | ||
| pathWithName = Path(modulePath + nodeBody), | ||
| pathWithName = pathWithName, | ||
| physicalPath = fileInfo.physicalPath, | ||
| language = SupportedLanguage.PYTHON, | ||
| nodeType = NodeType.VARIABLE, | ||
| dependencies = setOf(), | ||
| usedTypes = setOf() | ||
| nodeType = nodeType, | ||
| dependencies = emptySet(), | ||
| usedTypes = emptySet() | ||
| ) | ||
| } | ||
|
|
||
| var classOrFunctionDefinitionNode = definitionNode | ||
| if (definition.type == "decorated_definition") { | ||
| classOrFunctionDefinitionNode = definitionNode.getChildByFieldName("definition") | ||
| } | ||
| val (identifierStream, attributeStream) = declaration.usedTypes.partition { it.namespacePrefix.isEmpty() } | ||
| val identifierNames = identifierStream.map { it.name }.toSet() | ||
|
|
||
| val definitionName = nodeAsString(classOrFunctionDefinitionNode.getChildByFieldName("name"), nodeBody) | ||
|
|
||
| val importFromTypes = buildImportFromTypes( | ||
| identifierQuery.execute(definitionNode, nodeBody), | ||
| aliasedImportsFrom, | ||
| mutableImportFromDependencies | ||
| ) | ||
|
|
||
| val importDependencies = buildImportDependencies(imports, aliasedImports, definitionNode, nodeBody) | ||
| val deps = basePreloadedDeps.toMutableSet() | ||
| aliasedFromImports.forEach { import -> | ||
| if (import.path.last() in identifierNames) { | ||
| deps.addAll(import.twinPair()) | ||
| } | ||
| } | ||
| attributeStream.forEach { ut -> | ||
| deps.addAll(matchStandardImportAttribute(ut, standardImports)) | ||
| } | ||
|
|
||
| val usedTypes = identifierStream.map { Type.simple(it.name) }.toSet() | ||
| return Node( | ||
| pathWithName = Path(modulePath + definitionName), | ||
| pathWithName = pathWithName, | ||
| physicalPath = fileInfo.physicalPath, | ||
| language = SupportedLanguage.PYTHON, | ||
| nodeType = nodeType(classOrFunctionDefinitionNode), | ||
| dependencies = mutableImportFromDependencies.toSet() + importDependencies, | ||
| usedTypes = importFromTypes | ||
| nodeType = nodeType, | ||
| dependencies = deps, | ||
| usedTypes = usedTypes | ||
| ) | ||
|
christian-spahn-mw marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private fun nodeType(declaration: TSNode) = | ||
| when (declaration.type) { | ||
| "class_definition" -> NodeType.CLASS | ||
| "function_definition" -> NodeType.FUNCTION | ||
| else -> NodeType.UNKNOWN | ||
| } | ||
|
|
||
| private fun buildImportFromTypes( | ||
| types: List<String>, | ||
| aliasedImportsFrom: Map<String, String>, | ||
| mutableDependencies: MutableList<Dependency> | ||
| ): Set<Type> = | ||
| types | ||
| .map { | ||
| val aliasedType = checkAliasImportFrom(it, aliasedImportsFrom, mutableDependencies) | ||
| Type.simple(aliasedType) | ||
| }.toSet() | ||
|
|
||
| private fun checkAliasImportFrom( | ||
| type: String, | ||
| aliasedImportsFrom: Map<String, String>, | ||
| mutableDependencies: MutableList<Dependency> | ||
| ): String { | ||
| if (type in aliasedImportsFrom) { | ||
| val aliasedImport = aliasedImportsFrom[type]!!.split(".") | ||
| val aliasedType = aliasedImport.last() | ||
| mutableDependencies.add(Dependency(Path(aliasedImport))) | ||
| mutableDependencies.add(Dependency(Path(aliasedImport.dropLast(1) + listOf("__init__", aliasedType)))) | ||
| return aliasedType | ||
| private fun matchStandardImportAttribute( | ||
| usedType: UsedType, | ||
| standardImports: List<ImportDeclaration> | ||
| ): List<Dependency> { | ||
| val joinedPrefix = usedType.namespacePrefix.joinToString(SEPARATOR) | ||
| return standardImports.flatMap { import -> | ||
| if (import.path.joinToString(SEPARATOR) == joinedPrefix) { | ||
| listOf( | ||
| Dependency(Path(import.path + usedType.name)), | ||
| Dependency(Path(import.path + INIT + usedType.name)) | ||
| ) | ||
| } else { | ||
| emptyList() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return type | ||
| private fun ImportDeclaration.resolveRelative(modulePath: List<String>): ImportDeclaration { | ||
| val first = path.firstOrNull() ?: return this | ||
| if (first.isEmpty() || !first.all { it == '.' }) return this | ||
| val resolvedPath = modulePath.dropLast(first.length) + path.drop(1) | ||
| return copy(path = resolvedPath) | ||
| } | ||
|
|
||
| private fun buildImportDependencies( | ||
| imports: List<String>, | ||
| aliasedImports: Map<String, String>, | ||
| definitionNode: TSNode, | ||
| nodeBody: String | ||
| ): Set<Dependency> { | ||
| val dependencies = mutableSetOf<Dependency>() | ||
| if (imports.isNotEmpty() || aliasedImports.isNotEmpty()) { | ||
| val attributes = attributeQuery.execute(definitionNode, nodeBody) | ||
| attributes.forEach { attribute -> | ||
| val attributeList = attribute.split(".").toMutableList() | ||
| if (attributeList.size >= 2) { | ||
| val attributeType = attributeList.removeLast() | ||
| val attributeIdentifier = attributeList.joinToString(".") | ||
| if (attributeIdentifier in imports) { | ||
| dependencies.add(Dependency(Path(attributeList + listOf(attributeType)), false)) | ||
| dependencies.add(Dependency(Path(attributeList + listOf("__init__", attributeType)), false)) | ||
| } else if (attributeIdentifier in aliasedImports) { | ||
| val aliasedPath = Path(aliasedImports[attributeIdentifier]!!.split(".") + listOf(attributeType)) | ||
| dependencies.add(Dependency(aliasedPath, false)) | ||
| val aliasedInitPath = | ||
| Path(aliasedImports[attributeIdentifier]!!.split(".") + listOf("__init__", attributeType)) | ||
| dependencies.add(Dependency(aliasedInitPath, false)) | ||
| } | ||
| } | ||
| } | ||
| private fun ImportDeclaration.twinPair(): List<Dependency> { | ||
| val canonical = Dependency(Path(path), isWildcard) | ||
| val initPath = if (isWildcard) { | ||
| path + INIT | ||
| } else { | ||
| path.dropLast(1) + INIT + path.last() | ||
| } | ||
| return dependencies | ||
| return listOf(canonical, Dependency(Path(initPath), isWildcard)) | ||
| } | ||
| } | ||
6 changes: 0 additions & 6 deletions
6
...in/kotlin/de/maibornwolff/dependacharta/pipeline/analysis/analyzers/python/PythonUtils.kt
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
...nwolff/dependacharta/pipeline/analysis/analyzers/python/queries/PythonDefinitionsQuery.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.