|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import re |
| 4 | +import glob |
| 5 | +import json |
| 6 | + |
| 7 | + |
| 8 | +def parse_module_file(module_file_path): |
| 9 | + """ |
| 10 | + Extracts module name, version, and extensions from a module file. |
| 11 | + """ |
| 12 | + module_name = os.path.basename(os.path.dirname(module_file_path)) |
| 13 | + version = os.path.basename(module_file_path) |
| 14 | + |
| 15 | + try: |
| 16 | + with open(module_file_path, "r") as file: |
| 17 | + content = file.read() |
| 18 | + |
| 19 | + # Extract extensions from content using regex |
| 20 | + match = re.search(r'extensions\("(.+)"\)', content) |
| 21 | + extensions = [] |
| 22 | + |
| 23 | + if match: |
| 24 | + # Split the list of packages by commas |
| 25 | + packages = match.group(1) |
| 26 | + for pkg in packages.split(","): |
| 27 | + parts = pkg.split("/") |
| 28 | + |
| 29 | + # Check if the package is in the name/version format |
| 30 | + if len(parts) == 2: |
| 31 | + extensions.append((parts[0], parts[1])) |
| 32 | + elif len(parts) == 1: |
| 33 | + extensions.append((parts[0], "none")) |
| 34 | + else: |
| 35 | + print(f"Warning: Skipping invalid package format: {pkg}") |
| 36 | + |
| 37 | + return {(module_name, version): tuple(extensions)} |
| 38 | + |
| 39 | + except Exception as e: |
| 40 | + print(f"Error parsing module file {module_file_path}: {e}") |
| 41 | + return {(module_name, version): ()} |
| 42 | + |
| 43 | + |
| 44 | +def get_available_modules(base_dir): |
| 45 | + """ |
| 46 | + Get the list of modules from all subdirectories inside the specified base directory. |
| 47 | + """ |
| 48 | + try: |
| 49 | + modules = {} |
| 50 | + # Only look for .lua files |
| 51 | + for module_path in glob.glob(os.path.join(base_dir, "*/*.lua")): |
| 52 | + modules.update(parse_module_file(module_path)) |
| 53 | + return modules |
| 54 | + |
| 55 | + except Exception as e: |
| 56 | + print(f"Error retrieving modules from {base_dir}: {e}") |
| 57 | + return {} |
| 58 | + |
| 59 | + |
| 60 | +def compare_stacks(dir1, dir2): |
| 61 | + """ |
| 62 | + Compare two sets of Lmod module files, including versions and extensions. |
| 63 | + """ |
| 64 | + modules1 = get_available_modules(dir1) |
| 65 | + modules2 = get_available_modules(dir2) |
| 66 | + |
| 67 | + # Find differences between the two dictionaries |
| 68 | + modules_removed = set(modules1.keys()) - set(modules2.keys()) |
| 69 | + modules_added = set(modules2.keys()) - set(modules1.keys()) |
| 70 | + matching_keys = set(modules1.keys()) & set(modules2.keys()) |
| 71 | + |
| 72 | + diff_results = { |
| 73 | + "module_differences": { |
| 74 | + "missing": list("/".join(module) for module in modules_removed), |
| 75 | + "added": list("/".join(module) for module in modules_added), |
| 76 | + }, |
| 77 | + "extension_differences": [], |
| 78 | + } |
| 79 | + |
| 80 | + # Compare extensions for matching keys |
| 81 | + for key in matching_keys: |
| 82 | + if modules1[key] != modules2[key]: |
| 83 | + diff_results["extension_differences"].append( |
| 84 | + { |
| 85 | + "/".join(key): { |
| 86 | + "missing": list( |
| 87 | + "/".join(key) |
| 88 | + for key in list(set(modules1[key]) - set(modules2[key])) |
| 89 | + ), |
| 90 | + "added": list( |
| 91 | + "/".join(key) |
| 92 | + for key in list(set(modules2[key]) - set(modules1[key])) |
| 93 | + ), |
| 94 | + } |
| 95 | + } |
| 96 | + ) |
| 97 | + |
| 98 | + return diff_results |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + # Set up argument parser |
| 103 | + parser = argparse.ArgumentParser(description="Compare two Lmod module directories") |
| 104 | + parser.add_argument("path1", type=str, help="The first directory path") |
| 105 | + parser.add_argument("path2", type=str, help="The second directory path") |
| 106 | + |
| 107 | + # Parse the arguments |
| 108 | + args = parser.parse_args() |
| 109 | + |
| 110 | + # Validate the paths |
| 111 | + for path in [args.path1, args.path2]: |
| 112 | + if not os.path.exists(path): |
| 113 | + print(f"Warning: Path does not exist: {path}") |
| 114 | + |
| 115 | + # Compare the stacks |
| 116 | + diff_results = compare_stacks(args.path1, args.path2) |
| 117 | + |
| 118 | + # Print the differences |
| 119 | + if any( |
| 120 | + [ |
| 121 | + diff_results["module_differences"]["missing"], |
| 122 | + diff_results["module_differences"]["added"], |
| 123 | + diff_results["extension_differences"], |
| 124 | + ] |
| 125 | + ): |
| 126 | + print(json.dumps(diff_results, indent=2)) |
| 127 | + exit(1) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + main() |
0 commit comments