Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
copy_tree Namespace Reference

Functions

 ParseArgs (args)
 
 ValidateArgs (args)
 
 CopyTree (src, dst, ignore=None)
 
 WriteDepfile (depfile, stamp, dep_list)
 
 Main (argv)
 

Function Documentation

◆ CopyTree()

copy_tree.CopyTree (   src,
  dst,
  ignore = None 
)

Definition at line 51 of file copy_tree.py.

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
uint32_t uint32_t * format

◆ Main()

copy_tree.Main (   argv)

Definition at line 116 of file copy_tree.py.

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

◆ ParseArgs()

copy_tree.ParseArgs (   args)

Definition at line 14 of file copy_tree.py.

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

◆ ValidateArgs()

copy_tree.ValidateArgs (   args)

Definition at line 40 of file copy_tree.py.

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.
void print(void *str)
Definition bridge.cpp:126

◆ WriteDepfile()

copy_tree.WriteDepfile (   depfile,
  stamp,
  dep_list 
)

Definition at line 103 of file copy_tree.py.

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