bazel,python 规则,创建的文件如何获取它们
bazel, python rules, created files how to get them
如何访问我的 python 正在创建的文件?
所以在 python 里面我得到了类似的东西:
from pathlib import Path
p = Path('my_file.txt')
p.write_text('testing')
print("Testing done")
然后,当我执行 bazel run my_file
时,我可以在终端文本中看到一切顺利,并从印刷品中看到信息,但是当我试图找到 my_file.txt 时,什么也没有。那么我需要如何处理我的规则才能在 bazel 运行 完成后访问该文件?
write_text()
returns写入文本文件的字符数,也许你可以检查一下。或者,尝试 print(p.read_text())
如果您能提供更多详细信息,可能会有所帮助:
- python 代码如何变成 运行?是运行作为一个动作吗?
my_file
目标是什么?
- 你提到你有一些规则。这些 Starlark 规则是你写的吗?
但要直接回答:当一个二进制文件是 运行 和 bazel run
时,它在包含二进制文件所有依赖项的符号链接的“运行文件树”中执行。所以你的文件将被放在二进制文件的 运行files 目录中。
my_program.py
:
import os
from pathlib import Path
print("current working directory: " + os.getcwd())
p = Path('my_file.txt')
p.write_text('testing')
print("Testing done")
BUILD
:
py_binary(
name = "my_program",
srcs = ["my_program.py"],
)
$ bazel run my_program
INFO: Analyzed target //:my_program (18 packages loaded, 90 targets configured).
INFO: Found 1 target...
Target //:my_program up-to-date:
bazel-bin/my_program
INFO: Elapsed time: 0.226s, Critical Path: 0.01s
INFO: 5 processes: 5 internal.
INFO: Build completed successfully, 5 total actions
INFO: Build completed successfully, 5 total actions
current working directory: /home/ahumesky/.cache/bazel/_bazel_ahumesky/5123c9882cdbb6c5e34f583431173549/execroot/__main__/bazel-out/k8-fastbuild/bin/my_program.runfiles/__main__
Testing done
$ ls /home/ahumesky/.cache/bazel/_bazel_ahumesky/5123c9882cdbb6c5e34f583431173549/execroot/__main__/bazel-out/k8-fastbuild/bin/my_program.runfiles/__main__
external my_file.txt my_program my_program.py
$ cat /home/ahumesky/.cache/bazel/_bazel_ahumesky/5123c9882cdbb6c5e34f583431173549/execroot/__main__/bazel-out/k8-fastbuild/bin/my_program.runfiles/__main__/my_file.txt
testing
如果可能,将输出文件路径作为参数传递给程序可能会更好,这样文件就位于某个已知位置:
import sys
from pathlib import Path
p = Path(sys.argv[1])
p.write_text('testing')
print("Testing done")
$ bazel run my_program -- /tmp/output.txt
INFO: Analyzed target //:my_program (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:my_program up-to-date:
bazel-bin/my_program
INFO: Elapsed time: 0.040s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Testing done
$ cat /tmp/output.txt
testing
或者,如果您直接 运行 程序,则文件将位于当前工作目录中:
$ bazel build my_program
INFO: Analyzed target //:my_program (18 packages loaded, 90 targets configured).
INFO: Found 1 target...
Target //:my_program up-to-date:
bazel-bin/my_program
INFO: Elapsed time: 0.219s, Critical Path: 0.01s
INFO: 5 processes: 5 internal.
INFO: Build completed successfully, 5 total actions
$ bazel-bin/my_program
current working directory: /home/ahumesky/test
Testing done
$ cat my_file.txt
testing
看看那个 script:您可以使用 BUILD_WORKING_DIRECTORY
env 变量在工作目录中创建文件路径
如何访问我的 python 正在创建的文件?
所以在 python 里面我得到了类似的东西:
from pathlib import Path
p = Path('my_file.txt')
p.write_text('testing')
print("Testing done")
然后,当我执行 bazel run my_file
时,我可以在终端文本中看到一切顺利,并从印刷品中看到信息,但是当我试图找到 my_file.txt 时,什么也没有。那么我需要如何处理我的规则才能在 bazel 运行 完成后访问该文件?
write_text()
returns写入文本文件的字符数,也许你可以检查一下。或者,尝试 print(p.read_text())
如果您能提供更多详细信息,可能会有所帮助:
- python 代码如何变成 运行?是运行作为一个动作吗?
my_file
目标是什么?- 你提到你有一些规则。这些 Starlark 规则是你写的吗?
但要直接回答:当一个二进制文件是 运行 和 bazel run
时,它在包含二进制文件所有依赖项的符号链接的“运行文件树”中执行。所以你的文件将被放在二进制文件的 运行files 目录中。
my_program.py
:
import os
from pathlib import Path
print("current working directory: " + os.getcwd())
p = Path('my_file.txt')
p.write_text('testing')
print("Testing done")
BUILD
:
py_binary(
name = "my_program",
srcs = ["my_program.py"],
)
$ bazel run my_program
INFO: Analyzed target //:my_program (18 packages loaded, 90 targets configured).
INFO: Found 1 target...
Target //:my_program up-to-date:
bazel-bin/my_program
INFO: Elapsed time: 0.226s, Critical Path: 0.01s
INFO: 5 processes: 5 internal.
INFO: Build completed successfully, 5 total actions
INFO: Build completed successfully, 5 total actions
current working directory: /home/ahumesky/.cache/bazel/_bazel_ahumesky/5123c9882cdbb6c5e34f583431173549/execroot/__main__/bazel-out/k8-fastbuild/bin/my_program.runfiles/__main__
Testing done
$ ls /home/ahumesky/.cache/bazel/_bazel_ahumesky/5123c9882cdbb6c5e34f583431173549/execroot/__main__/bazel-out/k8-fastbuild/bin/my_program.runfiles/__main__
external my_file.txt my_program my_program.py
$ cat /home/ahumesky/.cache/bazel/_bazel_ahumesky/5123c9882cdbb6c5e34f583431173549/execroot/__main__/bazel-out/k8-fastbuild/bin/my_program.runfiles/__main__/my_file.txt
testing
如果可能,将输出文件路径作为参数传递给程序可能会更好,这样文件就位于某个已知位置:
import sys
from pathlib import Path
p = Path(sys.argv[1])
p.write_text('testing')
print("Testing done")
$ bazel run my_program -- /tmp/output.txt
INFO: Analyzed target //:my_program (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:my_program up-to-date:
bazel-bin/my_program
INFO: Elapsed time: 0.040s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Testing done
$ cat /tmp/output.txt
testing
或者,如果您直接 运行 程序,则文件将位于当前工作目录中:
$ bazel build my_program
INFO: Analyzed target //:my_program (18 packages loaded, 90 targets configured).
INFO: Found 1 target...
Target //:my_program up-to-date:
bazel-bin/my_program
INFO: Elapsed time: 0.219s, Critical Path: 0.01s
INFO: 5 processes: 5 internal.
INFO: Build completed successfully, 5 total actions
$ bazel-bin/my_program
current working directory: /home/ahumesky/test
Testing done
$ cat my_file.txt
testing
看看那个 script:您可以使用 BUILD_WORKING_DIRECTORY
env 变量在工作目录中创建文件路径