Flutter Engine
The Flutter Engine
copy_tree.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
6import argparse
7import gn_helpers
8import os
9import re
10import shutil
11import sys
12
13
14def ParseArgs(args):
15 args = args[1:]
16 parser = argparse.ArgumentParser(
17 description='A script to copy a file tree somewhere')
18
19 parser.add_argument('--depfile',
20 '-d',
21 type=str,
22 help='Path to a depfile to write when copying.')
23 parser.add_argument(
24 '--exclude_patterns',
25 '-e',
26 type=str,
27 help='Patterns to exclude [passed to shutil.copytree]')
28 parser.add_argument(
29 '--from', '-f', dest="copy_from", type=str, help='Source directory')
30 parser.add_argument(
31 '--stamp',
32 '-s',
33 type=str,
34 help='The path to a stamp file to output when finished.')
35 parser.add_argument('--to', '-t', type=str, help='Destination directory')
36
37 return parser.parse_args(args)
38
39
40def ValidateArgs(args):
41 if not args.copy_from or not os.path.isdir(args.copy_from):
42 print("--from argument must refer to a directory")
43 return False
44 if not args.to:
45 print("--to is required")
46 return False
47 return True
48
49
50# Returns a list of the files under 'src' that were copied.
51def CopyTree(src, dst, ignore=None):
52 copied_files = []
53
54 # Recursive helper method to collect errors but keep processing.
55 def copy_tree(src, dst, ignore, errors):
56 names = os.listdir(src)
57 if ignore is not None:
58 ignored_names = ignore(src, names)
59 else:
60 ignored_names = set()
61
62 if not os.path.exists(dst):
63 os.makedirs(dst)
64 for name in names:
65 if name in ignored_names:
66 continue
67 srcname = os.path.join(src, name)
68 dstname = os.path.join(dst, name)
69 try:
70 if os.path.isdir(srcname):
71 copy_tree(srcname, dstname, ignore, errors)
72 else:
73 copied_files.append(srcname)
74 shutil.copy(srcname, dstname)
75 except (IOError, os.error) as why:
76 errors.append((srcname, dstname, str(why)))
77 try:
78 shutil.copystat(src, dst)
79 except WindowsError:
80 # Can't copy file access times on Windows.
81 pass
82 except OSError as why:
83 errors.append((src, dst, str(why)))
84
85 # Format errors from file copies.
86 def format_error(error):
87 if len(error) == 1:
88 return "Error: {msg}".format(msg=str(error[0]))
89 return "From: {src}\nTo: {dst}\n{msg}" \
90 .format(src=error[0], dst=error[1], msg=error[2])
91
92 errors = []
93 copy_tree(src, dst, ignore, errors)
94 if errors:
95 failures = "\n\n".join(format_error(error) for error in errors)
96 parts = ("Some file copies failed:", "=" * 78, failures)
97 msg = '\n'.join(parts)
98 raise RuntimeError(msg)
99
100 return copied_files
101
102
103def WriteDepfile(depfile, stamp, dep_list):
104 os.makedirs(os.path.dirname(depfile), exist_ok=True)
105 # Paths in the depfile must be relative to the root build output directory,
106 # which is the cwd that ninja invokes the script from.
107 cwd = os.getcwd()
108 relstamp = os.path.relpath(stamp, cwd)
109 reldep_list = [os.path.relpath(d, cwd) for d in dep_list]
110 # Depfile paths must use an escape sequence for space characters.
111 reldep_list = [path.replace(" ", r"\ ") for path in reldep_list]
112 with open(depfile, 'w') as f:
113 print("{0}: {1}".format(relstamp, " ".join(reldep_list)), file=f)
114
115
116def Main(argv):
117 args = ParseArgs(argv)
118 if not ValidateArgs(args):
119 return -1
120
121 if args.exclude_patterns == None:
122 copied_files = CopyTree(args.copy_from, args.to)
123 else:
124 patterns = args.exclude_patterns.split(',')
125 copied_files = CopyTree(args.copy_from,
126 args.to,
127 ignore=shutil.ignore_patterns(*patterns))
128
129 if args.depfile and args.stamp:
130 WriteDepfile(args.depfile, args.stamp, copied_files)
131
132 if args.stamp:
133 open(args.stamp, 'w').close()
134
135 return 0
136
137
138if __name__ == '__main__':
139 sys.exit(Main(sys.argv))
uint32_t uint32_t * format
def CopyTree(src, dst, ignore=None)
Definition: copy_tree.py:51
def ParseArgs(args)
Definition: copy_tree.py:14
def Main(argv)
Definition: copy_tree.py:116
def WriteDepfile(depfile, stamp, dep_list)
Definition: copy_tree.py:103
def ValidateArgs(args)
Definition: copy_tree.py:40
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not set
Definition: switches.h:76
def print(*args, **kwargs)
Definition: run_tests.py:49
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741