PySVN - 确定存储库是否存在

发布于 2024-10-20 16:07:15 字数 489 浏览 5 评论 0原文

我正在编写一个小脚本来管理多个 SVN 存储库。用户传递他们想要更改的存储库的 ID(存储库的根的格式为 https://www.mydomain.com/)。 mydomain.com/)。

我需要检查给定的存储库是否确实存在。我尝试使用 Client.list 来查看是否可以找到任何文件,如下所示:

client = pysvn.Client()
client.list("https://.../<username>/")

但是如果存储库不存在,则脚本将挂在列表行上。通过挖掘回溯,看起来 pysvn 实际上挂在登录凭据回调上(client.callback_get_login - 我已经实现但省略了,如果存储库存在,它不会失败)。

你能建议我如何使用 pysvn 确定存储库是否存在?

干杯,

皮特

I'm writing a small script that manages several SVN repositories. Users pass through the ID of the repository they want to change (the root of the repos are of the form https://www.mydomain.com/).

I need to check if the given repo actually exists. I've tried using Client.list to see if I can find any files, like so:

client = pysvn.Client()
client.list("https://.../<username>/")

But if the repo does not exist then the script hangs on the list line. From digging through the tracebacks it looks like pysvn is actually hanging on the login credentials callback (client.callback_get_login - which I have implemented but omitted, it does not fail if the repo exists).

Can you suggest how I can determine if a repo exists or not using pysvn?

Cheers,

Pete

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云裳 2024-10-27 16:07:15

我无法重现您的挂在凭据回调中问题,因此可能需要对该问题进行扩展描述。我在 Ubuntu 10.04、Python 2.6.6 上运行 pysvn 1.7.2。

当我尝试使用 client.list() 列出不存在的远程存储库时,它会引发异常。您还可以使用 client.info2() 来检查远程存储库是否存在:

head_rev = pysvn.Revision(pysvn.opt_revision_kind.head)
bad_repo = 'https://.../xyz_i_dont_exist'
good_repo = 'https://.../real_project'
for url in (bad_repo, good_repo):
    try:
        info = client.info2(url, revision=head_rev, recurse=False)
        print url, 'exists.'
    except pysvn._pysvn_2_6.ClientError, ex:
        if 'non-existent' in ex.args[0]:
            print url, 'does not exist'
        else:
            print url, 'error:', ex.args[0]

I couldn't reproduce your hanging in credentials callback problem, so it might need an expanded description of the problem. I'm running pysvn 1.7.2 on Ubuntu 10.04, Python 2.6.6.

When I try to list a non-existent remote repository with client.list() it raises an exception. You could also use client.info2() to check for existence of a remote repository:

head_rev = pysvn.Revision(pysvn.opt_revision_kind.head)
bad_repo = 'https://.../xyz_i_dont_exist'
good_repo = 'https://.../real_project'
for url in (bad_repo, good_repo):
    try:
        info = client.info2(url, revision=head_rev, recurse=False)
        print url, 'exists.'
    except pysvn._pysvn_2_6.ClientError, ex:
        if 'non-existent' in ex.args[0]:
            print url, 'does not exist'
        else:
            print url, 'error:', ex.args[0]
眼睛会笑 2024-10-27 16:07:15

彼得,

我和我的团队经历了同样的挑战。 Samplebias,尝试提供 callback_get_login 函数,但将 callback_server_ssl_trust_prompt 设置为返回 (True, trust_dict['failures'], True)。 IFF subversion 没有缓存你的服务器证书信任设置,那么你可能会发现 info2() (或 Peter 的 list() 命令)挂起(它实际上不是挂起,它只是间歇性地需要更长的时间才能返回)。奇怪的是,当您在这些场景中按 CTRL-C 解释器时,您会得到指示,表明它挂在登录回调上,而不是 server_cert 验证上。尝试一下您的 ~/.subversion/auth 设置(特别是 svn.simplesvn.ssl.server 目录),然后您就可以了您会看到不同数量的“停留时间”。如果您需要处理真正永远不会返回的情况,请查看 pysvn.Client.callback_cancel 。

考虑: http://pysvn.tigris.org/docs/pysvn_prog_ref.html#pysvn_client_callback_ssl_server_trust_prompt 你需要决定你想要的行为是什么。您是否只想允许那些已缓存信任答案的连接?或者,无论服务器证书验证如何,您是否要始终接受(警告:这可能(显然)产生负面的安全影响)。考虑以下建议:

import pysvn

URL1 = "https://exists.your.org/svn/repos/dev/trunk/current"
URL2 = "https://doesntexit.your.org/svn/repos/dev/trunk/current"
URL3 = "https://exists.your.org/svn/repos/dev/trunk/youDontHavePermissionsBranch"

ALWAYS = "ALWAYS"
NEVER = "NEVER"

DESIRED_BEHAVIOR = ALWAYS

def ssl_server_certificate_trust_prompt(trust_dict):

if DESIRED_BEHAVIOR == NEVER:
    return (False, 0, False)
elif DESIRED_BEHAVIOR == ALWAYS:
    return (True, trust_dict['failures'], True)
raise Exception, "Unsupported behavior"

def testURL(url):
    try:
        c.info2(url)
        return True
    except pysvn.ClientError, ce:
        if ('non-existant' in ce.args[0]) or ('Host not found' in ce.args[0]):
            return False
        else:
            raise ce

c = pysvn.Client()
c.callback_ssl_server_trust_prompt = lambda t: (False, t['failures'], True)
c.callback_get_login = lambda x, y, z: (True, "uname", "pw", False)

if not testURL(URL1): print "Test1 failed."

if testURL(URL2): print "Test2 failed."
try:
    testURL(URL3)
    print "Test3 failed."
except: pass

实际上,您可能不想像我一样对返回值感兴趣。我确实认为单独考虑服务器返回的潜在 403 和“找不到主机”场景很重要。

Peter,

My team and I have experienced the same challenge. Samplebias, try providing a callback_get_login function but set your callback_server_ssl_trust_prompt to return (True, trust_dict['failures'], True). IFF subversion has not cached your server certificate trust settings, then you may find the info2() (or Peter's list() command) hangs (it's not actually hanging, it just takes intermittently much longer time to return). Oddly, when you CTRL-C the interpreter in these scenarios, you'll get indication that it hung on the login callback, not the server_cert verification. Play around with your ~/.subversion/auth settings (in particular the svn.simple and svn.ssl.server directories) and you'll see different amounts of 'hang time'. Look at pysvn.Client.callback_cancel if you need to handle situations which truly never return.

Considering: http://pysvn.tigris.org/docs/pysvn_prog_ref.html#pysvn_client_callback_ssl_server_trust_prompt you need to decide what your desired behavior is. Do you want ONLY to allow those connections for which you already have a cached trust answer? Or, do you want to ALWAYS accept regardless of server certificate verification (WARNING: this could (obviously) have negative security implications). Consider the following suggestion:

import pysvn

URL1 = "https://exists.your.org/svn/repos/dev/trunk/current"
URL2 = "https://doesntexit.your.org/svn/repos/dev/trunk/current"
URL3 = "https://exists.your.org/svn/repos/dev/trunk/youDontHavePermissionsBranch"

ALWAYS = "ALWAYS"
NEVER = "NEVER"

DESIRED_BEHAVIOR = ALWAYS

def ssl_server_certificate_trust_prompt(trust_dict):

if DESIRED_BEHAVIOR == NEVER:
    return (False, 0, False)
elif DESIRED_BEHAVIOR == ALWAYS:
    return (True, trust_dict['failures'], True)
raise Exception, "Unsupported behavior"

def testURL(url):
    try:
        c.info2(url)
        return True
    except pysvn.ClientError, ce:
        if ('non-existant' in ce.args[0]) or ('Host not found' in ce.args[0]):
            return False
        else:
            raise ce

c = pysvn.Client()
c.callback_ssl_server_trust_prompt = lambda t: (False, t['failures'], True)
c.callback_get_login = lambda x, y, z: (True, "uname", "pw", False)

if not testURL(URL1): print "Test1 failed."

if testURL(URL2): print "Test2 failed."
try:
    testURL(URL3)
    print "Test3 failed."
except: pass

In actuality, you probably don't want to get as fancy as I have with the return values. I do think it was important to consider a potential 403 returned by the server and the "Host not found" scenario separately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文