Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dart_pkg.py
Go to the documentation of this file.
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"""Utility for dart_pkg and dart_pkg_app rules"""
8
9import argparse
10import errno
11import json
12import os
13import shutil
14import subprocess
15import sys
16
17USE_LINKS = sys.platform != 'win32'
18
19DART_ANALYZE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dart_analyze.py')
20
21
22def dart_filter(path):
23 if os.path.isdir(path):
24 return True
25 _, ext = os.path.splitext(path)
26 # .dart includes '.mojom.dart'
27 return ext == '.dart'
28
29
31 abspath = os.path.abspath(path)
32 if not os.path.exists(abspath):
33 os.makedirs(abspath)
34
35
37 for path in paths:
38 _, filename = os.path.split(path)
39 if filename == 'pubspec.yaml':
40 return True
41 return False
42
43
44def link(from_root, to_root):
45 ensure_dir_exists(os.path.dirname(to_root))
46 try:
47 os.unlink(to_root)
48 except OSError as err:
49 if err.errno == errno.ENOENT:
50 pass
51
52 try:
53 os.symlink(from_root, to_root)
54 except OSError as err:
55 if err.errno == errno.EEXIST:
56 pass
57
58
59def copy(from_root, to_root, filter_func=None):
60 if not os.path.exists(from_root):
61 return
62 if os.path.isfile(from_root):
63 ensure_dir_exists(os.path.dirname(to_root))
64 shutil.copy(from_root, to_root)
65 return
66
67 ensure_dir_exists(to_root)
68
69 for root, dirs, files in os.walk(from_root):
70 # filter_func expects paths not names, so wrap it to make them absolute.
71 wrapped_filter = None
72 if filter_func:
73 wrapped_filter = lambda name, rt=root: filter_func(os.path.join(rt, name))
74
75 for name in filter(wrapped_filter, files):
76 from_path = os.path.join(root, name)
77 root_rel_path = os.path.relpath(from_path, from_root)
78 to_path = os.path.join(to_root, root_rel_path)
79 to_dir = os.path.dirname(to_path)
80 if not os.path.exists(to_dir):
81 os.makedirs(to_dir)
82 shutil.copy(from_path, to_path)
83
84 dirs[:] = list(filter(wrapped_filter, dirs))
85
86
87def copy_or_link(from_root, to_root, filter_func=None):
88 if USE_LINKS:
89 link(from_root, to_root)
90 else:
91 copy(from_root, to_root, filter_func)
92
93
94def link_if_possible(from_root, to_root):
95 if USE_LINKS:
96 link(from_root, to_root)
97
98
100 try:
101 os.remove(path)
102 except OSError as err:
103 if err.errno != errno.ENOENT:
104 raise
105
106
107def list_files(from_root, filter_func=None):
108 file_list = []
109 for root, dirs, files in os.walk(from_root):
110 # filter_func expects paths not names, so wrap it to make them absolute.
111 wrapped_filter = None
112 if filter_func:
113 wrapped_filter = lambda name, rt=root: filter_func(os.path.join(rt, name))
114 for name in filter(wrapped_filter, files):
115 path = os.path.join(root, name)
116 file_list.append(path)
117 dirs[:] = list(filter(wrapped_filter, dirs))
118 return file_list
119
120
122 if not USE_LINKS:
123 return
124 try:
125 link_path = os.readlink(path)
126 except OSError as err:
127 # Path was not a symlink.
128 if err.errno == errno.EINVAL:
129 pass
130 else:
131 if not os.path.exists(link_path):
132 remove_if_exists(path)
133
134
136 if not USE_LINKS:
137 return
138 for current_dir, _, child_files in os.walk(root_dir):
139 for filename in child_files:
140 path = os.path.join(current_dir, filename)
142
143
144def analyze_entrypoints(dart_sdk, package_root, entrypoints):
145 cmd = ['python', DART_ANALYZE]
146 cmd.append('--dart-sdk')
147 cmd.append(dart_sdk)
148 cmd.append('--entrypoints')
149 cmd.extend(entrypoints)
150 cmd.append('--package-root')
151 cmd.append(package_root)
152 cmd.append('--no-hints')
153 try:
154 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
155 except subprocess.CalledProcessError as err:
156 print('Failed analyzing %s' % entrypoints)
157 print(err.output)
158 return err.returncode
159 return 0
160
161
162def main():
163 parser = argparse.ArgumentParser(description='Generate a dart-pkg')
164 parser.add_argument(
165 '--dart-sdk', action='store', metavar='dart_sdk', help='Path to the Dart SDK.'
166 )
167 parser.add_argument(
168 '--package-name',
169 action='store',
170 metavar='package_name',
171 help='Name of package',
172 required=True
173 )
174 parser.add_argument(
175 '--pkg-directory',
176 metavar='pkg_directory',
177 help='Directory where dart_pkg should go',
178 required=True
179 )
180 parser.add_argument(
181 '--package-root', metavar='package_root', help='packages/ directory', required=True
182 )
183 parser.add_argument('--stamp-file', metavar='stamp_file', help='timestamp file', required=True)
184 parser.add_argument(
185 '--entries-file', metavar='entries_file', help='script entries file', required=True
186 )
187 parser.add_argument(
188 '--package-sources', metavar='package_sources', help='Package sources', nargs='+'
189 )
190 parser.add_argument(
191 '--package-entrypoints',
192 metavar='package_entrypoints',
193 help='Package entry points for analyzer',
194 nargs='*',
195 default=[]
196 )
197 parser.add_argument(
198 '--sdk-ext-directories',
199 metavar='sdk_ext_directories',
200 help='Directory containing .dart sources',
201 nargs='*',
202 default=[]
203 )
204 parser.add_argument(
205 '--sdk-ext-files',
206 metavar='sdk_ext_files',
207 help='List of .dart files that are part of sdk_ext.',
208 nargs='*',
209 default=[]
210 )
211 parser.add_argument(
212 '--sdk-ext-mappings',
213 metavar='sdk_ext_mappings',
214 help='Mappings for SDK extension libraries.',
215 nargs='*',
216 default=[]
217 )
218 parser.add_argument(
219 '--read_only',
220 action='store_true',
221 dest='read_only',
222 help='Package is a read only package.',
223 default=False
224 )
225 args = parser.parse_args()
226
227 # We must have a pubspec.yaml.
228 assert has_pubspec_yaml(args.package_sources)
229
230 target_dir = os.path.join(args.pkg_directory, args.package_name)
231 target_packages_dir = os.path.join(target_dir, 'packages')
232 lib_path = os.path.join(target_dir, 'lib')
233 ensure_dir_exists(lib_path)
234
235 mappings = {}
236 for mapping in args.sdk_ext_mappings:
237 library, path = mapping.split(',', 1)
238 mappings[library] = '../sdk_ext/%s' % path
239
240 sdkext_path = os.path.join(lib_path, '_sdkext')
241 if mappings:
242 with open(sdkext_path, 'w') as stream:
243 json.dump(mappings, stream, sort_keys=True, indent=2, separators=(',', ': '))
244 else:
245 remove_if_exists(sdkext_path)
246
247 # Copy or symlink package sources into pkg directory.
248 common_source_prefix = os.path.dirname(os.path.commonprefix(args.package_sources))
249 for source in args.package_sources:
250 relative_source = os.path.relpath(source, common_source_prefix)
251 target = os.path.join(target_dir, relative_source)
252 copy_or_link(source, target)
253
254 entrypoint_targets = []
255 for source in args.package_entrypoints:
256 relative_source = os.path.relpath(source, common_source_prefix)
257 target = os.path.join(target_dir, relative_source)
258 copy_or_link(source, target)
259 entrypoint_targets.append(target)
260
261 # Copy sdk-ext sources into pkg directory
262 sdk_ext_dir = os.path.join(target_dir, 'sdk_ext')
263 for directory in args.sdk_ext_directories:
264 sdk_ext_sources = list_files(directory, dart_filter)
265 common_prefix = os.path.commonprefix(sdk_ext_sources)
266 for source in sdk_ext_sources:
267 relative_source = os.path.relpath(source, common_prefix)
268 target = os.path.join(sdk_ext_dir, relative_source)
269 copy_or_link(source, target)
270
271 common_source_prefix = os.path.dirname(os.path.commonprefix(args.sdk_ext_files))
272 for source in args.sdk_ext_files:
273 relative_source = os.path.relpath(source, common_source_prefix)
274 target = os.path.join(sdk_ext_dir, relative_source)
275 copy_or_link(source, target)
276
277 # Symlink packages/
278 package_path = os.path.join(args.package_root, args.package_name)
279 copy_or_link(lib_path, package_path)
280
281 # Link dart-pkg/$package/packages to dart-pkg/packages
282 link_if_possible(args.package_root, target_packages_dir)
283
284 # Remove any broken symlinks in target_dir and package root.
285 remove_broken_symlinks(target_dir)
286 remove_broken_symlinks(args.package_root)
287
288 # If any entrypoints are defined, write them to disk so that the analyzer
289 # test can find them.
290 with open(args.entries_file, 'w') as file:
291 for entrypoint in entrypoint_targets:
292 file.write(entrypoint + '\n')
293
294 # Write stamp file.
295 with open(args.stamp_file, 'w'):
296 pass
297
298 return 0
299
300
301if __name__ == '__main__':
302 sys.exit(main())
void print(void *str)
Definition bridge.cpp:126
Definition copy.py:1
list_files(from_root, filter_func=None)
Definition dart_pkg.py:107
has_pubspec_yaml(paths)
Definition dart_pkg.py:36
remove_broken_symlinks(root_dir)
Definition dart_pkg.py:135
analyze_entrypoints(dart_sdk, package_root, entrypoints)
Definition dart_pkg.py:144
ensure_dir_exists(path)
Definition dart_pkg.py:30
link(from_root, to_root)
Definition dart_pkg.py:44
copy_or_link(from_root, to_root, filter_func=None)
Definition dart_pkg.py:87
link_if_possible(from_root, to_root)
Definition dart_pkg.py:94
remove_broken_symlink(path)
Definition dart_pkg.py:121
dart_filter(path)
Definition dart_pkg.py:22
remove_if_exists(path)
Definition dart_pkg.py:99
Definition main.py:1