OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # Copyright 2015 The Chromium 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 """Utility for dart_pkg and dart_pkg_app rules""" |
| 8 |
| 9 import ast |
| 10 import argparse |
| 11 import errno |
| 12 import os |
| 13 import shutil |
| 14 import sys |
| 15 |
| 16 # Disable lint check for finding modules: |
| 17 # pylint: disable=F0401 |
| 18 |
| 19 sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 20 "bindings/pylib")) |
| 21 |
| 22 from mojom.parse.parser import Parse |
| 23 from mojom.parse.translate import Translate |
| 24 |
| 25 USE_LINKS = sys.platform != "win32" |
| 26 |
| 27 def mojom_dart_filter(path): |
| 28 if os.path.isdir(path): |
| 29 return True |
| 30 # Don't include all .dart, just .mojom.dart. |
| 31 return path.endswith('.mojom.dart') |
| 32 |
| 33 |
| 34 def dart_filter(path): |
| 35 if os.path.isdir(path): |
| 36 return True |
| 37 _, ext = os.path.splitext(path) |
| 38 # .dart includes '.mojom.dart' |
| 39 return ext == '.dart' |
| 40 |
| 41 |
| 42 def mojom_filter(path): |
| 43 if os.path.isdir(path): |
| 44 return True |
| 45 _, ext = os.path.splitext(path) |
| 46 return ext == '.mojom' |
| 47 |
| 48 |
| 49 def ensure_dir_exists(path): |
| 50 if not os.path.exists(path): |
| 51 os.makedirs(path) |
| 52 |
| 53 |
| 54 def has_pubspec_yaml(paths): |
| 55 for path in paths: |
| 56 _, filename = os.path.split(path) |
| 57 if 'pubspec.yaml' == filename: |
| 58 return True |
| 59 return False |
| 60 |
| 61 |
| 62 def link(from_root, to_root, filter_func=None): |
| 63 ensure_dir_exists(os.path.dirname(to_root)) |
| 64 if os.path.exists(to_root): |
| 65 os.unlink(to_root) |
| 66 try: |
| 67 os.symlink(from_root, to_root) |
| 68 except OSError as e: |
| 69 if e.errno == errno.EEXIST: |
| 70 pass |
| 71 |
| 72 |
| 73 def copy(from_root, to_root, filter_func=None): |
| 74 if not os.path.exists(from_root): |
| 75 return |
| 76 if os.path.isfile(from_root): |
| 77 ensure_dir_exists(os.path.dirname(to_root)) |
| 78 shutil.copy(from_root, to_root) |
| 79 return |
| 80 |
| 81 ensure_dir_exists(to_root) |
| 82 |
| 83 for root, dirs, files in os.walk(from_root): |
| 84 # filter_func expects paths not names, so wrap it to make them absolute. |
| 85 wrapped_filter = None |
| 86 if filter_func: |
| 87 wrapped_filter = lambda name: filter_func(os.path.join(root, name)) |
| 88 |
| 89 for name in filter(wrapped_filter, files): |
| 90 from_path = os.path.join(root, name) |
| 91 root_rel_path = os.path.relpath(from_path, from_root) |
| 92 to_path = os.path.join(to_root, root_rel_path) |
| 93 to_dir = os.path.dirname(to_path) |
| 94 if not os.path.exists(to_dir): |
| 95 os.makedirs(to_dir) |
| 96 shutil.copy(from_path, to_path) |
| 97 |
| 98 dirs[:] = filter(wrapped_filter, dirs) |
| 99 |
| 100 |
| 101 def copy_or_link(from_root, to_root, filter_func=None): |
| 102 if USE_LINKS: |
| 103 link(from_root, to_root, filter_func) |
| 104 else: |
| 105 copy(from_root, to_root, filter_func) |
| 106 |
| 107 |
| 108 def mojom_path(filename): |
| 109 with open(filename) as f: |
| 110 source = f.read() |
| 111 tree = Parse(source, filename) |
| 112 _, name = os.path.split(filename) |
| 113 mojom = Translate(tree, name) |
| 114 elements = mojom['namespace'].split('.') |
| 115 elements.append("%s" % mojom['name']) |
| 116 return os.path.join(*elements) |
| 117 |
| 118 |
| 119 def main(): |
| 120 parser = argparse.ArgumentParser(description='Generate a dart-pkg') |
| 121 parser.add_argument('--package-name', |
| 122 action='store', |
| 123 type=str, |
| 124 metavar='package_name', |
| 125 help='Name of package', |
| 126 required=True) |
| 127 parser.add_argument('--gen-directory', |
| 128 metavar='gen_directory', |
| 129 help="dart-gen directory", |
| 130 required=True) |
| 131 parser.add_argument('--pkg-directory', |
| 132 metavar='pkg_directory', |
| 133 help='Directory where dart_pkg should go', |
| 134 required=True) |
| 135 parser.add_argument('--package-root', |
| 136 metavar='package_root', |
| 137 help='packages/ directory', |
| 138 required=True) |
| 139 parser.add_argument('--stamp-file', |
| 140 metavar='stamp_file', |
| 141 help='timestamp file', |
| 142 required=True) |
| 143 parser.add_argument('--package-sources', |
| 144 metavar='package_sources', |
| 145 help='Package sources', |
| 146 nargs='+') |
| 147 parser.add_argument('--mojom-sources', |
| 148 metavar='mojom_sources', |
| 149 help='.mojom and .mojom.dart sources', |
| 150 nargs='*', |
| 151 default=[]) |
| 152 args = parser.parse_args() |
| 153 |
| 154 # We must have a pubspec.yaml. |
| 155 assert has_pubspec_yaml(args.package_sources) |
| 156 |
| 157 # Copy or symlink package sources into pkg directory. |
| 158 target_dir = os.path.join(args.pkg_directory, args.package_name) |
| 159 common_source_prefix = os.path.commonprefix(args.package_sources) |
| 160 for source in args.package_sources: |
| 161 relative_source = os.path.relpath(source, common_source_prefix) |
| 162 target = os.path.join(target_dir, relative_source) |
| 163 copy_or_link(source, target) |
| 164 |
| 165 lib_path = os.path.join(target_dir, "lib") |
| 166 lib_mojom_path = os.path.join(lib_path, "mojom") |
| 167 |
| 168 # Copy mojom sources |
| 169 for mojom_source_path in args.mojom_sources: |
| 170 path = mojom_path(mojom_source_path) |
| 171 copy(mojom_source_path, os.path.join(lib_mojom_path, path)) |
| 172 |
| 173 # Copy generated mojom.dart files. |
| 174 generated_mojom_lib_path = os.path.join(args.gen_directory, "mojom/lib") |
| 175 for mojom_source_path in args.mojom_sources: |
| 176 path = mojom_path(mojom_source_path) |
| 177 source_path = '%s.dart' % os.path.join(generated_mojom_lib_path, path) |
| 178 target_path = '%s.dart' % os.path.join(lib_mojom_path, path) |
| 179 copy(source_path, target_path) |
| 180 |
| 181 # Symlink packages/ |
| 182 package_path = os.path.join(args.package_root, args.package_name) |
| 183 link(lib_path, package_path) |
| 184 |
| 185 # Write stamp file. |
| 186 with open(args.stamp_file, 'w') as f: |
| 187 pass |
| 188 |
| 189 if __name__ == '__main__': |
| 190 sys.exit(main()) |
OLD | NEW |