配置Mercurial服务器
我需要一个软件(不是服务),它有一个网络接口,可以用来创建和操作mercurial代码库。在我脑海中,我想象这个接口可能是这样的:
POST /repos
name=foo
在 /repos/foo
创建一个代码库,然后它就像 hgwebdir.cgi 一样工作。
有没有这样的软件?如果没有,有没有什么建议可以帮助我实现这个功能(我对python语法还可以,但对如何构建这样的应用程序相对不太了解)。
5 个回答
1
对于感兴趣的朋友,我最终实现了一个Python CGI脚本,做了一些类似的事情,通过将仓库的命名大部分交给服务器来避免一些安全问题。代码可以在这里找到:https://bitbucket.org/jimdowning/lf-hg-server/。主要的 create.py
脚本看起来是这样的:-
#!/usr/bin/env python
from __future__ import with_statement
import cgi
import ConfigParser
from mercurial import commands, ui
import os
import uuid
import cgitb
cgitb.enable()
def bad_request(reason):
print "Content-type: text/html"
print "Status: 400 ", reason
print
print "<html><body><h1>Bad request</h1><p>", reason, "</p></body></html>"
def abs_url(rel, env):
protocol = env["SERVER_PROTOCOL"].partition("/")[0].lower()
host = env["HTTP_HOST"]
return "%s://%s/%s" % (protocol, host, rel)
if not os.environ["REQUEST_METHOD"] == "POST":
bad_request("Method was not POST")
elif not (form.has_key("user")) :
bad_request("Missing parameter - requires 'user' param")
else :
form = cgi.FieldStorage()
user = form["user"].value
lfDir = os.path.dirname( os.environ["SCRIPT_FILENAME"])
id = str(uuid.uuid1())
userDir = os.path.join(lfDir, "repos", user)
rDir = os.path.join(userDir, id)
relPath = "lf/"+ rDir[len(lfDir)+1:]
os.makedirs(rDir)
commands.init(ui.ui(), rDir)
repoUrl = abs_url(relPath, os.environ)
config = ConfigParser.ConfigParser()
config.add_section("web")
config.set("web", "allow_push", "*")
config.set("web", "push_ssl", "false")
with open(os.path.join(rDir, ".hg", "hgrc"), "w+") as f:
config.write(f)
print "Content-Type: text/html\n"
print "Status: 201\nLocation:"
print
print "<html><body><h1>Repository Created</h1>"
print "<p>Created a repo at <a href=\""+ repoUrl + "\">"
print repoUrl+ "</a></p>"
print "</body></html>"
我把这个脚本放在和 hgwebdir.cgi
同一个目录下。通过在 hgweb.config
中做一些小把戏,它的整体功能变得简单了很多 :-
[collections]
repos/ = repos/
[web]
style = gitweb
2
3
我不知道有没有这样的软件,但如果你对网页应用和Python有点了解,其实自己做一个也很简单。
import os
from mercurial import commands,ui
os.mkdir("/repos/foo")
commands.init(ui.ui(),"/repos/foo")
这样就可以了。当然,你需要把它放在一个漂亮的WSGI脚本里,这样才能有网页界面或者API。
想了解更多信息,可以查看Mercurial API文档。