2009-10-05 38 views
1

我需要一個具有Web API的軟件(而不是服務)來創建以及與mercurial存儲庫交互。在我的腦海裏我想象中的API可能看起來像:供應mercurial服務器

POST /repos 
name=foo 

在創建一個/repos/foo庫,然後工作按hgwebdir.cgi。

這樣的軟件是否存在?如果沒有,關於如何去實現它的任何建議(我用python語法確定,但對於構​​建這樣的應用程序的最佳方式卻相對毫無幫助)。

回答

3

我不知道這樣的軟件,但如果您熟悉web應用程序和python,那麼自己創建一個軟件就會很麻煩。

import os 
from mercurial import commands,ui 
os.mkdir("/repos/foo") 
commands.init(ui.ui(),"/repos/foo") 

應該這樣做。當然,你需要用它一個很好的WSGI腳本中有網絡接口/ API

如需進一步信息,請參閱Mercurial API documentation

0

hgwebdir.cgi可以讓你一半在那裏,假設他們已經創建這是一種方法來承載他們。

1

我寫了一個俊俏的外殼CGI做的正是這一段時間以前。請注意安全警告:

#!/bin/sh 

echo -n -e "Content-Type: text/plain\n\n" 

mkdir -p /my/repos/$PATH_INFO 
cd /my/repos/$PATH_INFO 
hg init 

請注意使用the full instructions找到的安全警告。

1

對於那些對此感興趣的人,我最終實現了一個python CGI腳本來做這樣的事情,避免了一些安全問題,把repo的命名主要放在服務器端。代碼是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