|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import csv |
| 5 | +import logging |
| 6 | +import os |
| 7 | +import re |
| 8 | +import sys |
| 9 | +import urllib.request |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | +from include_analysis import IncludeAnalysisOutput, ParseError, parse_raw_include_analysis_output |
| 13 | +from suggest_include_changes import filter_filenames |
| 14 | +from utils import ( |
| 15 | + get_include_analysis_edge_expanded_sizes, |
| 16 | + get_include_analysis_edge_prevalence, |
| 17 | + get_include_analysis_edges_centrality, |
| 18 | + get_latest_include_analysis, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def extract_include_analysis_edges( |
| 23 | + include_analysis: IncludeAnalysisOutput, |
| 24 | + weight_threshold=None, |
| 25 | + filter_generated_files=False, |
| 26 | + filter_mojom_headers=False, |
| 27 | + filter_third_party=False, |
| 28 | +): |
| 29 | + filenames = filter_filenames( |
| 30 | + include_analysis["files"], |
| 31 | + filter_generated_files=filter_generated_files, |
| 32 | + filter_mojom_headers=filter_mojom_headers, |
| 33 | + filter_third_party=filter_third_party, |
| 34 | + ) |
| 35 | + |
| 36 | + expanded_size_edge_weights = get_include_analysis_edge_expanded_sizes(include_analysis) |
| 37 | + prevalence_edge_weights = get_include_analysis_edge_prevalence(include_analysis) |
| 38 | + centrality_edge_weights = get_include_analysis_edges_centrality(include_analysis) |
| 39 | + |
| 40 | + for file in filenames: |
| 41 | + for [include, size] in include_analysis["esizes"][file].items(): |
| 42 | + if weight_threshold and float(size) < weight_threshold: |
| 43 | + continue |
| 44 | + |
| 45 | + prevalence = expanded_size_edge_weights[file][include] |
| 46 | + expanded_size = expanded_size_edge_weights[file][include] |
| 47 | + centrality = centrality_edge_weights[file][include] |
| 48 | + |
| 49 | + yield file, include, size, prevalence, expanded_size, centrality |
| 50 | + |
| 51 | + |
| 52 | +def main(): |
| 53 | + parser = argparse.ArgumentParser(description="Extract include edges from include analysis, with filtering") |
| 54 | + parser.add_argument( |
| 55 | + "include_analysis_output", |
| 56 | + type=argparse.FileType("r"), |
| 57 | + nargs="?", |
| 58 | + help="The include analysis output to use.", |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + "--weight-threshold", type=float, help="Filter out changes with a weight value below the threshold." |
| 62 | + ) |
| 63 | + parser.add_argument( |
| 64 | + "--filter-third-party", action="store_true", help="Filter out third_party/ (excluding blink) and v8." |
| 65 | + ) |
| 66 | + parser.add_argument("--filter-generated-files", action="store_true", help="Filter out generated files.") |
| 67 | + parser.add_argument("--filter-mojom-headers", action="store_true", help="Filter out mojom headers.") |
| 68 | + group = parser.add_mutually_exclusive_group() |
| 69 | + group.add_argument("--quiet", action="store_true", default=False, help="Only log warnings and errors.") |
| 70 | + group.add_argument("--verbose", action="store_true", default=False, help="Enable verbose logging.") |
| 71 | + args = parser.parse_args() |
| 72 | + |
| 73 | + logging.basicConfig( |
| 74 | + format="%(asctime)s - %(levelname)s - %(message)s", |
| 75 | + level=logging.DEBUG if args.verbose else logging.WARNING if args.quiet else logging.INFO, |
| 76 | + ) |
| 77 | + |
| 78 | + # If the user specified an include analysis output file, use that instead of fetching it |
| 79 | + if args.include_analysis_output: |
| 80 | + raw_include_analysis = args.include_analysis_output.read() |
| 81 | + else: |
| 82 | + raw_include_analysis = get_latest_include_analysis() |
| 83 | + |
| 84 | + try: |
| 85 | + include_analysis = parse_raw_include_analysis_output(raw_include_analysis) |
| 86 | + except ParseError as e: |
| 87 | + message = str(e) |
| 88 | + print("error: Could not parse include analysis output file") |
| 89 | + if message: |
| 90 | + print(message) |
| 91 | + return 2 |
| 92 | + |
| 93 | + csv_writer = csv.writer(sys.stdout) |
| 94 | + |
| 95 | + try: |
| 96 | + for row in extract_include_analysis_edges( |
| 97 | + include_analysis, |
| 98 | + weight_threshold=args.weight_threshold, |
| 99 | + filter_generated_files=args.filter_generated_files, |
| 100 | + filter_mojom_headers=args.filter_mojom_headers, |
| 101 | + filter_third_party=args.filter_third_party, |
| 102 | + ): |
| 103 | + csv_writer.writerow(row) |
| 104 | + |
| 105 | + sys.stdout.flush() |
| 106 | + except BrokenPipeError: |
| 107 | + devnull = os.open(os.devnull, os.O_WRONLY) |
| 108 | + os.dup2(devnull, sys.stdout.fileno()) |
| 109 | + sys.exit(1) |
| 110 | + |
| 111 | + return 0 |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + try: |
| 116 | + sys.exit(main()) |
| 117 | + except KeyboardInterrupt: |
| 118 | + pass # Don't show the user anything |
0 commit comments