Skip to content

Commit

Permalink
Update Maven plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Simon committed Sep 26, 2024
1 parent 0ed3bfa commit b6b1150
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 130 deletions.
2 changes: 1 addition & 1 deletion src/cfgnet/network/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Node:
"""

def __init__(self, name: str, parent: Node = None):
def __init__(self, name: str, parent: Optional[Node] = None):
self.name: str = name
self.parent: Optional[Node] = parent
self.children: List[Any] = []
Expand Down
138 changes: 56 additions & 82 deletions src/cfgnet/plugins/concept/maven_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,98 +98,71 @@ def is_responsible(self, abs_file_path: str) -> bool:
return False

def parse_tree(self, subtree_root: _Element, parent_node: Node):
name = self._get_option_name(subtree_root)
name = subtree_root.tag

if name:
config_type = self.get_config_type(name)
option = OptionNode(name, subtree_root.sourceline, config_type)
parent_node.add_child(option)

self._add_attribs(subtree_root, option)

text = subtree_root.text
if text:
text = text.strip()
if text:
name = self._get_value_name(
text=text, config_type=config_type, parent=option
)
if name in ["dependencies", "plugins"]:
config_type = self.get_config_type(name)
option = OptionNode(name, subtree_root.sourceline, config_type)
parent_node.add_child(option)

value_node = ValueNode(name=name)
option.add_child(value_node)
self._parse_artifacts(subtree_root, option)

for child in subtree_root:
if child.tag is not ET.Comment:
self.parse_tree(child, option)
else:
config_type = self.get_config_type(name)
option = OptionNode(name, subtree_root.sourceline, config_type)
parent_node.add_child(option)

value_name = subtree_root.text.strip()

if value_name:
value_node = ValueNode(name=value_name)
option.add_child(value_node)
else:
for child in subtree_root:
if child.tag is not ET.Comment:
self.parse_tree(child, option)

# remove option nodes without children
if not option.children:
parent_node.children.remove(option)

@staticmethod
def _add_attribs(subtree_root: _Element, current_node: OptionNode):
current_attribs = subtree_root.attrib
for key in current_attribs:
config_type = MavenPlugin.get_config_type(key)
option = OptionNode(key, subtree_root.sourceline, config_type)
current_node.add_child(option)
value = current_attribs[key]

name = MavenPlugin._get_value_name(
text=value, config_type=config_type, parent=option
)

value_node = ValueNode(name=name)
option.add_child(value_node)

# pylint: disable=too-many-return-statements
@staticmethod
def _get_option_name(current_item: _Element) -> str:
"""
Construct a name for an option node to avoid ambiguous option nodes.
:param current_item: lxml etree element that should be inserted as an option node
:return: constructed name
"""
id_element = current_item.find("id")
if id_element is not None:
if id_element.text is not None:
return current_item.tag + "_" + id_element.text

artifact_id = current_item.find("artifactId")
if artifact_id is not None:
if artifact_id.text is not None:
return current_item.tag + "_" + artifact_id.text
def _parse_artifacts(self, subtree: _Element, current_node: OptionNode):
"""Parse Maven artifacts."""
for child in subtree:
if child.tag in ["dependency", "plugin"]:
qualified_name = self._get_fully_qualified_name(child)
config_type = self.get_config_type(qualified_name)
option_node = OptionNode(
name=qualified_name,
location=child.sourceline,
config_type=config_type,
)
current_node.add_child(option_node)

if current_item.tag in TAGS_CONTAINING_LISTS:
if current_item.text is not None:
return current_item.tag + "_" + current_item.text
for element in child:
self.parse_tree(
subtree_root=element, parent_node=option_node
)

return current_item.tag

@staticmethod
def _get_value_name(
text: str, config_type: ConfigType, parent: OptionNode
) -> str:
"""Create name for value name."""
if any(x in parent.id for x in ["dependencies", "plugins"]):
if parent.parent:
option_parts = parent.parent.name.split("_")
if len(option_parts) == 2:
option_name = option_parts[-1]
else:
option_name = parent.name
name = (
f"{option_name}:{text}"
if config_type == ConfigType.VERSION_NUMBER
else text
)
else:
name = (
f"{parent.name}:{text}"
if config_type == ConfigType.VERSION_NUMBER
else text
)
return name
# pylint: disable=invalid-name
def _get_fully_qualified_name(self, subtree: _Element) -> str:
"""Get fully qualified name for Maven artifacts."""
artifactID = None
groupID = None
version = None
for child in subtree:
if child.tag == "artifactId":
artifactID = child.text
if child.tag == "groupId":
groupID = child.text
if child.tag == "version":
version = child.text

if version:
return f"{groupID}:{artifactID}:{version}"

return f"{groupID}:{artifactID}"

def _add_executable_name(self, artifact: ArtifactNode) -> None:
try:
Expand Down Expand Up @@ -306,6 +279,7 @@ def _get_packaging_for_executable_name(
return "jar", None

@staticmethod
# pylint: disable=too-many-return-statements
def get_config_type(option_name: str) -> ConfigType:
"""
Get config type based on the option name.
Expand Down
Empty file.
16 changes: 4 additions & 12 deletions tests/cfgnet/network/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,20 @@ def test_enable_internal_links(get_config):
config.enable_internal_links = True
network = Network.init_network(cfg=config)

for node in network.get_nodes(node_type=ValueNode):
if "Dockerfile" in node.id:
print("Node: ", node, node.config_type)

for link in network.links:
print("=======================")
print("Link: ", link)
print(f"{link.node_a.config_type}<->{link.node_b.config_type}")

expected_links = {
"app.jar",
"target/example-app-1.0.jar",
"pom.xml",
"builder",
"version:5.9",
"monitoring",
"5.9"
}

link_targets = {
str(link).rsplit("::::", maxsplit=1)[-1] for link in network.links
}

assert len(network.links) == 5
assert len(network.links) == 6
assert expected_links == link_targets


Expand Down
6 changes: 1 addition & 5 deletions tests/cfgnet/plugins/concept/test_docker_compose_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,7 @@ def test_config_types(get_plugin):

artifact = docker_compose_plugin.parse_file(file, "docker-compose.yml")
nodes = artifact.get_nodes()

for node in nodes:
print(node)



port_node = next(
filter(
lambda x: x.id
Expand Down
50 changes: 24 additions & 26 deletions tests/cfgnet/plugins/concept/test_maven_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,40 @@ def test_parse_maven_file(get_plugin):
print(id)

assert artifact is not None
assert len(value_nodes) == 33

assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency_hibernate", "version", "hibernate:3.2.5.ga") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency_hibernate", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency_hibernate", "artifactId", "hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency_ejb3-persistence", "version", "ejb3-persistence:1.0.1.GA") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency_ejb3-persistence", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency_ejb3-persistence", "artifactId", "ejb3-persistence") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency_hibernate-core", "version", "hibernate-core:3.6.3.Final") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency_hibernate-core", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency_hibernate-core", "artifactId", "hibernate-core") in ids
assert len(value_nodes) == 31

assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:hibernate:3.2.5.ga", "version", "3.2.5.ga") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:hibernate:3.2.5.ga", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:hibernate:3.2.5.ga", "artifactId", "hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:ejb3-persistence:1.0.1.GA", "version", "1.0.1.GA") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:ejb3-persistence:1.0.1.GA", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:ejb3-persistence:1.0.1.GA", "artifactId", "ejb3-persistence") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:hibernate-core", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:hibernate-core", "artifactId", "hibernate-core") in ids
assert make_id("pom.xml", "project", "modules", "module", "config") in ids
assert make_id("pom.xml", "project", "modelVersion", "modelVersion:4.0.0") in ids
assert make_id("pom.xml", "project", "modelVersion", "4.0.0") in ids
assert make_id("pom.xml", "project", "packaging", "jar") in ids
assert make_id("pom.xml", "file", "pom.xml") in ids
assert make_id("pom.xml", "project", "groupId", "com.example.apps") in ids
assert make_id("pom.xml", "project", "artifactId", "my-cool-app") in ids
assert make_id("pom.xml", "project", "version", "version:42") in ids
assert make_id("pom.xml", "project", "version", "42") in ids
assert make_id("pom.xml", "project", "packaging", "jar") in ids
assert make_id("pom.xml", "ExecutableName", "target/my-cool-app-42.jar") in ids
assert make_id("pom.xml", "ExecutableNameNoVersion", "target/my-cool-app.jar") in ids
assert make_id("pom.xml", "project", "distributionManagement", "status", "deployed") in ids
assert make_id("pom.xml", "project", "distributionManagement", "downloadUrl", "http://test/my-project") in ids
assert make_id("pom.xml", "project", "issueManagement", "system", "Bugzilla") in ids
assert make_id("pom.xml", "project", "issueManagement", "url", "http://test/bugzilla/") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-shade-plugin", "version", "maven-shade-plugin:1.4") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-shade-plugin", "groupId", "org.apache.maven.plugins") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-shade-plugin", "artifactId", "maven-shade-plugin") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-shade-plugin", "executions", "execution", "phase", "package") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-shade-plugin", "configuration", "finalName", "test") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-shade-plugin", "executions", "execution", "goals", "goal_shade", "shade") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-compiler-plugin", "version", "maven-compiler-plugin:2.3.2") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-compiler-plugin", "groupId", "org.apache.maven.plugins") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-compiler-plugin", "artifactId", "maven-compiler-plugin") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-compiler-plugin", "configuration", "source", "source:1.6") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin_maven-compiler-plugin", "configuration", "target", "target:1.6") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "version", "1.4") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "groupId", "org.apache.maven.plugins") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "artifactId", "maven-shade-plugin") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "executions", "execution", "phase", "package") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "configuration", "finalName", "test") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "executions", "execution", "goals", "goal", "shade") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-compiler-plugin", "groupId", "org.apache.maven.plugins") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-compiler-plugin", "artifactId", "maven-compiler-plugin") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-compiler-plugin", "configuration", "source", "1.6") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-compiler-plugin", "configuration", "target", "1.6") in ids


def test_config_types(get_plugin):
Expand Down Expand Up @@ -130,7 +128,7 @@ def test_config_types(get_plugin):
version_node = next(
filter(
lambda x: x.id
== make_id("pom.xml", "project", "version", "version:42"),
== make_id("pom.xml", "project", "version", "42"),
nodes,
)
)
Expand All @@ -139,7 +137,7 @@ def test_config_types(get_plugin):
filter(
lambda x: x.id
== make_id(
"pom.xml", "project", "modelVersion", "modelVersion:4.0.0"
"pom.xml", "project", "modelVersion", "4.0.0"
),
nodes,
)
Expand Down
2 changes: 0 additions & 2 deletions tests/files/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
Expand All @@ -36,7 +35,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ index 0000000..ddcc1c2
+ </dependency>
+
+ <dependency>
+ <groupId>selenium</groupId>
+ <artifactId>selenium_artifact</artifactId>
+ <groupId>test</groupId>
+ <artifactId>monitoring</artifactId>
+ <version>5.9</version>
+ </dependency>
+
Expand Down

0 comments on commit b6b1150

Please sign in to comment.