Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: tools/git/move_source_file.py

Issue 935333002: Update from https://crrev.com/316786 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/git/for-all-touched-files.py ('k') | tools/idl_parser/idl_parser.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 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 """Moves C++ files to a new location, updating any include paths that point 6 """Moves C++ files to a new location, updating any include paths that point
7 to them, and re-ordering headers as needed. If multiple source files are 7 to them, and re-ordering headers as needed. If multiple source files are
8 specified, the destination must be a directory. Updates include guards in 8 specified, the destination must be a directory. Updates include guards in
9 moved header files. Assumes Chromium coding style. 9 moved header files. Assumes Chromium coding style.
10 10
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 # used in our code. 99 # used in our code.
100 # 100 #
101 # This work takes a bit of time. If this script starts feeling too 101 # This work takes a bit of time. If this script starts feeling too
102 # slow, one good way to speed it up is to make the comment handling 102 # slow, one good way to speed it up is to make the comment handling
103 # optional under a flag. 103 # optional under a flag.
104 mffr.MultiFileFindReplace( 104 mffr.MultiFileFindReplace(
105 r'(//.*)%s' % re.escape(from_path), 105 r'(//.*)%s' % re.escape(from_path),
106 r'\1%s' % to_path, 106 r'\1%s' % to_path,
107 ['*.cc', '*.h', '*.m', '*.mm', '*.cpp']) 107 ['*.cc', '*.h', '*.m', '*.mm', '*.cpp'])
108 108
109 # Update references in .gyp(i) files. 109 # Update references in GYP and BUILD.gn files.
110 def PathMinusFirstComponent(path):
111 """foo/bar/baz -> bar/baz"""
112 parts = re.split(r"[/\\]", path, 1)
113 if len(parts) == 2:
114 return parts[1]
115 else:
116 return parts[0]
117 mffr.MultiFileFindReplace(
118 r'([\'"])%s([\'"])' % re.escape(PathMinusFirstComponent(from_path)),
119 r'\1%s\2' % PathMinusFirstComponent(to_path),
120 ['*.gyp*'])
121
122 # Update references in BUILD.gn files.
123 # 110 #
124 # Unlike .gyp(i) files, BUILD.gn files can be placed in any directories, 111 # GYP files are mostly located under the first level directory (ex.
125 # and paths in a BUILD.gn file are relative to the directory where the 112 # chrome/chrome_browser.gypi), but sometimes they are located in
126 # BUILD.gn file is placed. 113 # directories at a deeper level (ex. extensions/shell/app_shell.gypi). On
114 # the other hand, BUILD.gn files can be placed in any directories.
115 #
116 # Paths in a GYP or BUILD.gn file are relative to the directory where the
117 # file is placed.
127 # 118 #
128 # For instance, "chrome/browser/chromeos/device_uma.h" is listed as 119 # For instance, "chrome/browser/chromeos/device_uma.h" is listed as
129 # "browser/chromeos/device_uma.h" in "chrome/chrome_browser_chromeos.gypi", 120 # "browser/chromeos/device_uma.h" in "chrome/chrome_browser_chromeos.gypi",
130 # but it's listed as "device_uma.h" in "chrome/browser/chromeos/BUILD.gn". 121 # but it's listed as "device_uma.h" in "chrome/browser/chromeos/BUILD.gn".
131 # 122 #
132 # To handle this, the code here will visit directories from the top level 123 # To handle this, the code here will visit directories from the top level
133 # src directory to the directory of |from_path| and try to update BUILD.gn 124 # src directory to the directory of |from_path| and try to update GYP and
134 # in each directory. 125 # BUILD.gn files in each directory.
135 # 126 #
136 # The code only handles files moved/renamed within the same BUILD.gn 127 # The code only handles files moved/renamed within the same build file. If
137 # file. If files are moved beyond the same BUILD.gn file, the affected 128 # files are moved beyond the same build file, the affected build files
138 # BUILD.gn files should be fixed manually. 129 # should be fixed manually.
139 def SplitByFirstComponent(path): 130 def SplitByFirstComponent(path):
140 """'foo/bar/baz' -> ('foo', 'bar/baz') 131 """'foo/bar/baz' -> ('foo', 'bar/baz')
141 'bar' -> ('bar', '') 132 'bar' -> ('bar', '')
142 '' -> ('', '') 133 '' -> ('', '')
143 """ 134 """
144 parts = re.split(r"[/\\]", path, 1) 135 parts = re.split(r"[/\\]", path, 1)
145 if len(parts) == 2: 136 if len(parts) == 2:
146 return (parts[0], parts[1]) 137 return (parts[0], parts[1])
147 else: 138 else:
148 return (parts[0], '') 139 return (parts[0], '')
149 140
150 visiting_directory = '' 141 visiting_directory = ''
151 from_rest = from_path 142 from_rest = from_path
152 to_rest = to_path 143 to_rest = to_path
153 while True: 144 while True:
154 mffr.MultiFileFindReplace( 145 mffr.MultiFileFindReplace(
155 r'([\'"])%s([\'"])' % from_rest, 146 r'([\'"])%s([\'"])' % from_rest,
156 r'\1%s\2' % to_rest, 147 r'\1%s\2' % to_rest,
157 [os.path.join(visiting_directory, 'BUILD.gn')]) 148 [os.path.join(visiting_directory, 'BUILD.gn'),
149 os.path.join(visiting_directory, '*.gyp*')])
158 from_first, from_rest = SplitByFirstComponent(from_rest) 150 from_first, from_rest = SplitByFirstComponent(from_rest)
159 to_first, to_rest = SplitByFirstComponent(to_rest) 151 to_first, to_rest = SplitByFirstComponent(to_rest)
160 visiting_directory = os.path.join(visiting_directory, from_first) 152 visiting_directory = os.path.join(visiting_directory, from_first)
161 if not from_rest or not to_rest: 153 if not from_rest or not to_rest:
162 break 154 break
163 155
164 156
165 def MakeIncludeGuardName(path_from_root): 157 def MakeIncludeGuardName(path_from_root):
166 """Returns an include guard name given a path from root.""" 158 """Returns an include guard name given a path from root."""
167 guard = path_from_root.replace('/', '_') 159 guard = path_from_root.replace('/', '_')
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 continue 223 continue
232 to_path = MakeDestinationPath(from_path, orig_to_path) 224 to_path = MakeDestinationPath(from_path, orig_to_path)
233 if not opts.already_moved: 225 if not opts.already_moved:
234 MoveFile(from_path, to_path) 226 MoveFile(from_path, to_path)
235 UpdatePostMove(from_path, to_path) 227 UpdatePostMove(from_path, to_path)
236 return 0 228 return 0
237 229
238 230
239 if __name__ == '__main__': 231 if __name__ == '__main__':
240 sys.exit(main()) 232 sys.exit(main())
OLDNEW
« no previous file with comments | « tools/git/for-all-touched-files.py ('k') | tools/idl_parser/idl_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698