7"""Utility for dart_pkg and dart_pkg_app rules"""
17USE_LINKS = sys.platform !=
'win32'
19DART_ANALYZE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'dart_analyze.py')
23 if os.path.isdir(path):
25 _, ext = os.path.splitext(path)
31 abspath = os.path.abspath(path)
32 if not os.path.exists(abspath):
38 _, filename = os.path.split(path)
39 if filename ==
'pubspec.yaml':
44def link(from_root, to_root):
48 except OSError
as err:
49 if err.errno == errno.ENOENT:
53 os.symlink(from_root, to_root)
54 except OSError
as err:
55 if err.errno == errno.EEXIST:
59def copy(from_root, to_root, filter_func=None):
60 if not os.path.exists(from_root):
62 if os.path.isfile(from_root):
64 shutil.copy(from_root, to_root)
69 for root, dirs, files
in os.walk(from_root):
73 wrapped_filter =
lambda name, rt=root: filter_func(os.path.join(rt, name))
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):
82 shutil.copy(from_path, to_path)
84 dirs[:] = list(filter(wrapped_filter, dirs))
89 link(from_root, to_root)
91 copy(from_root, to_root, filter_func)
96 link(from_root, to_root)
102 except OSError
as err:
103 if err.errno != errno.ENOENT:
109 for root, dirs, files
in os.walk(from_root):
111 wrapped_filter =
None
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))
125 link_path = os.readlink(path)
126 except OSError
as err:
128 if err.errno == errno.EINVAL:
131 if not os.path.exists(link_path):
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)
145 cmd = [
'python', DART_ANALYZE]
146 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')
154 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
155 except subprocess.CalledProcessError
as err:
156 print(
'Failed analyzing %s' % entrypoints)
158 return err.returncode
163 parser = argparse.ArgumentParser(description=
'Generate a dart-pkg')
165 '--dart-sdk', action=
'store', metavar=
'dart_sdk', help=
'Path to the Dart SDK.'
170 metavar=
'package_name',
171 help=
'Name of package',
176 metavar=
'pkg_directory',
177 help=
'Directory where dart_pkg should go',
181 '--package-root', metavar=
'package_root', help=
'packages/ directory', required=
True
183 parser.add_argument(
'--stamp-file', metavar=
'stamp_file', help=
'timestamp file', required=
True)
185 '--entries-file', metavar=
'entries_file', help=
'script entries file', required=
True
188 '--package-sources', metavar=
'package_sources', help=
'Package sources', nargs=
'+'
191 '--package-entrypoints',
192 metavar=
'package_entrypoints',
193 help=
'Package entry points for analyzer',
198 '--sdk-ext-directories',
199 metavar=
'sdk_ext_directories',
200 help=
'Directory containing .dart sources',
206 metavar=
'sdk_ext_files',
207 help=
'List of .dart files that are part of sdk_ext.',
212 '--sdk-ext-mappings',
213 metavar=
'sdk_ext_mappings',
214 help=
'Mappings for SDK extension libraries.',
222 help=
'Package is a read only package.',
225 args = parser.parse_args()
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')
236 for mapping
in args.sdk_ext_mappings:
237 library, path = mapping.split(
',', 1)
238 mappings[library] =
'../sdk_ext/%s' % path
240 sdkext_path = os.path.join(lib_path,
'_sdkext')
242 with open(sdkext_path,
'w')
as stream:
243 json.dump(mappings, stream, sort_keys=
True, indent=2, separators=(
',',
': '))
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)
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)
259 entrypoint_targets.append(target)
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)
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)
278 package_path = os.path.join(args.package_root, args.package_name)
290 with open(args.entries_file,
'w')
as file:
291 for entrypoint
in entrypoint_targets:
292 file.write(entrypoint +
'\n')
295 with open(args.stamp_file,
'w'):
301if __name__ ==
'__main__':
def analyze_entrypoints(dart_sdk, package_root, entrypoints)
def ensure_dir_exists(path)
def copy(from_root, to_root, filter_func=None)
def remove_if_exists(path)
def copy_or_link(from_root, to_root, filter_func=None)
def has_pubspec_yaml(paths)
def link_if_possible(from_root, to_root)
def remove_broken_symlink(path)
def link(from_root, to_root)
def list_files(from_root, filter_func=None)
def remove_broken_symlinks(root_dir)
def print(*args, **kwargs)