Skip to content

Commit

Permalink
Add option to get all system level config files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Simon committed Sep 27, 2024
1 parent 7ece969 commit 6b2a8d2
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/cfgnet/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def main(verbose: bool):
@click.option("-b", "--enable-static-blacklist", is_flag=True)
@click.option("-i", "--enable-internal-links", is_flag=True)
@click.option("-c", "--enable-all-conflicts", is_flag=True)
@click.option("-s", "--system_level", is_flag=False)
@click.option("-f", "--config-files", multiple=True)
@add_project_root_argument
@add_enable_linker_option
Expand All @@ -60,6 +61,7 @@ def init(
enable_static_blacklist: bool,
enable_internal_links: bool,
enable_all_conflicts: bool,
system_level: bool,
project_root: str,
enable_linker: List[str],
disable_linker: List[str],
Expand All @@ -76,6 +78,7 @@ def init(
enable_internal_links=enable_internal_links,
enabled_linkers=list(set(enable_linker) - set(disable_linker)),
enable_all_conflicts=enable_all_conflicts,
system_level=system_level,
)
LinkerManager.set_enabled_linkers(network_configuration.enabled_linkers)
logger.configure_repo_logger(network_configuration.logfile_path())
Expand Down Expand Up @@ -135,6 +138,7 @@ def validate(project_root: str):
@click.option("-i", "--enable-internal-links", is_flag=True)
@click.option("-c", "--enable-all-conflicts", is_flag=True)
@click.option("-f", "--config-files", multiple=True)
@click.option("-s", "--system_level", multiple=True)
@add_project_root_argument
@add_enable_linker_option
@add_disable_linker_option
Expand All @@ -146,6 +150,7 @@ def analyze(
enable_linker: List[str],
disable_linker: List[str],
config_files: List,
system_level: bool,
):
"""Run self-evaluating analysis of commit history."""
project_name = os.path.basename(project_root)
Expand All @@ -159,6 +164,7 @@ def analyze(
enable_internal_links=enable_internal_links,
enabled_linkers=list(set(enable_linker) - set(disable_linker)),
enable_all_conflicts=enable_all_conflicts,
system_level=system_level,
)
LinkerManager.set_enabled_linkers(network_configuration.enabled_linkers)
logger.configure_repo_logger(network_configuration.logfile_path())
Expand Down Expand Up @@ -223,11 +229,10 @@ def export(
@main.command()
@click.option("-f", "--config-files", multiple=True)
@click.option("-o", "--output", required=True)
@click.option("-f", "--system_level", is_flag=False)
@add_project_root_argument
def extract(
project_root: str,
config_files: List,
output: str,
project_root: str, config_files: List, output: str, system_level: bool
):
"""Extract key-value pairs."""
project_name = os.path.basename(project_root)
Expand All @@ -239,6 +244,7 @@ def extract(
enable_static_blacklist=False,
enable_internal_links=False,
enable_all_conflicts=False,
system_level=system_level,
)

start = time.time()
Expand Down
7 changes: 6 additions & 1 deletion src/cfgnet/network/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
)
from cfgnet.network.network_configuration import NetworkConfiguration
from cfgnet.exporter.exporter import DotExporter, JSONExporter
from cfgnet.utility.util import is_test_file
from cfgnet.utility.util import is_test_file, get_system_files


class Network:
Expand Down Expand Up @@ -291,6 +291,10 @@ def init_network(cfg: NetworkConfiguration) -> Network:
if cfg.config_files:
tracked_files.update(cfg.config_files)

if cfg.system_level:
system_files = get_system_files()
tracked_files.update(system_files)

project_name = cfg.project_name()
root = ProjectNode(name=project_name, root_dir=cfg.project_root_abs)
network = Network(project_name=project_name, root=root, cfg=cfg)
Expand All @@ -308,6 +312,7 @@ def init_network(cfg: NetworkConfiguration) -> Network:
plugins, abs_file_path
)
if plugin:
print("File to parse: ", abs_file_path)
try:
plugin.parse_file(
abs_file_path=abs_file_path,
Expand Down
1 change: 1 addition & 0 deletions src/cfgnet/network/network_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class NetworkConfiguration:
enable_static_blacklist: bool
enable_internal_links: bool
enable_all_conflicts: bool
system_level: bool
# Path to CfgNet data directory relative to project_root
cfgnet_path_rel: str = ".cfgnet"
# List of names of enabled linkers
Expand Down
47 changes: 47 additions & 0 deletions src/cfgnet/utility/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
import os
import platform
import logging
from typing import Set, Optional


def is_test_file(abs_file_path) -> bool:
"""
Check if a given file is a test file.
:return: True if test file else False
"""
test_indicators = ["/tests", "test", "tests"]
return any(indicator in abs_file_path for indicator in test_indicators)


def get_system_config_dir() -> Optional[str]:
"""
Determine the system configuration directory based on the operating system.
:return: Path to the system configuration directory
"""
os_type = platform.system()

print("OS: ", os_type)

if os_type == "Linux":
# Common Linux config directories
return "/etc"

if os_type == "Windows":
# Windows config directory (use environment variables for system paths)
return os.getenv("ProgramData", "C:\\ProgramData")

logging.error("System config directory not defined for OS %s. ", os_type)
return None


def get_system_files() -> Set:
config_dir = get_system_config_dir()
system_files = set()

if config_dir:
for root, _, files in os.walk(config_dir):
for file in files:
print("File: ", file)
abs_file_path = os.path.join(root, file)
system_files.add(abs_file_path)

return system_files
2 changes: 2 additions & 0 deletions tests/cfgnet/analyze/test_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def get_config_(get_repo):
enable_static_blacklist=False,
enable_internal_links=False,
enable_all_conflicts=False,
system_level=False
)

return network_configuration
Expand All @@ -49,6 +50,7 @@ def get_config_all_conflicts_(get_repo):
enable_static_blacklist=False,
enable_internal_links=False,
enable_all_conflicts=True,
system_level=False
)

return network_configuration
Expand Down
5 changes: 5 additions & 0 deletions tests/cfgnet/conflicts/test_conflict_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def get_maven_docker_networks():
enable_static_blacklist=False,
enable_internal_links=False,
enable_all_conflicts=False,
system_level=False
)
ref_network = Network.init_network(cfg=network_configuration)

Expand All @@ -60,6 +61,7 @@ def get_docker_networks():
enable_static_blacklist=False,
enable_internal_links=True,
enable_all_conflicts=False,
system_level=False
)
ref_network = Network.init_network(cfg=network_configuration)

Expand All @@ -82,6 +84,7 @@ def get_port_db_networks():
enable_static_blacklist=False,
enable_internal_links=False,
enable_all_conflicts=False,
system_level=False
)
ref_network = Network.init_network(cfg=network_configuration)

Expand All @@ -104,6 +107,7 @@ def get_nodejs_networks():
enable_static_blacklist=False,
enable_internal_links=False,
enable_all_conflicts=True,
system_level=False
)
ref_network = Network.init_network(cfg=network_configuration)

Expand All @@ -126,6 +130,7 @@ def get_networks_equally_changed():
enable_static_blacklist=False,
enable_internal_links=False,
enable_all_conflicts=False,
system_level=False
)
ref_network = Network.init_network(cfg=network_configuration)

Expand Down
3 changes: 2 additions & 1 deletion tests/cfgnet/exporter/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def get_config_(get_repo):
project_root_abs=os.path.abspath(get_repo.root),
enable_static_blacklist=False,
enable_internal_links=False,
enable_all_conflicts=False
enable_all_conflicts=False,
system_level=False
)

yield network_configuration
Expand Down
3 changes: 2 additions & 1 deletion tests/cfgnet/network/test_ignorefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def test_ignorefile(repo):
project_root_abs=os.path.abspath(repo.root),
enable_static_blacklist=False,
enable_internal_links=False,
enable_all_conflicts=False
enable_all_conflicts=False,
system_level=False
)

network = Network.init_network(cfg)
Expand Down
3 changes: 2 additions & 1 deletion tests/cfgnet/network/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def get_config_(get_repo):
project_root_abs=os.path.abspath(get_repo.root),
enable_static_blacklist=False,
enable_internal_links=False,
enable_all_conflicts=False
enable_all_conflicts=False,
system_level=False
)

return network_configuration
Expand Down

0 comments on commit 6b2a8d2

Please sign in to comment.