使用 python 编写 Mercurial 脚本
我正在尝试在 python 中以编程方式获取 Mercurial 修订号/id(它是哈希而不是数字)。
原因是我想将其添加到我们网站上的 css/js 文件中,如下所示:
<link rel="stylesheet" href="example.css?{% mercurial_revision "example.css" %}" />
这样,每当对样式表进行更改时,它将获得一个新的 url,并且不再使用旧的缓存版本。
或者如果您知道在哪里可以找到 Mercurial python 模块的良好文档,那也会很有帮助。 我似乎无法在任何地方找到它。
我的解决方案
我最终使用 subprocess 来运行获取 hg 节点的命令。 我选择这个解决方案是因为 api 不能保证保持不变,但 bash 接口可能会:
import subprocess
def get_hg_rev(file_path):
pipe = subprocess.Popen(
["hg", "log", "-l", "1", "--template", "{node}", file_path],
stdout=subprocess.PIPE
)
return pipe.stdout.read()
示例使用:
> path_to_file = "/home/jim/workspace/lgr/pinax/projects/lgr/site_media/base.css"
> get_hg_rev(path_to_file)
'0ed525cf38a7b7f4f1321763d964a39327db97c4'
I am trying to get the mercurial revision number/id (it's a hash not a number) programmatically in python.
The reason is that I want to add it to the css/js files on our website like so:
<link rel="stylesheet" href="example.css?{% mercurial_revision "example.css" %}" />
So that whenever a change is made to the stylesheet, it will get a new url and no longer use the old cached version.
OR if you know where to find good documentation for the mercurial python module, that would also be helpful. I can't seem to find it anywhere.
My Solution
I ended up using subprocess to just run a command that gets the hg node. I chose this solution because the api is not guaranteed to stay the same, but the bash interface probably will:
import subprocess
def get_hg_rev(file_path):
pipe = subprocess.Popen(
["hg", "log", "-l", "1", "--template", "{node}", file_path],
stdout=subprocess.PIPE
)
return pipe.stdout.read()
example use:
> path_to_file = "/home/jim/workspace/lgr/pinax/projects/lgr/site_media/base.css"
> get_hg_rev(path_to_file)
'0ed525cf38a7b7f4f1321763d964a39327db97c4'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
确实没有官方 API,但您可以通过阅读其他扩展(尤其是与 hg 捆绑的扩展)来了解最佳实践。 对于这个特定的问题,我会做这样的事情:
更新 在某些时候参数顺序发生了变化,现在是这样的:
It's true there's no official API, but you can get an idea about best practices by reading other extensions, particularly those bundled with hg. For this particular problem, I would do something like this:
Update At some point the parameter order changed, now it's like this:
您是指此文档吗?
请注意,如该页面所述,没有官方 API,因为他们仍然保留随时更改它的权利。 但你可以看到最近几个版本的更改列表,它不是很广泛。
Do you mean this documentation?
Note that, as stated in that page, there is no official API, because they still reserve the right to change it at any time. But you can see the list of changes in the last few versions, it is not very extensive.
一个更新的、更干净的子进程版本(使用
.check_output()
,在 Python 2.7/3.1 中添加),我在 Django 设置文件中使用它进行粗略的端到端部署检查(我将其转储到HTML 注释):如果您不希望出现一些奇怪的问题来阻止启动,您可以将其包装在
try
中:An updated, cleaner subprocess version (uses
.check_output()
, added in Python 2.7/3.1) that I use in my Django settings file for a crude end-to-end deployment check (I dump it into an HTML comment):You could wrap it in a
try
if you don't want some strange hiccup to prevent startup:如果您使用的是 Python 2,则需要使用
hglib
.抱歉,如果您使用的是 Python 3,我不知道该使用什么。 可能是
hgapi
。本答案的内容
Mercurial 的 API
Mercurial 有两个官方 API。
hglib
(wiki) 从 Python 2 与它进行交互PyPI) 包,由 Mercurial 团队维护。subprocess
与其对话,或 < a href="https://pypi.python.org/pypi/hgapi/1.7.2" rel="noreferrer">hgapi
等。如何使用 hglib
安装:
用法:
更多使用信息请参见 hglib wiki 页面。
为什么 hglib 是 Python 2 用户的最佳选择
因为它是由 Mercurial 团队维护的,并且是 Mercurial 团队推荐用于与 Mercurial 交互的方式。
来自 Mercurial 的 wiki,有关与 Mercurial 交互的以下声明:
从命令服务器页面:
如前所述,Mercurial 命令服务器的 Python 接口是 hglib。
顺便说一句,命令行界面的每个命令的开销可不是开玩笑的。 我曾经构建了一个非常小的测试套件(只有大约 5 个测试),它通过
subprocess
使用hg
来创建、逐次提交、一些具有例如合并情况的存储库。 在整个项目中,套件的运行时间保持在 5 到 30 秒之间,几乎所有时间都花在hg
调用上。如果你正在编写一个钩子,那么不鼓励的内部接口非常方便
Python钩子函数的签名如下:
ui
和repo
是前面提到的不鼓励的非官方的一部分内部 API。 事实上,它们就在函数参数中,这使得它们使用起来非常方便,例如在这个preupdate
挂钩的示例中,它不允许在某些分支之间进行合并。如果您的钩子代码不是那么重要,并且您不发布它,您可能会选择使用不鼓励的非官方内部 API。 如果您的挂钩是您要发布的扩展的一部分,最好使用
hglib
。If you are using Python 2, you want to use
hglib
.I don't know what to use if you're using Python 3, sorry. Probably
hgapi
.Contents of this answer
Mercurial's APIs
Mercurial has two official APIs.
hglib
(wiki, PyPI) package, which is maintained by the Mercurial team.subprocess
, orhgapi
, or somesuch.How to use hglib
Installation:
Usage:
More usage information on the hglib wiki page.
Why hglib is the best choice for Python 2 users
Because it is maintained by the Mercurial team, and it is what the Mercurial team recommend for interfacing with Mercurial.
From Mercurial's wiki, the following statement on interfacing with Mercurial:
From the command server page:
The Python interface to the Mercurial command-server, as said, is
hglib
.The per-command overhead of the command line interface is no joke, by the way. I once built a very small test suite (only about 5 tests) that used
hg
viasubprocess
to create, commit by commit, a handful of repos with e.g. merge situations. Throughout the project, the runtime of suite stayed between 5 to 30 seconds, with nearly all time spent in thehg
calls.If you're writing a hook, that discouraged internal interface is awfully convenient
The signature of a Python hook function is like so:
ui
andrepo
are part of the aforementioned discouraged unofficial internal API. The fact that they are right there in your function args makes them terribly convenient to use, such as in this example of apreupdate
hook that disallows merges between certain branches.If your hook code is not so important, and you're not publishing it, you might choose to use the discouraged unofficial internal API. If your hook is part of an extension that you're publishing, better use
hglib
.尝试关键字扩展
give a try to the keyword extension
FWIW 为了避免在每个页面/视图渲染上获取该值,我只是将部署放入
settings.py
文件中。 然后我可以引用settings.REVISION,而无需访问 Mercurial 和/或其他进程的所有开销。 您是否曾经在不重新加载服务器的情况下更改过此值?FWIW to avoid fetching that value on every page/view render, I just have my deploy put it into the
settings.py
file. Then I can referencesettings.REVISION
without all the overhead of accessing mercurial and/or another process. Do you ever have this value change w/o reloading your server?我想做OP想做的同样的事情,从脚本中获取
hg id -i
(获取整个存储库的提示修订版,而不是该存储库中的单个文件),但我没有想要使用 popen,来自brendan
的代码让我开始使用,但这不是我想要的。所以我写了这个......欢迎评论/批评。 这会将十六进制的尖端转速作为字符串获取。
I wanted to do the same thing the OP wanted to do, get
hg id -i
from a script (get tip revision of the whole REPOSITORY, not of a single FILE in that repo) but I did not want to use popen, and the code frombrendan
got me started, but wasn't what I wanted.So I wrote this... Comments/criticism welcome. This gets the tip rev in hex as a string.