WebKitTools/Scripts/webkitpy/style/main.py
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
       
     2 #
       
     3 # Redistribution and use in source and binary forms, with or without
       
     4 # modification, are permitted provided that the following conditions
       
     5 # are met:
       
     6 # 1.  Redistributions of source code must retain the above copyright
       
     7 #     notice, this list of conditions and the following disclaimer.
       
     8 # 2.  Redistributions in binary form must reproduce the above copyright
       
     9 #     notice, this list of conditions and the following disclaimer in the
       
    10 #     documentation and/or other materials provided with the distribution.
       
    11 #
       
    12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
       
    13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
       
    15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
       
    16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
       
    17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
       
    18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
       
    19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
       
    20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    22 
       
    23 import logging
       
    24 import os
       
    25 import sys
       
    26 
       
    27 from webkitpy.common.system.ospath import relpath as _relpath
       
    28 
       
    29 
       
    30 _log = logging.getLogger(__name__)
       
    31 
       
    32 
       
    33 def change_directory(checkout_root, paths, mock_os=None):
       
    34     """Change the working directory to the WebKit checkout root, if possible.
       
    35 
       
    36     If every path in the paths parameter is below the checkout root (or if
       
    37     the paths parameter is empty or None), this method changes the current
       
    38     working directory to the checkout root and converts the paths parameter
       
    39     as described below.
       
    40         This allows the paths being checked to be displayed relative to the
       
    41     checkout root, and for path-specific style checks to work as expected.
       
    42     Path-specific checks include whether files should be skipped, whether
       
    43     custom style rules should apply to certain files, etc.
       
    44         If the checkout root is None or the empty string, this method returns
       
    45     the paths parameter unchanged.
       
    46 
       
    47     Returns:
       
    48       paths: A copy of the paths parameter -- possibly converted, as follows.
       
    49              If this method changed the current working directory to the
       
    50              checkout root, then the list is the paths parameter converted to
       
    51              normalized paths relative to the checkout root.  Otherwise, the
       
    52              paths are not converted.
       
    53 
       
    54     Args:
       
    55       paths: A list of paths to the files that should be checked for style.
       
    56              This argument can be None or the empty list if a git commit
       
    57              or all changes under the checkout root should be checked.
       
    58       checkout_root: The path to the root of the WebKit checkout, or None or
       
    59                      the empty string if no checkout could be detected.
       
    60       mock_os: A replacement module for unit testing.  Defaults to os.
       
    61 
       
    62     """
       
    63     os_module = os if mock_os is None else mock_os
       
    64 
       
    65     if paths is not None:
       
    66         paths = list(paths)
       
    67 
       
    68     if not checkout_root:
       
    69         if not paths:
       
    70             raise Exception("The paths parameter must be non-empty if "
       
    71                             "there is no checkout root.")
       
    72 
       
    73         # FIXME: Consider trying to detect the checkout root for each file
       
    74         #        being checked rather than only trying to detect the checkout
       
    75         #        root for the current working directory.  This would allow
       
    76         #        files to be checked correctly even if the script is being
       
    77         #        run from outside any WebKit checkout.
       
    78         #
       
    79         #        Moreover, try to find the "source root" for each file
       
    80         #        using path-based heuristics rather than using only the
       
    81         #        presence of a WebKit checkout.  For example, we could
       
    82         #        examine parent directories until a directory is found
       
    83         #        containing JavaScriptCore, WebCore, WebKit, WebKitSite,
       
    84         #        and WebKitTools.
       
    85         #             Then log an INFO message saying that a source root not
       
    86         #        in a WebKit checkout was found.  This will allow us to check
       
    87         #        the style of non-scm copies of the source tree (e.g.
       
    88         #        nightlies).
       
    89         _log.warn("WebKit checkout root not found:\n"
       
    90                   "  Path-dependent style checks may not work correctly.\n"
       
    91                   "  See the help documentation for more info.")
       
    92 
       
    93         return paths
       
    94 
       
    95     if paths:
       
    96         # Then try converting all of the paths to paths relative to
       
    97         # the checkout root.
       
    98         rel_paths = []
       
    99         for path in paths:
       
   100             rel_path = _relpath(path, checkout_root)
       
   101             if rel_path is None:
       
   102                 # Then the path is not below the checkout root.  Since all
       
   103                 # paths should be interpreted relative to the same root,
       
   104                 # do not interpret any of the paths as relative to the
       
   105                 # checkout root.  Interpret all of them relative to the
       
   106                 # current working directory, and do not change the current
       
   107                 # working directory.
       
   108                 _log.warn(
       
   109 """Path-dependent style checks may not work correctly:
       
   110 
       
   111   One of the given paths is outside the WebKit checkout of the current
       
   112   working directory:
       
   113 
       
   114     Path: %s
       
   115     Checkout root: %s
       
   116 
       
   117   Pass only files below the checkout root to ensure correct results.
       
   118   See the help documentation for more info.
       
   119 """
       
   120                           % (path, checkout_root))
       
   121 
       
   122                 return paths
       
   123             rel_paths.append(rel_path)
       
   124         # If we got here, the conversion was successful.
       
   125         paths = rel_paths
       
   126 
       
   127     _log.debug("Changing to checkout root: " + checkout_root)
       
   128     os_module.chdir(checkout_root)
       
   129 
       
   130     return paths