gpt4 book ai didi

python - 使用 python 编写 Mercurial 脚本

转载 作者:IT老高 更新时间:2023-10-28 22:09:07 26 4
gpt4 key购买 nike

我正在尝试在 python 中以编程方式获取 mercurial 修订号/id(它是哈希而不是数字)。

原因是我想将它添加到我们网站上的 css/js 文件中,如下所示:

<link rel="stylesheet" href="example.css?{% mercurial_revision "example.css" %}" />

这样,每当对样式表进行更改时,它将获得一个新的 url,并且不再使用旧的缓存版本。

如果您知道在哪里可以找到关于 mercurial python 模块 的优秀文档,那也会很有帮助。我似乎在任何地方都找不到它。

我的解决方案

我最终使用 subprocess 来运行一个获取 hg 节点的命令。我选择这个解决方案是因为 api 不能保证保持不变,但 bash 接口(interface)可能会:

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'

最佳答案

确实没有官方 API,但您可以通过阅读其他扩展(尤其是与 hg 捆绑的扩展)了解最佳实践。对于这个特殊的问题,我会这样做:

from mercurial import ui, hg
from mercurial.node import hex

repo = hg.repository('/path/to/repo/root', ui.ui())
fctx = repo.filectx('/path/to/file', 'tip')
hexnode = hex(fctx.node())

更新在某些时候参数顺序发生了变化,现在是这样的:

   repo = hg.repository(ui.ui(), '/path/to/repo/root' )

关于python - 使用 python 编写 Mercurial 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1153469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com