颠覆 python 绑定文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1448894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:16:16  来源:igfitidea点击:

subversion python bindings documentation?

pythonsvndocumentation

提问by retracile

Where can I find a good introduction to using the subversion python bindings?

我在哪里可以找到使用 subversion python 绑定的好介绍?

I found one section in the svnbookthat talks about it; and some basic examples from 1.3.

在 svnbook中找到了一个讨论它的部分;以及1.3 中的一些基本示例

Is there something a bit more thorough and up-to-date?

有没有更彻底和最新的东西?

回答by sdaau

Just wanted to add a little clarification here.

只是想在这里补充一点说明。

Thanks to the above two answers (@BenjaminWohlwendand @Logan), I became aware there are more than one set of Python bindings/interfaces for Subversion; I did this on my Ubuntu 11.04 box:

感谢以上两个答案(@BenjaminWohlwend@Logan),我意识到 Subversion 有不止一套 Python 绑定/接口;我在我的 Ubuntu 11.04 机器上做了这个:

$ apt-cache search '[Ss]vn|[Ss]ubversion' | grep -i python
python-svn - A(nother) Python interface to Subversion
python-svn-dbg - A(nother) Python interface to Subversion (debug extension)
python-subversion - Python bindings for Subversion
python-subversion-dbg - Python bindings for Subversion (debug extension)
python-opster - a python command line parsing speedster
python-py - Advanced Python testing tool and networking lib
python-rope - Python refactoring library
python-subvertpy - Alternative Python bindings for Subversion

One can look at the Debian package filelist, to determine which libraries these refer to; so we have:

可以查看 Debian 软件包文件列表,以确定它们引用了哪些库;所以我们有:

... and I also found another in the repository:

...我还在存储库中找到了另一个:

The link http://svn.apache.org/repos/asf/subversion(which I got from @BenjaminWohlwend) is apparently the Apache Software Foundation (asf?) Subversion repository for Subversion source code itself.

链接http://svn.apache.org/repos/asf/subversion(我从@BenjaminWohlwend 获得)显然是 Apache 软件基金会(asf?)Subversion 源代码本身的 Subversion 存储库。

The OP's quest for documentation seems related to python-subversion(or SWIG bindings (or libsvn); whose build-from-source instructions are in @Logan's post. I couldn't find much better documentation source from the svn.developer: Using the APIsalready referred in the OP, except for the bindings/swig/python/README; it explains how SWIG generates Python interfaces from C:

OP 对文档的追求似乎与python-subversion(或 SWIG 绑定(或libsvn)有关;其从源代码构建的说明在@Logan的帖子中。我从svn.developer 中找不到更好的文档源: Using the APIsalready在 OP 中引用,除了bindings/swig/python/README;它解释了 SWIG 如何从 C 生成 Python 接口:

TRANSLATING PARAMETER LISTS

The argument-reductions laws of the SWIG bindings something go like
this:

- The module prefix can be omitted.  o:  

     void *some_C_function = svn_client_foo;  

  becomes:  

     import svn.client  
     func = svn.client.foo  

[...]

翻译参数列表

SWIG 绑定的参数减少法则是
这样的:

- The module prefix can be omitted.  o:  

     void *some_C_function = svn_client_foo;  

  becomes:  

     import svn.client  
     func = svn.client.foo  

[...]

Then, one could look in, say, svn/core.py, and find functions (and "Symbols defined explicitly") like svn_mergeinfo_merge; noting that core.pyimports libsvn.core- where libsvnprobably refers to the shared object (.so) files built from the C file libsvn_swig_py/swigutil_py.c.

然后,人们可以查看svn/core.py,并找到像这样的函数(和“显式定义的符号”)svn_mergeinfo_merge;请注意core.py导入libsvn.core- 其中libsvn可能是指.so从 C 文件libsvn_swig_py/swigutil_py.c构建的共享对象 ( ) 文件。

Then, we can look up svn_mergeinfo_merge, and find a commit message like SVNSearch: Subversion (commit 23570 05.03.2007), which refers to that function, and a svn_mergeinfo.h; looking up that file further, we find it in the ASF repository: svn_mergeinfo.h, which indeed contains:

然后,我们可以查找svn_mergeinfo_merge,并找到像SVNSearch: Subversion (commit 23570 05.03.2007)这样的提交消息,它指的是那个函数,还有一个svn_mergeinfo.h; 进一步查找该文件,我们在 ASF 存储库中找到了它:svn_mergeinfo.h,其中确实包含:

/** Like svn_mergeinfo_merge2, but uses only one pool.
 *
 * @deprecated Provided for backward compatibility with the 1.5 API.
 */
SVN_DEPRECATED
svn_error_t *
svn_mergeinfo_merge(svn_mergeinfo_t mergeinfo,
                    svn_mergeinfo_t changes,
                    apr_pool_t *pool);

Seeing DEPRECATEDthere, it's probably good here to refer to svn commit: r1175903 (Mon Sep 26 2011):

看到DEPRECATED那里,在这里参考svn commit: r1175903 (Mon Sep 26 2011)可能很好:

  • subversion/libsvn_subr/mergeinfo.c

    (svn_mergeinfo_merge2): New.

    (svn_mergeinfo_merge): Move to deprecated.c.

    (svn_mergeinfo_catalog_merge): Use the new API.

  • 颠覆/libsvn_subr/mergeinfo.c

    (svn_mergeinfo_merge2):新的。

    (svn_mergeinfo_merge):移至 deprecated.c。

    (svn_mergeinfo_catalog_merge):使用新的 API。

That is - that particular function has been deprecated in 2011 - so hopefully, one's Python SVN bindings and SVN installation should be matching...

也就是说 - 该特定功能已在 2011 年被弃用 - 所以希望一个人的 Python SVN 绑定和 SVN 安装应该匹配......

回答by Logan

If you build Subversion from source the Subversion Python bindings aren't automatically included. You have to specifically build and include them. Luckily, you can do this after you've installed Subversion. The source for the bindings is included within the Subversion source code.

如果您从源代码构建 Subversion,则不会自动包含 Subversion Python 绑定。您必须专门构建并包含它们。幸运的是,您可以在安装 Subversion 后执行此操作。绑定的源代码包含在 Subversion 源代码中。

These instructions are for Subversion 1.5.9 and Python 2.4.3 on Red Hat Enterprise Linux, but they should be easily hackable for recent versions of Subversion and Python and generic unix installs.

这些说明适用于 Red Hat Enterprise Linux 上的 Subversion 1.5.9 和 Python 2.4.3,但对于最新版本的 Subversion 和 Python 以及通用 unix 安装,它们应该很容易被破解。

First, download swig from http://downloads.sourceforge.net/swig

首先,从http://downloads.sourceforge.net/swig下载 swig

tar -xvf swig-<version>.tar.gz
cd swig-<version>

At this point you have to make a decision. You can install swig for all supported languages or you can install for just what you need. 'make check' can take up to an hour to run and may fail due to errors from languages you aren't concerned with.

在这一点上,您必须做出决定。您可以为所有支持的语言安装 swig,也可以只安装您需要的语言。“make check”最多可能需要一个小时才能运行,并且可能会由于您不关心的语言的错误而失败。

If you want to use all supported languages run:

如果要使用所有支持的语言,请运行:

./configure 

If you want to scope down to just python, run:

如果您只想将范围缩小到 python,请运行:

./configure --with-python=/usr/local/bin/python2.4 --without-perl --without-ruby --without-php4

Next, run:

接下来,运行:

make

If you opted for the full install, run:

如果您选择完整安装,请运行:

make -k check

If you scoped down to just python you only need to run the python tests:

如果你只关注 python,你只需要运行 python 测试:

make check-python-examples
make check-python-test-suite

If everything's OK, you're ready to install swig:

如果一切正常,您就可以安装 swig:

make install

进行安装

From here, installing the subversion python bindings should be fairly straightforward:

从这里开始,安装 subversion python 绑定应该相当简单:

tar -xvf subversion-1.5.9.tar.gz --gzip 
cd subversion-1.5.9
make swig-py
make install-swig-py
touch /usr/lib64/python2.4/site-packages/svn-python.pth
echo /usr/local/lib/svn-python > /usr/lib64/python2.4/site-packages/svn-python.pth

As always, your mileage may vary depending on your specific versions and architecture. Good luck.

与往常一样,您的里程可能会因您的特定版本和架构而异。祝你好运。

回答by Benjamin Wohlwend

回答by sdaau

Hope it's OK with another post re: python-subversion: I wanted to try Example 8.3. A Python status crawler - Using the APIs (svnbook). on Ubuntu 11.04, Python 2.7, svn_client_status2crashed on a filename with UTF-8 characters with "Error (22): Error converting entry in directory 'path' to UTF-8" - the solution to this is to call setlocalebefore any calls to this function.

希望在另一篇文章中没问题python-subversion:我想尝试示例 8.3。Python 状态爬虫 - 使用 API (svnbook)。在 Ubuntu 11.04、Python 2.7 上,svn_client_status2在带有 UTF-8 字符的文件名上崩溃,并显示“错误 (22):将目录‘路径’中的条目转换为 UTF-8 时出错” - 解决此问题的方法是setlocale在对此函数进行任何调用之前调用.

I also realized there are svn_client_status3and svn_client_status4in the Python API; svn_client_status3has a slightly different call, and also needs setlocale. However, svn_client_status4should NOT be used - it segfaults, since it needs a struct argument which Python cannot deliver; for more, see #16027750 Debugging: stepping through Python script using gdb?.

我还意识到Python API 中有svn_client_status3svn_client_status4svn_client_status3有一个稍微不同的调用,也需要setlocale. 但是,svn_client_status4不应使用 - 它会导致段错误,因为它需要一个 Python 无法提供的结构参数;有关更多信息,请参见#16027750 调试:使用 gdb 单步执行 Python 脚本?.

To wrap it up, here is the Example 8.3 with locale setting which uses svn_client_status3- and doesn't crash on my system (even on filenames with UTF-8 characters):

总结一下,这里是使用区域设置的示例 8.3,它使用svn_client_status3- 并且不会在我的系统上崩溃(即使在带有 UTF-8 字符的文件名上):

#!/usr/bin/env python

# modified from:
# http://svnbook.red-bean.com/en/1.5/svn.developer.usingapi.html
# does the same as `svn status`, and is marked:
"""Crawl a working copy directory, printing status information."""

# tested on Python 2.7, Ubuntu Natty 11.04; needs:
# sudo apt-get install python-subversion

import locale
print locale.getlocale() # (None, None) - in C: ANSI_X3.4-1968
locale.setlocale(locale.LC_ALL, '') # would print en_US.UTF-8
print locale.getlocale() # NOW it is ('en_US', 'UTF-8')

import sys
import os.path

import getopt
import svn.core, svn.client, svn.wc


def generate_status_code(status):
  """Translate a status value into a single-character status code,
  using the same logic as the Subversion command-line client."""
  code_map = { svn.wc.svn_wc_status_none        : ' ',
               svn.wc.svn_wc_status_normal      : ' ',
               svn.wc.svn_wc_status_added       : 'A',
               svn.wc.svn_wc_status_missing     : '!',
               svn.wc.svn_wc_status_incomplete  : '!',
               svn.wc.svn_wc_status_deleted     : 'D',
               svn.wc.svn_wc_status_replaced    : 'R',
               svn.wc.svn_wc_status_modified    : 'M',
               svn.wc.svn_wc_status_merged      : 'G',
               svn.wc.svn_wc_status_conflicted  : 'C',
               svn.wc.svn_wc_status_obstructed  : '~',
               svn.wc.svn_wc_status_ignored     : 'I',
               svn.wc.svn_wc_status_external    : 'X',
               svn.wc.svn_wc_status_unversioned : '?',
             }
  return code_map.get(status, '?')


def do_status(wc_path, verbose):
  # Build a client context baton.
  ctx = svn.client.svn_client_ctx_t()

  def _status_callback(path, status):
      """A callback function for svn_client_status."""

      # Print the path, minus the bit that overlaps with the root of
      # the status crawl
      text_status = generate_status_code(status.text_status)
      prop_status = generate_status_code(status.prop_status)
      print '%s%s  %s' % (text_status, prop_status, path)

  # Do the status crawl, using _status_callback() as our callback function.
  revision = svn.core.svn_opt_revision_t()
  revision.type = svn.core.svn_opt_revision_head
  #~ svn.client.svn_client_status2(wc_path, revision, _status_callback,
                                #~ svn.core.svn_depth_infinity, verbose,
                                #~ 0, 0, 1, ctx)
  svn.client.svn_client_status3(wc_path, revision, _status_callback,
                                svn.core.svn_depth_infinity, verbose,
                                0, 0, 1, (), ctx)
  # DO NOT USE svn_client_status4! (needs a C struct argument)


def usage_and_exit(errorcode):
    """Print usage message, and exit with ERRORCODE."""
    stream = errorcode and sys.stderr or sys.stdout
    stream.write("""Usage: %s OPTIONS WC-PATH
Options:
  --help, -h    : Show this usage message
  --verbose, -v : Show all statuses, even uninteresting ones
""" % (os.path.basename(sys.argv[0])))
    sys.exit(errorcode)

if __name__ == '__main__':
  # Parse command-line options.
  try:
    opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "verbose"])
  except getopt.GetoptError:
    usage_and_exit(1)
  verbose = 0
  for opt, arg in opts:
    if opt in ("-h", "--help"):
      usage_and_exit(0)
    if opt in ("-v", "--verbose"):
      verbose = 1
  if len(args) != 1:
    usage_and_exit(2)

  # Canonicalize the repository path.
  wc_path = svn.core.svn_path_canonicalize(args[0])

  # Do the real work.
  try:
    do_status(wc_path, verbose)
  except svn.core.SubversionException, e:
    sys.stderr.write("Error (%d): %s\n" % (e.apr_err, e.message))
    sys.exit(1)