【问题标题】:PySVN - Determine if a repository existsPySVN - 确定存储库是否存在
【发布时间】:2011-07-10 06:45:39
【问题描述】:

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

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

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

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

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

干杯,

皮特

【问题讨论】:

    标签: python svn repository exists


    【解决方案1】:

    彼得,

    我和我的团队都经历过同样的挑战。 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 和“未找到主机”情况很重要。

    【讨论】:

    • 如果上面的代码无法解释调用仍然挂起,您可能需要实现 pysvn.Client.callback_cancel 并查看收到回调时的执行状态。否则,我需要查看有关您的代码/服务器配置的更多细节。如果您愿意,请随时离线与我联系。
    【解决方案2】:

    我无法重现您的挂起凭据回调问题,因此可能需要对问题进行扩展描述。我在 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]
    

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多