aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/build_defs/docker/path.bzl
blob: 4e9f13aac28617469ce224ca48e16342b4736403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Rules for manipulating paths."""

def dirname(path):
    """Returns the directory's name."""
    last_sep = path.rfind("/")
    if last_sep == -1:
        return ""  # The artifact is at the top level.
    return path[:last_sep]

def join(directory, path):
    """Compute the relative data path prefix from the data_path attribute."""
    if not path:
        return directory
    if path[0] == "/":
        return path[1:]
    if directory == "/":
        return path
    return directory + "/" + path

def canonicalize(path):
    """Canonicalize the input path."""
    if not path:
        return path

    # Strip ./ from the beginning if specified.
    # There is no way to handle .// correctly (no function that would make
    # that possible and Skylark is not turing complete) so just consider it
    # as an absolute path. A path of / should preserve the entire
    # path up to the repository root.
    if path == "/":
        return path
    if len(path) >= 2 and path[0:2] == "./":
        path = path[2:]
    if not path or path == ".":  # Relative to current package
        return ""
    elif path[0] == "/":  # Absolute path
        return path
    else:  # Relative to a sub-directory
        return path

def strip_prefix(path, prefix):
    """Returns the path with the specified prefix removed."""
    if path.startswith(prefix):
        return path[len(prefix):]
    return path

def runfile(ctx, f):
    """Return the runfiles relative path of f."""
    if ctx.workspace_name:
        return ctx.workspace_name + "/" + f.short_path
    else:
        return f.short_path