|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2013 The Flutter 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: deps_parser.py --deps <DEPS file> --output <flattened deps> |
| 8 | +# |
| 9 | +# This script parses the DEPS file, extracts the fully qualified dependencies |
| 10 | +# and writes the to a file. This file will be later used to validate the dependencies |
| 11 | +# are pinned to a hash. |
| 12 | + |
| 13 | +import argparse |
| 14 | +import os |
| 15 | +import sys |
| 16 | + |
| 17 | +SCRIPT_DIR = os.path.dirname(sys.argv[0]) |
| 18 | +CHECKOUT_ROOT = os.path.realpath(os.path.join(SCRIPT_DIR, '..')) |
| 19 | + |
| 20 | + |
| 21 | +# Used in parsing the DEPS file. |
| 22 | +class VarImpl(object): |
| 23 | + _env_vars = { |
| 24 | + "host_cpu": "x64", |
| 25 | + "host_os": "linux", |
| 26 | + } |
| 27 | + |
| 28 | + def __init__(self, local_scope): |
| 29 | + self._local_scope = local_scope |
| 30 | + |
| 31 | + def Lookup(self, var_name): |
| 32 | + """Implements the Var syntax.""" |
| 33 | + if var_name in self._local_scope.get("vars", {}): |
| 34 | + return self._local_scope["vars"][var_name] |
| 35 | + # Inject default values for env variables |
| 36 | + if var_name in self._env_vars: |
| 37 | + return self._env_vars[var_name] |
| 38 | + raise Exception("Var is not defined: %s" % var_name) |
| 39 | + |
| 40 | + |
| 41 | +def ParseDepsFile(deps_file): |
| 42 | + local_scope = {} |
| 43 | + var = VarImpl(local_scope) |
| 44 | + global_scope = { |
| 45 | + 'Var': var.Lookup, |
| 46 | + 'deps_os': {}, |
| 47 | + } |
| 48 | + # Read the content. |
| 49 | + with open(deps_file, 'r') as fp: |
| 50 | + deps_content = fp.read() |
| 51 | + |
| 52 | + # Eval the content. |
| 53 | + exec (deps_content, global_scope, local_scope) |
| 54 | + |
| 55 | + # Extract the deps and filter. |
| 56 | + deps = local_scope.get('deps', {}) |
| 57 | + filtered_deps = [] |
| 58 | + for k, v in deps.items(): |
| 59 | + # We currently do not support packages or cipd which are represented |
| 60 | + # as dictionaries. |
| 61 | + if isinstance(v, str): |
| 62 | + filtered_deps.append(v) |
| 63 | + |
| 64 | + return filtered_deps |
| 65 | + |
| 66 | + |
| 67 | +def WriteManifest(deps, manifest_file): |
| 68 | + print('\n'.join(sorted(deps))) |
| 69 | + with open(manifest_file, 'w') as manifest: |
| 70 | + manifest.write('\n'.join(sorted(deps))) |
| 71 | + |
| 72 | + |
| 73 | +def ParseArgs(args): |
| 74 | + args = args[1:] |
| 75 | + parser = argparse.ArgumentParser( |
| 76 | + description='A script to flatten a gclient DEPS file.') |
| 77 | + |
| 78 | + parser.add_argument( |
| 79 | + '--deps', |
| 80 | + '-d', |
| 81 | + type=str, |
| 82 | + help='Input DEPS file.', |
| 83 | + default=os.path.join(CHECKOUT_ROOT, 'DEPS')) |
| 84 | + parser.add_argument( |
| 85 | + '--output', |
| 86 | + '-o', |
| 87 | + type=str, |
| 88 | + help='Output flattened deps file.', |
| 89 | + default=os.path.join(CHECKOUT_ROOT, 'deps_flatten.txt')) |
| 90 | + |
| 91 | + return parser.parse_args(args) |
| 92 | + |
| 93 | + |
| 94 | +def Main(argv): |
| 95 | + args = ParseArgs(argv) |
| 96 | + deps = ParseDepsFile(args.deps) |
| 97 | + WriteManifest(deps, args.output) |
| 98 | + return 0 |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + sys.exit(Main(sys.argv)) |
0 commit comments