首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PySVN -确定存储库是否存在

PySVN -确定存储库是否存在
EN

Stack Overflow用户
提问于 2011-03-07 10:57:09
回答 2查看 5K关注 0票数 2

我正在编写一个管理几个SVN存储库的小脚本。用户通过他们想要更改的存储库的ID ( repos的根是https://www.mydomain.com/形式的)。

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

代码语言:javascript
运行
复制
client = pysvn.Client()
client.list("https://.../<username>/")

但是,如果回购不存在,则脚本挂在列表行上。从追溯过程中可以看出,pysvn实际上挂在登录凭据回调(client.callback_get_login --我已经实现了,但省略了,如果存在回购)。

你能建议我如何确定回购是否存在或不使用pysvn?

干杯,

皮特

EN

回答 2

Stack Overflow用户

发布于 2011-03-07 15:46:57

我无法重现您在凭据回调问题中的挂起情况,因此可能需要对问题进行扩展描述。我在Ubuntu10.04,Python2.6.6上运行pysvn 1.7.2。

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

代码语言:javascript
运行
复制
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]
票数 1
EN

Stack Overflow用户

发布于 2011-03-07 19:52:55

彼得,

我和我的团队也经历过同样的挑战。尝试提供一个callback_get_login函数,但将callback_server_ssl_trust_prompt设置为返回(True, trust_dict['failures'], True)。IFF subversion没有缓存您的服务器证书信任设置,然后您可能会发现info2() (或Peter的list()命令)挂起(它实际上不是挂起的,只是间歇地需要更长的时间返回)。奇怪的是,当您在这些场景中使用CTRL解释器时,会发现它挂在登录回调上,而不是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,你需要决定你想要的行为是什么。是否只允许已缓存信任答案的连接?或者,无论服务器证书验证如何,您都希望始终接受(警告:这可能(显然)会产生负面的安全影响)。考虑以下建议:

代码语言:javascript
运行
复制
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和“没有找到主机”场景是很重要的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5218810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档