在不调用 bzr 的情况下从 Python 确定 Bazaar 版本号

发布于 2024-09-17 12:07:23 字数 763 浏览 3 评论 0原文

我有一个 django (Python) 项目,需要知道其代码在 Bazaar 中的版本用于部署目的。这是一个 Web 应用程序,所以我不想这样做,因为它会触发一个新的子进程,并且不会扩展。

import subprocess
subprocess.Popen(["bzr", "revno"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

有没有办法解析 Bazaar 存储库来计算版本号? Bazaar 本身是用 Python 编写的,包含用于计算 revno 的代码,这让我觉得这并不简单。

rh = self.revision_history()
revno = len(rh)

编辑:最终修复

from bzrlib.branch import BzrBranch
branch = BzrBranch.open_containing('.')[0]
revno = len(branch.revision_history())

编辑:最终修复,但这次是真的

from bzrlib.branch import BzrBranch
branch = BzrBranch.open_containing('.')[0]
revno = branch.last_revision_info()[0]

I have a django (Python) project that needs to know what version its code is on in Bazaar for deployment purposes. This is a web application, so I don't want to do this because it fires off a new subprocess and that's not going to scale.

import subprocess
subprocess.Popen(["bzr", "revno"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

Is there a way to parse Bazaar repositories to calculate the version number? Bazaar itself is written in Python and contains this code for calculating the revno, which makes me think it isn't exactly trivial.

rh = self.revision_history()
revno = len(rh)

Edit: Final fix

from bzrlib.branch import BzrBranch
branch = BzrBranch.open_containing('.')[0]
revno = len(branch.revision_history())

Edit: Final fix but for real this time

from bzrlib.branch import BzrBranch
branch = BzrBranch.open_containing('.')[0]
revno = branch.last_revision_info()[0]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2024-09-24 12:07:52

执行一次并缓存结果(如果需要,在数据库/文件中)?我怀疑版本会改变那么多。

Do it once and cache the result (in a DB/file, if need be)? I doubt the version is going to change that much.

怎樣才叫好 2024-09-24 12:07:46

您可以使用 Bazaar 的 bzrlib API 获取有关任何给定 Bazaar 存储库的信息。

>>> from bzrlib.branch import BzrBranch
>>> branch =  BzrBranch.open('.')
>>> branch.last_revision_info()

更多示例请参见此处

You can use Bazaar's bzrlib API to get information about any given Bazaar repository.

>>> from bzrlib.branch import BzrBranch
>>> branch =  BzrBranch.open('.')
>>> branch.last_revision_info()

More examples are available here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文