|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import sys |
| 5 | +import os |
| 6 | + |
| 7 | +def process_sarif_file(sarif_file, renamed_files_file): |
| 8 | + """ |
| 9 | + Process SARIF file to rename files back to .ino and adjust line numbers |
| 10 | + """ |
| 11 | + # Read the renamed files mapping |
| 12 | + with open(renamed_files_file, 'r') as f: |
| 13 | + renamed_files = json.load(f) |
| 14 | + |
| 15 | + # Read the SARIF file |
| 16 | + with open(sarif_file, 'r') as f: |
| 17 | + sarif_data = json.load(f) |
| 18 | + |
| 19 | + # Process each result |
| 20 | + if 'runs' in sarif_data and len(sarif_data['runs']) > 0: |
| 21 | + results = sarif_data['runs'][0].get('results', []) |
| 22 | + |
| 23 | + for result in results: |
| 24 | + if 'locations' in result and len(result['locations']) > 0: |
| 25 | + location = result['locations'][0] |
| 26 | + |
| 27 | + if 'physicalLocation' in location: |
| 28 | + physical_location = location['physicalLocation'] |
| 29 | + |
| 30 | + if 'artifactLocation' in physical_location: |
| 31 | + uri = physical_location['artifactLocation'].get('uri') |
| 32 | + |
| 33 | + # Check if this file was originally a .ino file |
| 34 | + if uri in renamed_files: |
| 35 | + # Rename the file back to .ino |
| 36 | + physical_location['artifactLocation']['uri'] = renamed_files[uri] |
| 37 | + |
| 38 | + # Adjust line numbers if they exist |
| 39 | + if 'region' in physical_location: |
| 40 | + region = physical_location['region'] |
| 41 | + |
| 42 | + if 'startLine' in region: |
| 43 | + region['startLine'] = max(1, region['startLine'] - 1) |
| 44 | + |
| 45 | + if 'endLine' in region: |
| 46 | + region['endLine'] = max(1, region['endLine'] - 1) |
| 47 | + |
| 48 | + # Write the processed SARIF file |
| 49 | + with open(sarif_file, 'w') as f: |
| 50 | + json.dump(sarif_data, f, indent=2) |
| 51 | + |
| 52 | +def main(): |
| 53 | + if len(sys.argv) != 3: |
| 54 | + print("Usage: python3 sarif_nobuild.py <sarif_file> <renamed_files_file>") |
| 55 | + sys.exit(1) |
| 56 | + |
| 57 | + sarif_file = sys.argv[1] |
| 58 | + renamed_files_file = sys.argv[2] |
| 59 | + |
| 60 | + # Check if files exist |
| 61 | + if not os.path.exists(sarif_file): |
| 62 | + print(f"SARIF file not found: {sarif_file}") |
| 63 | + sys.exit(1) |
| 64 | + |
| 65 | + if not os.path.exists(renamed_files_file): |
| 66 | + print(f"Renamed files mapping not found: {renamed_files_file}") |
| 67 | + sys.exit(1) |
| 68 | + |
| 69 | + try: |
| 70 | + process_sarif_file(sarif_file, renamed_files_file) |
| 71 | + print("SARIF file processed successfully") |
| 72 | + except Exception as e: |
| 73 | + print(f"Error processing SARIF file: {e}") |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + main() |
0 commit comments