简体   繁体   English

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

[英]Determining the Bazaar version number from Python without calling bzr

I have a django (Python) project that needs to know what version its code is on in Bazaar for deployment purposes. 我有一个django(Python)项目,需要了解其代码在Bazaar中用于部署目的的版本。 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. 这是一个Web应用程序,因此我不希望这样做,因为它会触发一个新的子流程,并且不会扩展。

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存储库以计算版本号? Bazaar itself is written in Python and contains this code for calculating the revno, which makes me think it isn't exactly trivial. Bazaar本身是用Python编写的,并包含用于计算revno的代码,这使我认为它并不是完全无关紧要的。

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]

You can use Bazaar's bzrlib API to get information about any given Bazaar repository. 您可以使用Bazaar的bzrlib API来获取有关任何给定Bazaar存储库的信息。

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

More examples are available here . 这里有更多示例。

Do it once and cache the result (in a DB/file, if need be)? 是否只执行一次并缓存结果(如果需要,缓存在数据库/文件中)? I doubt the version is going to change that much. 我怀疑版本会发生太大变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM