使用python的Mercurial脚本 [英] Mercurial scripting with python

查看:92
本文介绍了使用python的Mercurial脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



原因是我想添加它,因为我想在python中以编程方式获取汞修订号/ ID(它不是数字)到我们网站上的css / js文件,如下所示:

 < link rel =stylesheethref =example.css ?{%mercurial_revisionexample.css%}/> 

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



如果你知道在哪里找到好的文档,模块,这也将是有帮助的。我似乎找不到任何地方。



我的解决方案



我最终使用子流程来运行一个获取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'


解决方案

没有官方API,但您可以通过阅读其他扩展程序,特别是与hg捆绑在一起的方式了解最佳做法。对于这个特殊的问题,我会这样做:

 从mercurial import ui,hg 
from mercurial.node import

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')


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'

解决方案

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:

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())

Update At some point the parameter order changed, now it's like this:

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

这篇关于使用python的Mercurial脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆