DEV Community

Sachin Varghese
Sachin Varghese

Posted on

Py compare script 3

`import os
import difflib

def extract_method_names(file_path):
"""Extract method names from a Python file."""
method_names = []
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if line.startswith('def '):
# Extract method name
method_name = line.split('(')[0][4:] # Get the name after 'def'
method_names.append(method_name)
return method_names

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

def compare_methods(repo1_methods, repo2_methods):
"""Compare method names from two repositories."""
comparison_results = {}
for package, methods in repo1_methods.items():
if package in repo2_methods:
repo2_method_names = repo2_methods[package]
# Compare method names
diff = difflib.unified_diff(
methods,
repo2_method_names,
lineterm='',
fromfile='Repo1',
tofile='Repo2'
)
comparison_results[package] = list(diff)
return comparison_results

def main(repo1_path, repo2_path):
repo1_methods = get_package_methods(repo1_path)
repo2_methods = get_package_methods(repo2_path)
comparison_results = compare_methods(repo1_methods, repo2_methods)

for package, diffs in comparison_results.items():
    print(f"Comparing methods in package: {package}")
    for line in diffs:
        print(line)
Enter fullscreen mode Exit fullscreen mode

if name == "main":
# Replace these paths with the actual paths to your repositories
repo1_path = '/path/to/repo1'
repo2_path = '/path/to/repo2'
main(repo1_path, repo2_path)`

Top comments (0)