Tensorflow的Bazel编程(五)

只了解一下常用的这几种语言的Rules,这一篇是python的rules。

py_binary

py_binary(name, deps, srcs, data, args, compatible_with, default_python_version, deprecation, distribs, features, imports, licenses, main, output_licenses, restricted_to, srcs_version, stamp, tags, testonly, visibility)

A py_binary is an executable Python program consisting of a collection of .py source files (possibly belonging to other py_library rules), a *.runfiles directory tree containing all the code and data needed by the program at run-time, and a stub script that starts up the program with the correct initial environment and data.

Examples
py_binary(
    name = "foo",
    srcs = ["foo.py"],
    data = [":transform"],  # a cc_binary which we invoke at run time
    deps = [
        "//pyglib",
        ":foolib",  # a py_library
    ],
)

If you want to run a py_binary from within another binary or test (for example, running a python binary to set up some mock resource from within a java_test) then the correct approach is to make the other binary or test depend on the py_binary in its data section. The other binary can then locate the py_binary relative to the source directory.

py_binary(
    name = "test_main",
    srcs = ["test_main.py"],
    deps = [":testlib"],
)

java_library(
    name = "testing",
    srcs = glob(["*.java"]),
    data = [":test_main"]
)

Arguments

Attributes
name

Name; required

A unique name for this rule.


If main is unspecified, this should be the same as the name of the source file that is the main entry point of the application, minus the extension. For example, if your entry point is called main.py, then your name should be main.
deps

List of labels; optional

The list of other libraries to be linked in to the binary target. See general comments about deps at Attributes common to all build rules. These can be py_binary rules, py_library rules or cc_library rules,
srcs

List of labels; required

The list of source files that are processed to create the target. This includes all your checked-in code and any generated source files. The line between srcs and deps is loose. The .py files probably belong in srcs and library targets probably belong in deps, but don't worry about it too much.
data

List of labels; optional

The list of files needed by this binary at runtime. See general comments about data at Attributes common to all build rules. Also see the data argument of the py_library rule for details.
default_python_version

String; optional; nonconfigurable; default is "PY2"

A string specifying the default Python major version to use when building this binary and all of its deps. Valid values are "PY2" (default) or "PY3". Python 3 support is experimental.
imports

List of strings; optional

List of import directories to be added to the PYTHONPATH.

Subject to "Make variable" substitution. These import directories will be added for this rule and all rules that depend on it (note: not the rules this rule depends on. Each directory will be added to PYTHONPATH by py_binary rules that depend on this rule.

Absolute paths (paths that start with /) and paths that references a path above the execution root are not allowed and will result in an error.

main

Label; optional

The name of the source file that is the main entry point of the application. This file must also be listed in srcs. If left unspecified, name is used instead (see above). If name does not match any filename in srcs, main must be specified.
srcs_version

String; optional; default is "PY2"

A string specifying the Python major version(s) that the .py source files listed in the srcs of this rule are compatible with. Valid values are:
"PY2ONLY" - Python 2 code that is not suitable for 2to3 conversion.
"PY2" - Python 2 code that is expected to work when run through 2to3.
"PY2AND3" - Code that is compatible with both Python 2 and 3 without 2to3 conversion.
"PY3" - Python 3 code that will not run on Python 2.
stamp

Integer; optional; default is -1

Enable link stamping. Whether to encode build information into the binary. Possible values:
  • stamp = 1: Stamp the build information into the binary. Stamped binaries are only rebuilt when their dependencies change. Use this if there are tests that depend on the build information.
  • stamp = 0: Always replace build information by constant values. This gives good build result caching.
  • stamp = -1: Embedding of build information is controlled by the --[no]stamp Bazel flag.

py_library

py_library(name, deps, srcs, data, compatible_with, deprecation, distribs, features, imports, licenses, restricted_to, srcs_version, tags, testonly, visibility)

Arguments

Attributes
name

Name; required

A unique name for this rule.

deps

List of labels; optional

The list of other libraries to be linked in to the library target. See general comments about deps at Attributes common to all build rules. In practice, these arguments are treated like those in srcs; you may move items between these lists willy-nilly. It's probably more readable to keep your .py files in your srcs.
srcs

List of labels; optional

The list of source files that are processed to create the target. This includes all your checked-in code and any generated source files.
data

List of labels; optional

The list of files needed by this library at runtime. See general comments about data at Attributes common to all build rules.
imports

List of strings; optional

List of import directories to be added to the PYTHONPATH.

Subject to "Make variable" substitution. These import directories will be added for this rule and all rules that depend on it (note: not the rules this rule depends on. Each directory will be added to PYTHONPATH by py_binary rules that depend on this rule.

Absolute paths (paths that start with /) and paths that references a path above the execution root are not allowed and will result in an error.

srcs_version

String; optional; default is "PY2"

A string specifying the Python major version(s) that the .py source files listed in the srcs of this rule are compatible with. Valid values are:
"PY2ONLY" - Python 2 code that is not suitable for 2to3 conversion.
"PY2" - Python 2 code that is expected to work when run through 2to3.
"PY2AND3" - Code that is compatible with both Python 2 and 3 without 2to3 conversion.
"PY3" - Python 3 code that will not run on Python 2.

py_test

py_test(name, deps, srcs, data, args, compatible_with, default_python_version, deprecation, distribs, features, flaky, imports, licenses, local, main, restricted_to, shard_count, size, srcs_version, stamp, tags, testonly, timeout, visibility)

A py_test() rule compiles a test. A test is a binary wrapper around some test code.

Examples

py_test(
    name = "runtest_test",
    srcs = ["runtest_test.py"],
    deps = [
        "//path/to/a/py/library",
    ],
)

It's also possible to specify a main module:

py_binary(
    name = "foo",
    srcs = [
        "bar.py",
        "baz.py",
    ],
    main = "bar.py",
)

Arguments

Attributes
name

Name; required

A unique name for this rule.

deps

List of labels; optional

The list of other libraries to be linked in to the binary target. See general comments about deps at Attributes common to all build rules. These can be py_binary rules, py_library rules or cc_library rules,
srcs

List of labels; required

The list of source files that are processed to create the target. This includes all your checked-in code and any generated source files. The line between srcs and deps is loose. The .py files probably belong in srcs and library targets probably belong in deps, but don't worry about it too much.
data

List of labels; optional

The list of files needed by this binary at runtime. See general comments about data at Attributes common to all build rules. Also see the data argument of the py_library rule for details.
default_python_version

String; optional; nonconfigurable; default is "PY2"

A string specifying the default Python major version to use when building this binary and all of its deps. Valid values are "PY2" (default) or "PY3". Python 3 support is experimental.
imports

List of strings; optional

List of import directories to be added to the PYTHONPATH.

Subject to "Make variable" substitution. These import directories will be added for this rule and all rules that depend on it (note: not the rules this rule depends on. Each directory will be added to PYTHONPATH by py_binary rules that depend on this rule.

Absolute paths (paths that start with /) and paths that references a path above the execution root are not allowed and will result in an error.

main

Label; optional

The name of the source file that is the main entry point of the application. This file must also be listed in srcs. If left unspecified, name is used instead (see above). If name does not match any filename in srcs, main must be specified.
srcs_version

String; optional; default is "PY2"

A string specifying the Python major version(s) that the .py source files listed in the srcs of this rule are compatible with. Valid values are:
"PY2ONLY" - Python 2 code that is not suitable for 2to3 conversion.
"PY2" - Python 2 code that is expected to work when run through 2to3.
"PY2AND3" - Code that is compatible with both Python 2 and 3 without 2to3 conversion.
"PY3" - Python 3 code that will not run on Python 2.
stamp

Integer; optional; default is 0

See the section on py_binary() arguments, except that the stamp argument is set to 0 by default for tests.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值