|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:io' as io; |
| 7 | + |
| 8 | +import 'package:args/args.dart'; |
| 9 | +import 'package:engine_repo_tools/engine_repo_tools.dart'; |
| 10 | +import 'package:meta/meta.dart'; |
| 11 | +import 'package:path/path.dart' as p; |
| 12 | + |
| 13 | +/// "Downloads" (i.e. decodes base64 encoded strings) goldens from buildbucket. |
| 14 | +/// |
| 15 | +/// See ../README.md for motivation and usage. |
| 16 | +final class BuildBucketGoldenScraper { |
| 17 | + /// Creates a scraper with the given configuration. |
| 18 | + BuildBucketGoldenScraper({ |
| 19 | + required this.pathOrUrl, |
| 20 | + this.dryRun = false, |
| 21 | + String? engineSrcPath, |
| 22 | + StringSink? outSink, |
| 23 | + }) : |
| 24 | + engine = engineSrcPath != null ? |
| 25 | + Engine.fromSrcPath(engineSrcPath) : |
| 26 | + Engine.findWithin(p.dirname(p.fromUri(io.Platform.script))), |
| 27 | + _outSink = outSink ?? io.stdout; |
| 28 | + |
| 29 | + /// Creates a scraper from the command line arguments. |
| 30 | + /// |
| 31 | + /// Throws [FormatException] if the arguments are invalid. |
| 32 | + factory BuildBucketGoldenScraper.fromCommandLine( |
| 33 | + List<String> args, { |
| 34 | + StringSink? outSink, |
| 35 | + StringSink? errSink, |
| 36 | + }) { |
| 37 | + outSink ??= io.stdout; |
| 38 | + errSink ??= io.stderr; |
| 39 | + |
| 40 | + final ArgResults argResults = _argParser.parse(args); |
| 41 | + if (argResults['help'] as bool) { |
| 42 | + _usage(args); |
| 43 | + } |
| 44 | + final String? pathOrUrl = argResults.rest.isEmpty ? null : argResults.rest.first; |
| 45 | + if (pathOrUrl == null) { |
| 46 | + _usage(args); |
| 47 | + } |
| 48 | + return BuildBucketGoldenScraper( |
| 49 | + pathOrUrl: pathOrUrl, |
| 50 | + dryRun: argResults['dry-run'] as bool, |
| 51 | + outSink: outSink, |
| 52 | + engineSrcPath: argResults['engine-src-path'] as String?, |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + static Never _usage(List<String> args) { |
| 57 | + final StringBuffer output = StringBuffer(); |
| 58 | + output.writeln('Usage: build_bucket_golden_scraper [options] <path or URL>'); |
| 59 | + output.writeln(); |
| 60 | + output.writeln(_argParser.usage); |
| 61 | + throw FormatException(output.toString(), args.join(' ')); |
| 62 | + } |
| 63 | + |
| 64 | + static final ArgParser _argParser = ArgParser() |
| 65 | + ..addFlag( |
| 66 | + 'help', |
| 67 | + abbr: 'h', |
| 68 | + help: 'Print this help message.', |
| 69 | + negatable: false, |
| 70 | + ) |
| 71 | + ..addFlag( |
| 72 | + 'dry-run', |
| 73 | + help: "If true, don't write any files to disk (other than temporary files).", |
| 74 | + negatable: false, |
| 75 | + ) |
| 76 | + ..addOption( |
| 77 | + 'engine-src-path', |
| 78 | + help: 'The path to the engine source code.', |
| 79 | + valueHelp: 'path/that/contains/src (defaults to the directory containing this script)', |
| 80 | + ); |
| 81 | + |
| 82 | + /// A local path or a URL to a buildbucket log file. |
| 83 | + final String pathOrUrl; |
| 84 | + |
| 85 | + /// If true, don't write any files to disk (other than temporary files). |
| 86 | + final bool dryRun; |
| 87 | + |
| 88 | + /// The path to the engine source code. |
| 89 | + final Engine engine; |
| 90 | + |
| 91 | + /// How to print output, typically [io.stdout]. |
| 92 | + final StringSink _outSink; |
| 93 | + |
| 94 | + /// Runs the scraper. |
| 95 | + Future<int> run() async { |
| 96 | + // If the path is a URL, download it and store it in a temporary file. |
| 97 | + final Uri? maybeUri = Uri.tryParse(pathOrUrl); |
| 98 | + if (maybeUri == null) { |
| 99 | + throw FormatException('Invalid path or URL: $pathOrUrl'); |
| 100 | + } |
| 101 | + |
| 102 | + final String contents; |
| 103 | + if (maybeUri.hasScheme) { |
| 104 | + contents = await _downloadFile(maybeUri); |
| 105 | + } else { |
| 106 | + final io.File readFile = io.File(pathOrUrl); |
| 107 | + if (!readFile.existsSync()) { |
| 108 | + throw FormatException('File does not exist: $pathOrUrl'); |
| 109 | + } |
| 110 | + contents = readFile.readAsStringSync(); |
| 111 | + } |
| 112 | + |
| 113 | + // Check that it is a buildbucket log file. |
| 114 | + if (!contents.contains(_buildBucketMagicString)) { |
| 115 | + throw FormatException('Not a buildbucket log file: $pathOrUrl'); |
| 116 | + } |
| 117 | + |
| 118 | + // Check for occurences of a base64 encoded string. |
| 119 | + // |
| 120 | + // The format looks something like this: |
| 121 | + // [LINE N+0]: See also the base64 encoded /b/s/w/ir/cache/builder/src/flutter/testing/resources/performance_overlay_gold_120fps_new.png: |
| 122 | + // [LINE N+1]: {{BASE_64_ENCODED_IMAGE}} |
| 123 | + // |
| 124 | + // We want to extract the file name (relative to the engine root) and then |
| 125 | + // decode the base64 encoded string (and write it to disk if we are not in |
| 126 | + // dry-run mode). |
| 127 | + final List<_Golden> goldens = <_Golden>[]; |
| 128 | + final List<String> lines = contents.split('\n'); |
| 129 | + for (int i = 0; i < lines.length; i++) { |
| 130 | + final String line = lines[i]; |
| 131 | + if (line.startsWith(_base64MagicString)) { |
| 132 | + final String relativePath = line.split(_buildBucketMagicString).last.split(':').first; |
| 133 | + |
| 134 | + // Remove the _new suffix from the file name. |
| 135 | + final String pathWithouNew = relativePath.replaceAll('_new', ''); |
| 136 | + |
| 137 | + final String base64EncodedString = lines[i + 1]; |
| 138 | + final List<int> bytes = base64Decode(base64EncodedString); |
| 139 | + final io.File outFile = io.File(p.join(engine.srcDir.path, pathWithouNew)); |
| 140 | + goldens.add(_Golden(outFile, bytes)); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (goldens.isEmpty) { |
| 145 | + _outSink.writeln('No goldens found.'); |
| 146 | + return 0; |
| 147 | + } |
| 148 | + |
| 149 | + // Sort and de-duplicate the goldens. |
| 150 | + goldens.sort(); |
| 151 | + final Set<_Golden> uniqueGoldens = goldens.toSet(); |
| 152 | + |
| 153 | + // Write the goldens to disk (or pretend to in dry-run mode). |
| 154 | + _outSink.writeln('${dryRun ? 'Found' : 'Wrote'} ${uniqueGoldens.length} golden file changes:'); |
| 155 | + for (final _Golden golden in uniqueGoldens) { |
| 156 | + final String truncatedPathAfterFlutterDir = golden.outFile.path.split('flutter${p.separator}').last; |
| 157 | + _outSink.writeln(' $truncatedPathAfterFlutterDir'); |
| 158 | + if (!dryRun) { |
| 159 | + await golden.outFile.writeAsBytes(golden.bytes); |
| 160 | + } |
| 161 | + } |
| 162 | + if (dryRun) { |
| 163 | + _outSink.writeln('Run again without --dry-run to apply these changes.'); |
| 164 | + } |
| 165 | + |
| 166 | + return 0; |
| 167 | + } |
| 168 | + |
| 169 | + static const String _buildBucketMagicString = '/b/s/w/ir/cache/builder/src/'; |
| 170 | + static const String _base64MagicString = 'See also the base64 encoded $_buildBucketMagicString'; |
| 171 | + |
| 172 | + static Future<String> _downloadFile(Uri uri) async { |
| 173 | + final io.HttpClient client = io.HttpClient(); |
| 174 | + final io.HttpClientRequest request = await client.getUrl(uri); |
| 175 | + final io.HttpClientResponse response = await request.close(); |
| 176 | + final StringBuffer contents = StringBuffer(); |
| 177 | + await response.transform(utf8.decoder).forEach(contents.write); |
| 178 | + client.close(); |
| 179 | + return contents.toString(); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +@immutable |
| 184 | +final class _Golden implements Comparable<_Golden> { |
| 185 | + const _Golden(this.outFile, this.bytes); |
| 186 | + |
| 187 | + /// Where to write the golden file. |
| 188 | + final io.File outFile; |
| 189 | + |
| 190 | + /// The bytes of the golden file to write. |
| 191 | + final List<int> bytes; |
| 192 | + |
| 193 | + @override |
| 194 | + int get hashCode => outFile.path.hashCode; |
| 195 | + |
| 196 | + @override |
| 197 | + bool operator ==(Object other) { |
| 198 | + return other is _Golden && other.outFile.path == outFile.path; |
| 199 | + } |
| 200 | + |
| 201 | + @override |
| 202 | + int compareTo(_Golden other) { |
| 203 | + return outFile.path.compareTo(other.outFile.path); |
| 204 | + } |
| 205 | +} |
0 commit comments