|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import yaml |
| 3 | +import sys |
| 4 | +import re |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +def add_selenium_version(version): |
| 8 | + """ |
| 9 | + Add a new Selenium version configuration to selenium-matrix.yml |
| 10 | + |
| 11 | + Args: |
| 12 | + version (str): The Selenium version to add (e.g., '4.36.0') |
| 13 | + """ |
| 14 | + # Validate version format |
| 15 | + if not re.match(r'^\d+\.\d+\.\d+$', version): |
| 16 | + print(f"Error: Version '{version}' is not in the correct format (e.g., 4.36.0)") |
| 17 | + sys.exit(1) |
| 18 | + |
| 19 | + matrix_file = Path(__file__).parent / 'selenium-matrix.yml' |
| 20 | + |
| 21 | + def replace_none(d): |
| 22 | + if isinstance(d, dict): |
| 23 | + return {k: replace_none(v) for k, v in d.items()} |
| 24 | + elif isinstance(d, list): |
| 25 | + return [replace_none(x) for x in d] |
| 26 | + elif d is None: |
| 27 | + return [] |
| 28 | + return d |
| 29 | + |
| 30 | + # Read the existing YAML file |
| 31 | + with open(matrix_file, 'r') as f: |
| 32 | + try: |
| 33 | + data = yaml.safe_load(f) or {} |
| 34 | + # Replace None values with empty strings |
| 35 | + data = replace_none(data) |
| 36 | + except yaml.YAMLError as e: |
| 37 | + print(f"Error reading YAML file: {e}") |
| 38 | + sys.exit(1) |
| 39 | + |
| 40 | + # Check if version already exists |
| 41 | + if version in data.get('matrix', {}).get('selenium', {}): |
| 42 | + print(f"Version {version} already exists in the matrix") |
| 43 | + sys.exit(0) |
| 44 | + |
| 45 | + # Create the new version entry |
| 46 | + new_entry = { |
| 47 | + 'BASE_RELEASE': f'selenium-{version}', |
| 48 | + 'BASE_VERSION': version, |
| 49 | + 'VERSION': version, |
| 50 | + 'BINDING_VERSION': version, |
| 51 | + 'browser': [] |
| 52 | + } |
| 53 | + |
| 54 | + # Add the new version to the matrix |
| 55 | + data['matrix']['selenium'][version] = new_entry |
| 56 | + |
| 57 | + # Sort the selenium versions in descending order |
| 58 | + if 'selenium' in data['matrix']: |
| 59 | + sorted_selenium = {} |
| 60 | + # Get all versions, handle both string and numeric versions correctly |
| 61 | + versions = [] |
| 62 | + for v in data['matrix']['selenium'].keys(): |
| 63 | + try: |
| 64 | + # Convert version string to tuple of integers for proper numeric comparison |
| 65 | + ver_tuple = tuple(map(int, v.split('.'))) if v != 'nightly' else (float('inf'),) |
| 66 | + versions.append((ver_tuple, v)) |
| 67 | + except (ValueError, AttributeError): |
| 68 | + # Fallback for non-numeric versions (like 'nightly') |
| 69 | + versions.append((v, v)) |
| 70 | + |
| 71 | + # Sort in descending order, with 'nightly' first, then by version numbers |
| 72 | + versions.sort(reverse=True, key=lambda x: (x[0] == 'nightly', x[0])) |
| 73 | + |
| 74 | + # Rebuild the selenium dictionary in sorted order |
| 75 | + for ver_tuple, ver in versions: |
| 76 | + sorted_selenium[ver] = data['matrix']['selenium'][ver] |
| 77 | + |
| 78 | + data['matrix']['selenium'] = sorted_selenium |
| 79 | + |
| 80 | + # Write back to the file while preserving the original structure |
| 81 | + with open(matrix_file, 'w') as f: |
| 82 | + yaml.dump(data, f, default_flow_style=False, sort_keys=False, width=1000) |
| 83 | + |
| 84 | + print(f"Successfully added Selenium version {version} to the matrix") |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + if len(sys.argv) != 2: |
| 88 | + print(f"Usage: {sys.argv[0]} <version>") |
| 89 | + print("Example: python add_selenium_version.py 4.36.0") |
| 90 | + sys.exit(1) |
| 91 | + |
| 92 | + version = sys.argv[1].strip() |
| 93 | + add_selenium_version(version) |
0 commit comments