def UpdatePostMove(from_path, to_path):
    """Given a file that has moved from |from_path| to |to_path|,
  updates the moved file's include guard to match the new path and
  updates all references to the file in other source files. Also tries
  to update references in .gyp(i) files using a heuristic.
  """
    # Include paths always use forward slashes.
    from_path = from_path.replace('\\', '/')
    to_path = to_path.replace('\\', '/')

    if os.path.splitext(from_path)[1] in ['.h', '.hh']:
        UpdateIncludeGuard(from_path, to_path)

        # Update include/import references.
        files_with_changed_includes = mffr.MultiFileFindReplace(
            r'(#(include|import)\s*["<])%s([>"])' % re.escape(from_path),
            r'\1%s\3' % to_path, ['*.cc', '*.h', '*.m', '*.mm'])

        # Reorder headers in files that changed.
        for changed_file in files_with_changed_includes:

            def AlwaysConfirm(a, b):
                return True

            sort_headers.FixFileWithConfirmFunction(changed_file,
                                                    AlwaysConfirm)

    # Update comments; only supports // comments, which are primarily
    # used in our code.
    #
    # This work takes a bit of time. If this script starts feeling too
    # slow, one good way to speed it up is to make the comment handling
    # optional under a flag.
    mffr.MultiFileFindReplace(r'(//.*)%s' % re.escape(from_path),
                              r'\1%s' % to_path,
                              ['*.cc', '*.h', '*.m', '*.mm'])

    # Update references in .gyp(i) files.
    def PathMinusFirstComponent(path):
        """foo/bar/baz -> bar/baz"""
        parts = re.split(r"[/\\]", path, 1)
        if len(parts) == 2:
            return parts[1]
        else:
            return parts[0]

    mffr.MultiFileFindReplace(
        r'([\'"])%s([\'"])' % re.escape(PathMinusFirstComponent(from_path)),
        r'\1%s\2' % PathMinusFirstComponent(to_path), ['*.gyp*'])
Exemple #2
0
def UpdateIncludes(from_path, to_path, in_blink):
    """Updates any includes of |from_path| to |to_path|. Paths supplied to this
  function have been mapped to forward slashes.
  """
    from_include_path = from_path
    to_include_path = to_path
    if in_blink:
        from_include_path = UpdateIncludePathForBlink(from_include_path)
        to_include_path = UpdateIncludePathForBlink(to_include_path)

    # This handles three types of include/imports:
    # . C++ includes.
    # . Object-C imports
    # . Imports in mojom files.
    files_with_changed_includes = mffr.MultiFileFindReplace(
        r'(#?(include|import)\s*["<])%s([>"]);?' %
        re.escape(from_include_path), r'\1%s\3' % to_include_path,
        ['*.cc', '*.h', '*.m', '*.mm', '*.cpp', '*.mojom'])

    # Reorder headers in files that changed.
    for changed_file in files_with_changed_includes:

        def AlwaysConfirm(a, b):
            return True

        sort_headers.FixFileWithConfirmFunction(changed_file, AlwaysConfirm,
                                                True, in_blink)
Exemple #3
0
def UpdateIncludes(from_path, to_path):
    """Updates any includes of |from_path| to |to_path|. Paths supplied to this
  function have been mapped to forward slashes.
  """
    # This handles three types of include/imports:
    # . C++ includes.
    # . Object-C imports
    # . Imports in mojom files.
    files_with_changed_includes = mffr.MultiFileFindReplace(
        r'(#?(include|import)\s*["<])%s([>"]);?' % re.escape(from_path),
        r'\1%s\3' % to_path,
        ['*.cc', '*.h', '*.m', '*.mm', '*.cpp', '*.mojom'])
Exemple #4
0
def UpdatePostMove(from_path, to_path):
    """Given a file that has moved from |from_path| to |to_path|,
  updates the moved file's include guard to match the new path and
  updates all references to the file in other source files. Also tries
  to update references in .gyp(i) files using a heuristic.
  """
    # Include paths always use forward slashes.
    from_path = from_path.replace('\\', '/')
    to_path = to_path.replace('\\', '/')
    extension = os.path.splitext(from_path)[1]

    if extension in ['.h', '.hh', '.mojom']:
        UpdateIncludes(from_path, to_path)
        if extension == '.mojom':
            # For mojom files, update includes of generated headers.
            UpdateIncludes(from_path + '.h', to_path + '.h')
            UpdateIncludes(from_path + '-blink.h', to_path + '-blink.h')
            UpdateIncludes(from_path + '-shared.h', to_path + '-shared.h')
            UpdateIncludes(from_path + '-forward.h', to_path + '-forward.h')
        else:
            UpdateIncludeGuard(from_path, to_path)

    # Update comments; only supports // comments, which are primarily
    # used in our code.
    #
    # This work takes a bit of time. If this script starts feeling too
    # slow, one good way to speed it up is to make the comment handling
    # optional under a flag.
    mffr.MultiFileFindReplace(r'(//.*)%s' % re.escape(from_path),
                              r'\1%s' % to_path,
                              ['*.cc', '*.h', '*.m', '*.mm', '*.cpp'])

    # Update references in GYP and BUILD.gn files.
    #
    # GYP files are mostly located under the first level directory (ex.
    # chrome/chrome_browser.gypi), but sometimes they are located in
    # directories at a deeper level (ex. extensions/shell/app_shell.gypi). On
    # the other hand, BUILD.gn files can be placed in any directories.
    #
    # Paths in a GYP or BUILD.gn file are relative to the directory where the
    # file is placed.
    #
    # For instance, "chrome/browser/chromeos/device_uma.h" is listed as
    # "browser/chromeos/device_uma.h" in "chrome/chrome_browser_chromeos.gypi",
    # but it's listed as "device_uma.h" in "chrome/browser/chromeos/BUILD.gn".
    #
    # To handle this, the code here will visit directories from the top level
    # src directory to the directory of |from_path| and try to update GYP and
    # BUILD.gn files in each directory.
    #
    # The code only handles files moved/renamed within the same build file. If
    # files are moved beyond the same build file, the affected build files
    # should be fixed manually.
    def SplitByFirstComponent(path):
        """'foo/bar/baz' -> ('foo', 'bar/baz')
       'bar' -> ('bar', '')
       '' -> ('', '')
    """
        parts = re.split(r"[/\\]", path, 1)
        if len(parts) == 2:
            return (parts[0], parts[1])
        else:
            return (parts[0], '')

    visiting_directory = ''
    from_rest = from_path
    to_rest = to_path
    while True:
        files_with_changed_sources = mffr.MultiFileFindReplace(
            r'([\'"])%s([\'"])' % from_rest, r'\1%s\2' % to_rest, [
                os.path.join(visiting_directory, 'BUILD.gn'),
                os.path.join(visiting_directory, '*.gyp*')
            ])
        for changed_file in files_with_changed_sources:
            sort_sources.ProcessFile(changed_file, should_confirm=False)
        from_first, from_rest = SplitByFirstComponent(from_rest)
        to_first, to_rest = SplitByFirstComponent(to_rest)
        visiting_directory = os.path.join(visiting_directory, from_first)
        if not from_rest or not to_rest or from_rest == to_rest:
            break
Exemple #5
0
def UpdatePostMove(from_path, to_path):
    """Given a file that has moved from |from_path| to |to_path|,
  updates the moved file's include guard to match the new path and
  updates all references to the file in other source files. Also tries
  to update references in .gyp(i) files using a heuristic.
  """
    # Include paths always use forward slashes.
    from_path = from_path.replace('\\', '/')
    to_path = to_path.replace('\\', '/')

    if os.path.splitext(from_path)[1] in ['.h', '.hh']:
        UpdateIncludeGuard(from_path, to_path)

        # Update include/import references.
        files_with_changed_includes = mffr.MultiFileFindReplace(
            r'(#(include|import)\s*["<])%s([>"])' % re.escape(from_path),
            r'\1%s\3' % to_path, ['*.cc', '*.h', '*.m', '*.mm', '*.cpp'])

        # Reorder headers in files that changed.
        for changed_file in files_with_changed_includes:

            def AlwaysConfirm(a, b):
                return True

            sort_headers.FixFileWithConfirmFunction(changed_file,
                                                    AlwaysConfirm, True)

    # Update comments; only supports // comments, which are primarily
    # used in our code.
    #
    # This work takes a bit of time. If this script starts feeling too
    # slow, one good way to speed it up is to make the comment handling
    # optional under a flag.
    mffr.MultiFileFindReplace(r'(//.*)%s' % re.escape(from_path),
                              r'\1%s' % to_path,
                              ['*.cc', '*.h', '*.m', '*.mm', '*.cpp'])

    # Update references in .gyp(i) files.
    def PathMinusFirstComponent(path):
        """foo/bar/baz -> bar/baz"""
        parts = re.split(r"[/\\]", path, 1)
        if len(parts) == 2:
            return parts[1]
        else:
            return parts[0]

    mffr.MultiFileFindReplace(
        r'([\'"])%s([\'"])' % re.escape(PathMinusFirstComponent(from_path)),
        r'\1%s\2' % PathMinusFirstComponent(to_path), ['*.gyp*'])

    # Update references in BUILD.gn files.
    #
    # Unlike .gyp(i) files, BUILD.gn files can be placed in any directories,
    # and paths in a BUILD.gn file are relative to the directory where the
    # BUILD.gn file is placed.
    #
    # For instance, "chrome/browser/chromeos/device_uma.h" is listed as
    # "browser/chromeos/device_uma.h" in "chrome/chrome_browser_chromeos.gypi",
    # but it's listed as "device_uma.h" in "chrome/browser/chromeos/BUILD.gn".
    #
    # To handle this, the code here will visit directories from the top level
    # src directory to the directory of |from_path| and try to update BUILD.gn
    # in each directory.
    #
    # The code only handles files moved/renamed within the same BUILD.gn
    # file. If files are moved beyond the same BUILD.gn file, the affected
    # BUILD.gn files should be fixed manually.
    def SplitByFirstComponent(path):
        """'foo/bar/baz' -> ('foo', 'bar/baz')
       'bar' -> ('bar', '')
       '' -> ('', '')
    """
        parts = re.split(r"[/\\]", path, 1)
        if len(parts) == 2:
            return (parts[0], parts[1])
        else:
            return (parts[0], '')

    visiting_directory = ''
    from_rest = from_path
    to_rest = to_path
    while True:
        mffr.MultiFileFindReplace(
            r'([\'"])%s([\'"])' % from_rest, r'\1%s\2' % to_rest,
            [os.path.join(visiting_directory, 'BUILD.gn')])
        from_first, from_rest = SplitByFirstComponent(from_rest)
        to_first, to_rest = SplitByFirstComponent(to_rest)
        visiting_directory = os.path.join(visiting_directory, from_first)
        if not from_rest or not to_rest:
            break