|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to parse changed_crates file and extract crate names with versions. |
| 4 | +Extracts lines with 'name = "..."' and '+to = "..."' patterns and writes |
| 5 | +the crate names with versions to a new file in format: - crate_name@version |
| 6 | +""" |
| 7 | + |
| 8 | +import re |
| 9 | +import os |
| 10 | +import argparse |
| 11 | + |
| 12 | +def parse_crate_names(input_file, output_file): |
| 13 | + """ |
| 14 | + Parse the input file to extract crate names with versions and write them to output file. |
| 15 | +
|
| 16 | + Args: |
| 17 | + input_file (str): Path to the input file |
| 18 | + output_file (str): Path to the output file |
| 19 | + """ |
| 20 | + crates = [] |
| 21 | + |
| 22 | + # Pattern to match lines with name = "crate-name" |
| 23 | + name_pattern = r'name\s*=\s*"([^"]+)"' |
| 24 | + # Pattern to match lines with +to = "version" |
| 25 | + version_pattern = r'\+to\s*=\s*"([^"]+)"' |
| 26 | + |
| 27 | + try: |
| 28 | + with open(input_file, 'r', encoding='utf-8') as f: |
| 29 | + lines = f.readlines() |
| 30 | + |
| 31 | + current_crate = None |
| 32 | + for line_num, line in enumerate(lines, 1): |
| 33 | + # Look for lines that contain name = "something" |
| 34 | + name_match = re.search(name_pattern, line) |
| 35 | + if name_match: |
| 36 | + current_crate = name_match.group(1) |
| 37 | + print(f"Found crate name: {current_crate} (line {line_num})") |
| 38 | + |
| 39 | + # Look ahead for the +to version line |
| 40 | + # Typically it's within the next few lines |
| 41 | + for lookahead_offset in range(1, 10): |
| 42 | + if line_num - 1 + lookahead_offset < len(lines): |
| 43 | + version_line = lines[line_num - 1 + lookahead_offset] |
| 44 | + version_match = re.search(version_pattern, version_line) |
| 45 | + if version_match: |
| 46 | + version = version_match.group(1) |
| 47 | + crates.append((current_crate, version)) |
| 48 | + print(f" -> Version: {version} (line {line_num + lookahead_offset})") |
| 49 | + break |
| 50 | + |
| 51 | + except FileNotFoundError: |
| 52 | + print(f"Error: Input file '{input_file}' not found.") |
| 53 | + return |
| 54 | + except Exception as e: |
| 55 | + print(f"Error reading input file: {e}") |
| 56 | + return |
| 57 | + |
| 58 | + # Write crate names with versions to output file |
| 59 | + try: |
| 60 | + with open(output_file, 'w', encoding='utf-8') as f: |
| 61 | + f.write("The following crates were updated to the corresponding versions:\n\n") |
| 62 | + for crate_name, version in crates: |
| 63 | + f.write(f"- {crate_name}@{version}\n") |
| 64 | + print(f"\nSuccessfully extracted {len(crates)} crates with versions.") |
| 65 | + print(f"Output written to: {output_file}") |
| 66 | + |
| 67 | + except Exception as e: |
| 68 | + print(f"Error writing output file: {e}") |
| 69 | + |
| 70 | +def main(): |
| 71 | + parser = argparse.ArgumentParser( |
| 72 | + description='Parse changed_crates file and extract crate names.' |
| 73 | + ) |
| 74 | + parser.add_argument( |
| 75 | + 'input_file', |
| 76 | + help='Path to the input file containing crate information' |
| 77 | + ) |
| 78 | + parser.add_argument( |
| 79 | + 'output_file', |
| 80 | + help='Path to the output file where crate names will be written' |
| 81 | + ) |
| 82 | + |
| 83 | + args = parser.parse_args() |
| 84 | + |
| 85 | + print("Parsing crate names from diff file...") |
| 86 | + print(f"Input file: {args.input_file}") |
| 87 | + print(f"Output file: {args.output_file}") |
| 88 | + print("-" * 50) |
| 89 | + |
| 90 | + parse_crate_names(args.input_file, args.output_file) |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + main() |
0 commit comments