File: import.bzl

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (100 lines) | stat: -rw-r--r-- 2,972 bytes parent folder | download | duplicates (6)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
def _py_import_impl(ctx):
    # Unpack the file somewhere, and present as a python library. We need to
    # know all the files in the zip, and that's problematic. For now, we might
    # be able to get away with just creating and declaring the directory.

    root = ctx.actions.declare_directory("%s-pyroot" % ctx.attr.name)
    args = ctx.actions.args()

    if ctx.file.wheel.path.endswith(".zip") or ctx.file.wheel.path.endswith(".whl"):
        args.add("x")
        args.add(ctx.file.wheel.path)
        args.add_all(["-d", root.path])

        ctx.actions.run(
            outputs = [root],
            inputs = [ctx.file.wheel],
            arguments = [args],
            executable = ctx.executable._zip,
        )
    elif ctx.file.wheel.path.endswith(".tar.gz"):
        args.add(ctx.file.wheel.path)
        args.add(root.path)

        ctx.actions.run(
            outputs = [root],
            inputs = [ctx.file.wheel],
            arguments = [args],
            executable = ctx.executable._untar,
        )
    else:
        fail("Unrecognised file extension: %s" % ctx.attr.wheel)

    runfiles = ctx.runfiles(files = [root])
    for dep in ctx.attr.deps:
        runfiles = runfiles.merge(dep[DefaultInfo].default_runfiles)

    imports = depset(
        items = [
            "%s/%s/%s-pyroot" % (ctx.workspace_name, ctx.label.package, ctx.label.name),
        ],
        transitive = [dep[PyInfo].imports for dep in ctx.attr.deps],
    )
    transitive_sources = depset(
        items = [],
        transitive = [dep[PyInfo].transitive_sources for dep in ctx.attr.deps],
    )

    py_srcs = ctx.attr.srcs_version

    info = PyInfo(
        imports = imports,
        has_py2_only_sources = py_srcs == "PY2",
        has_py3_only_sources = py_srcs == "PY3",
        transitive_sources = transitive_sources,
        uses_shared_libraries = not ctx.attr.zip_safe,
    )

    return [
        DefaultInfo(
            files = depset(items = [root]),
            default_runfiles = runfiles,
        ),
        info,
    ]

py_import = rule(
    _py_import_impl,
    attrs = {
        "wheel": attr.label(
            allow_single_file = True,
            mandatory = True,
        ),
        "zip_safe": attr.bool(
            default = True,
        ),
        "python_version": attr.string(
            default = "PY3",
            values = ["PY2", "PY3"],
        ),
        "srcs_version": attr.string(
            default = "PY2AND3",
            values = ["PY2", "PY3", "PY2AND3"],
        ),
        "deps": attr.label_list(
            allow_empty = True,
            providers = [PyInfo],
        ),
        "_zip": attr.label(
            allow_single_file = True,
            cfg = "host",
            default = "@bazel_tools//tools/zip:zipper",
            executable = True,
        ),
        "_untar": attr.label(
            cfg = "exec",
            default = "//py/private:untar",
            executable = True,
        ),
    },
)