Espérons que c'est OK avec un autre poste re: python-subversion
: Je voulais essayer Example 8.3. A Python status crawler - Using the APIs (svnbook). sur Ubuntu 11.04, Python 2.7, svn_client_status2
écrasé sur un nom de fichier avec des caractères UTF-8 avec "Erreur (22): Erreur de conversion de l'entrée dans le répertoire 'chemin' à UTF-8" - la solution est d'appeler setlocale
avant toute appelle à cette fonction.
J'ai également réalisé qu'il y a svn_client_status3
et svn_client_status4
dans l'API Python; svn_client_status3
a un appel légèrement différent, et a également besoin de setlocale
. Cependant, svn_client_status4
NE DOIT PAS être utilisé - il segfault, car il a besoin d'un argument struct que Python ne peut pas fournir; pour plus d'informations, voir #16027750 Debugging: stepping through Python script using gdb?.
Pour conclure, voici l'exemple 8.3 avec des paramètres régionaux qui utilise svn_client_status3
- et ne tombe pas en panne sur mon système (même sur les noms de fichiers avec des caractères 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)
pysvn est un autre ensemble de liaisons de python que ceux que je suis désireux d'utiliser . Mais ces exemples peuvent être utiles. – retracile
+1 le svnshell.py sera très utile. Mais je ne vois pas beaucoup de choses liées aux caisses et aux fusions, alors je suis toujours à la recherche de plus. – retracile