|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2017 The Dart project authors. All rights reserved. |
| 4 | +# Use of this source code is governed by a BSD-style license that can be |
| 5 | +# found in the LICENSE file. |
| 6 | + |
| 7 | +# Usage: tools/dart/create_updated_flutter_deps.py [-d dart/DEPS] [-f flutter/DEPS] |
| 8 | +# |
| 9 | +# This script parses existing flutter DEPS file, identifies all 'dart_' prefixed |
| 10 | +# dependencies, looks up revision from dart DEPS file, updates those dependencies |
| 11 | +# and rewrites flutter DEPS file. |
| 12 | + |
| 13 | +import argparse |
| 14 | +import os |
| 15 | +import sys |
| 16 | + |
| 17 | +DART_SCRIPT_DIR = os.path.dirname(sys.argv[0]) |
| 18 | +DART_ROOT = os.path.realpath(os.path.join(DART_SCRIPT_DIR, '../../third_party/dart')) |
| 19 | +FLUTTER_ROOT = os.path.realpath(os.path.join(DART_SCRIPT_DIR, '../../flutter')) |
| 20 | + |
| 21 | +class VarImpl(object): |
| 22 | + def __init__(self, local_scope): |
| 23 | + self._local_scope = local_scope |
| 24 | + |
| 25 | + def Lookup(self, var_name): |
| 26 | + """Implements the Var syntax.""" |
| 27 | + if var_name in self._local_scope.get("vars", {}): |
| 28 | + return self._local_scope["vars"][var_name] |
| 29 | + if var_name == 'host_os': |
| 30 | + return 'linux' # assume some default value |
| 31 | + if var_name == 'host_cpu': |
| 32 | + return 'x64' # assume some default value |
| 33 | + raise Exception("Var is not defined: %s" % var_name) |
| 34 | + |
| 35 | + |
| 36 | +def ParseDepsFile(deps_file): |
| 37 | + local_scope = {} |
| 38 | + var = VarImpl(local_scope) |
| 39 | + global_scope = { |
| 40 | + 'Var': var.Lookup, |
| 41 | + 'deps_os': {}, |
| 42 | + } |
| 43 | + # Read the content. |
| 44 | + with open(deps_file, 'r') as fp: |
| 45 | + deps_content = fp.read() |
| 46 | + |
| 47 | + # Eval the content. |
| 48 | + exec(deps_content, global_scope, local_scope) |
| 49 | + |
| 50 | + return (local_scope.get('vars', {}), local_scope.get('deps', {})) |
| 51 | + |
| 52 | +def ParseArgs(args): |
| 53 | + args = args[1:] |
| 54 | + parser = argparse.ArgumentParser( |
| 55 | + description='A script to generate updated dart dependencies for flutter DEPS.') |
| 56 | + parser.add_argument('--dart_deps', '-d', |
| 57 | + type=str, |
| 58 | + help='Dart DEPS file.', |
| 59 | + default=os.path.join(DART_ROOT, 'DEPS')) |
| 60 | + parser.add_argument('--flutter_deps', '-f', |
| 61 | + type=str, |
| 62 | + help='Flutter DEPS file.', |
| 63 | + default=os.path.join(FLUTTER_ROOT, 'DEPS')) |
| 64 | + return parser.parse_args(args) |
| 65 | + |
| 66 | +def Main(argv): |
| 67 | + args = ParseArgs(argv) |
| 68 | + (new_vars, new_deps) = ParseDepsFile(args.dart_deps) |
| 69 | + (old_vars, old_deps) = ParseDepsFile(args.flutter_deps) |
| 70 | + |
| 71 | + updated_vars = {} |
| 72 | + |
| 73 | + # Collect updated dependencies |
| 74 | + for (k,v) in sorted(old_vars.items()): |
| 75 | + if k not in ('dart_revision', 'dart_git') and k.startswith('dart_'): |
| 76 | + dart_key = k[len('dart_'):] |
| 77 | + if dart_key in new_vars: |
| 78 | + updated_revision = new_vars[dart_key].lstrip('@') if dart_key in new_vars else v |
| 79 | + updated_vars[k] = updated_revision |
| 80 | + |
| 81 | + # Write updated DEPS file to a side |
| 82 | + updatedfilename = args.flutter_deps + ".new" |
| 83 | + updatedfile = open(updatedfilename, "w") |
| 84 | + file = open(args.flutter_deps) |
| 85 | + lines = file.readlines() |
| 86 | + i = 0 |
| 87 | + while i < len(lines): |
| 88 | + updatedfile.write(lines[i]) |
| 89 | + if lines[i].startswith(" 'dart_revision':"): |
| 90 | + i = i + 2 |
| 91 | + updatedfile.writelines([ |
| 92 | + '\n', |
| 93 | + ' # WARNING: DO NOT EDIT MANUALLY\n', |
| 94 | + ' # The lines between blank lines above and below are generated by a script. See create_updated_flutter_deps.py\n']) |
| 95 | + while i < len(lines) and len(lines[i].strip()) > 0: |
| 96 | + i = i + 1 |
| 97 | + for (k, v) in sorted(updated_vars.items()): |
| 98 | + updatedfile.write(" '%s': '%s',\n" % (k, v)) |
| 99 | + updatedfile.write('\n') |
| 100 | + |
| 101 | + elif lines[i].startswith(" # WARNING: Unused Dart dependencies"): |
| 102 | + updatedfile.write('\n') |
| 103 | + i = i + 1 |
| 104 | + while i < len(lines) and (lines[i].startswith(" # WARNING: end of dart dependencies") == 0): |
| 105 | + i = i + 1 |
| 106 | + for (k, v) in sorted(old_deps.items()): |
| 107 | + if (k.startswith('src/third_party/dart/')): |
| 108 | + for (dart_k, dart_v) in (list(new_deps.items())): |
| 109 | + dart_k_suffix = dart_k[len('sdk/') if dart_k.startswith('sdk/') else 0:] |
| 110 | + if (k.endswith(dart_k_suffix)): |
| 111 | + if (isinstance(dart_v, str)): |
| 112 | + updated_value = dart_v.replace(new_vars["dart_git"], "Var('dart_git') + '/") |
| 113 | + updated_value = updated_value.replace(old_vars["chromium_git"], "Var('chromium_git') + '") |
| 114 | + |
| 115 | + plain_v = dart_k[dart_k.rfind('/') + 1:] |
| 116 | + # This dependency has to be special-cased here because the |
| 117 | + # repository name is not the same as the directory name. |
| 118 | + if plain_v == "quiver": |
| 119 | + plain_v = "quiver-dart" |
| 120 | + if ('dart_' + plain_v + '_tag' in updated_vars): |
| 121 | + updated_value = updated_value[:updated_value.rfind('@')] + "' + '@' + Var('dart_" + plain_v + "_tag')" |
| 122 | + elif ('dart_' + plain_v + '_rev' in updated_vars): |
| 123 | + updated_value = updated_value[:updated_value.rfind('@')] + "' + '@' + Var('dart_" + plain_v + "_rev')" |
| 124 | + else: |
| 125 | + updated_value = updated_value + "'" |
| 126 | + else: |
| 127 | + # Non-string values(dicts) copy verbatim, keeping them sorted |
| 128 | + # to ensure stable ordering of items. |
| 129 | + updated_value = dict(sorted(dart_v.items())) |
| 130 | + |
| 131 | + updatedfile.write(" '%s':\n %s,\n\n" % (k, updated_value)) |
| 132 | + break |
| 133 | + updatedfile.write(lines[i]) |
| 134 | + i = i + 1 |
| 135 | + |
| 136 | + # Rename updated DEPS file into a new DEPS file |
| 137 | + os.remove(args.flutter_deps) |
| 138 | + os.rename(updatedfilename, args.flutter_deps) |
| 139 | + |
| 140 | + return 0 |
| 141 | + |
| 142 | +if __name__ == '__main__': |
| 143 | + sys.exit(Main(sys.argv)) |
0 commit comments