DEV Community

Sachin Varghese
Sachin Varghese

Posted on

Py compare script 5

import os
import re
import difflib

def extract_java_method_names(file_path):
    """Extract method names from a Java file."""
    method_names = []
    method_pattern = re.compile(r'^(\bpublic\b|\bprotected\b|\bprivate\b|\bstatic\b\s*)*(?=\w+\s+\w+\s*\([^)]*\))\s*\w+\s*\([^)]*\)\s*{?')
    with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
        for line in file:
            line = line.strip()
            match = method_pattern.match(line)
            if match:
                method_name = match.group(2)
                method_names.append(method_name)
    return method_names

def get_all_method_names(repo_path):
    """Get all method names from Java files in a repository."""
    package_methods = {}
    for root, _, files in os.walk(repo_path):
        for file in files:
            if file.endswith('.java'):
                file_path = os.path.join(root, file)
                methods = extract_java_method_names(file_path)
                package_name = os.path.relpath(root, repo_path).replace(os.sep, '.')
                if package_name not in package_methods:
                    package_methods[package_name] = []
                package_methods[package_name].extend(methods)
    return package_methods

def compare_methods(repo1_methods, repo2_methods):
    """Compare method names from two repositories."""
    comparison_results = {}
    all_packages = set(repo1_methods.keys()).union(repo2_methods.keys())
    for package in all_packages:
        methods1 = set(repo1_methods.get(package, []))
        methods2 = set(repo2_methods.get(package, []))
        only_in_repo1 = methods1 - methods2
        only_in_repo2 = methods2 - methods1
        if only_in_repo1 or only_in_repo2:
            comparison_results[package] = {
                'only_in_repo1': sorted(only_in_repo1),
                'only_in_repo2': sorted(only_in_repo2)
            }
    return comparison_results

def main(repo1_path, repo2_path):
    repo1_methods = get_all_method_names(repo1_path)
    repo2_methods = get_all_method_names(repo2_path)
    comparison_results = compare_methods(repo1_methods, repo2_methods)
    for package, diffs in comparison_results.items():
        print(f"\nComparing methods in package: {package}")
        if diffs['only_in_repo1']:
            print("Methods only in Repo1:")
            for method in diffs['only_in_repo1']:
                print(f"  - {method}")
        if diffs['only_in_repo2']:
            print("Methods only in Repo2:")
            for method in diffs['only_in_repo2']:
                print(f"  - {method}")

if __name__ == "__main__":
    # Replace these paths with the actual paths to your Java repositories
    repo1_path = ""
    repo2_path = ""
    main(repo1_path, repo2_path)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)