OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """This script provides methods for clobbering build directories.""" | 6 """This script provides methods for clobbering build directories.""" |
7 | 7 |
8 import argparse | 8 import argparse |
9 import os | 9 import os |
10 import shutil | 10 import shutil |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 build_ninja_file = os.path.join(build_dir, 'build.ninja') | 59 build_ninja_file = os.path.join(build_dir, 'build.ninja') |
60 build_commands = extract_gn_build_commands(build_ninja_file) | 60 build_commands = extract_gn_build_commands(build_ninja_file) |
61 | 61 |
62 try: | 62 try: |
63 gn_args_file = os.path.join(build_dir, 'args.gn') | 63 gn_args_file = os.path.join(build_dir, 'args.gn') |
64 with open(gn_args_file, 'r') as f: | 64 with open(gn_args_file, 'r') as f: |
65 args_contents = f.read() | 65 args_contents = f.read() |
66 except IOError: | 66 except IOError: |
67 args_contents = '' | 67 args_contents = '' |
68 | 68 |
69 delete_dir(build_dir) | 69 e = None |
| 70 try: |
| 71 # delete_dir and os.mkdir() may fail, such as when chrome.exe is running, |
| 72 # and we still want to restore args.gn/build.ninja/build.ninja.d, so catch |
| 73 # the exception and rethrow it later. |
| 74 delete_dir(build_dir) |
| 75 os.mkdir(build_dir) |
| 76 except Exception as e: |
| 77 pass |
70 | 78 |
71 # Put back the args file (if any). | 79 # Put back the args file (if any). |
72 os.mkdir(build_dir) | |
73 if args_contents != '': | 80 if args_contents != '': |
74 with open(gn_args_file, 'w') as f: | 81 with open(gn_args_file, 'w') as f: |
75 f.write(args_contents) | 82 f.write(args_contents) |
76 | 83 |
77 # Write the build.ninja file sufficiently to regenerate itself. | 84 # Write the build.ninja file sufficiently to regenerate itself. |
78 with open(os.path.join(build_dir, 'build.ninja'), 'w') as f: | 85 with open(os.path.join(build_dir, 'build.ninja'), 'w') as f: |
79 if build_commands != '': | 86 if build_commands != '': |
80 f.write(build_commands) | 87 f.write(build_commands) |
81 else: | 88 else: |
82 # Couldn't parse the build.ninja file, write a default thing. | 89 # Couldn't parse the build.ninja file, write a default thing. |
83 f.write('''rule gn | 90 f.write('''rule gn |
84 command = gn -q gen //out/%s/ | 91 command = gn -q gen //out/%s/ |
85 description = Regenerating ninja files | 92 description = Regenerating ninja files |
86 | 93 |
87 build build.ninja: gn | 94 build build.ninja: gn |
88 generator = 1 | 95 generator = 1 |
89 depfile = build.ninja.d | 96 depfile = build.ninja.d |
90 ''' % (os.path.split(build_dir)[1])) | 97 ''' % (os.path.split(build_dir)[1])) |
91 | 98 |
92 # Write a .d file for the build which references a nonexistant file. This | 99 # Write a .d file for the build which references a nonexistant file. This |
93 # will make Ninja always mark the build as dirty. | 100 # will make Ninja always mark the build as dirty. |
94 with open(build_ninja_d_file, 'w') as f: | 101 with open(build_ninja_d_file, 'w') as f: |
95 f.write('build.ninja: nonexistant_file.gn\n') | 102 f.write('build.ninja: nonexistant_file.gn\n') |
96 | 103 |
| 104 if e: |
| 105 # Rethrow the exception we caught earlier. |
| 106 raise e |
97 | 107 |
98 def clobber(out_dir): | 108 def clobber(out_dir): |
99 """Clobber contents of build directory. | 109 """Clobber contents of build directory. |
100 | 110 |
101 Don't delete the directory itself: some checkouts have the build directory | 111 Don't delete the directory itself: some checkouts have the build directory |
102 mounted.""" | 112 mounted.""" |
103 for f in os.listdir(out_dir): | 113 for f in os.listdir(out_dir): |
104 path = os.path.join(out_dir, f) | 114 path = os.path.join(out_dir, f) |
105 if os.path.isfile(path): | 115 if os.path.isfile(path): |
106 os.unlink(path) | 116 os.unlink(path) |
107 elif os.path.isdir(path): | 117 elif os.path.isdir(path): |
108 delete_build_dir(path) | 118 delete_build_dir(path) |
109 | 119 |
110 | 120 |
111 def main(): | 121 def main(): |
112 parser = argparse.ArgumentParser() | 122 parser = argparse.ArgumentParser() |
113 parser.add_argument('out_dir', help='The output directory to clobber') | 123 parser.add_argument('out_dir', help='The output directory to clobber') |
114 args = parser.parse_args() | 124 args = parser.parse_args() |
115 clobber(args.out_dir) | 125 clobber(args.out_dir) |
116 return 0 | 126 return 0 |
117 | 127 |
118 | 128 |
119 if __name__ == '__main__': | 129 if __name__ == '__main__': |
120 sys.exit(main()) | 130 sys.exit(main()) |
OLD | NEW |