1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
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.
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.
27 from webkitpy.common.system.ospath import relpath as _relpath
30 _log = logging.getLogger(__name__)
33 def change_directory(checkout_root, paths, mock_os=None):
34 """Change the working directory to the WebKit checkout root, if possible.
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
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.
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.
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.
63 os_module = os if mock_os is None else mock_os
70 raise Exception("The paths parameter must be non-empty if "
71 "there is no checkout root.")
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.
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, Websites,
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.
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.")
96 # Then try converting all of the paths to paths relative to
100 rel_path = _relpath(path, checkout_root)
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
109 """Path-dependent style checks may not work correctly:
111 One of the given paths is outside the WebKit checkout of the current
117 Pass only files below the checkout root to ensure correct results.
118 See the help documentation for more info.
120 % (path, checkout_root))
123 rel_paths.append(rel_path)
124 # If we got here, the conversion was successful.
127 _log.debug("Changing to checkout root: " + checkout_root)
128 os_module.chdir(checkout_root)