#!/usr/bin/env python

import os
import sys
import re
import unicodedata
try:
    from subprocess import CalledProcessError
    CalledProcessError  # pyflakes
except ImportError:
    CalledProcessError = SystemError
from subprocess import PIPE
from distutils import log

# XML format: <entry\n   kind="file">\n<name>README.txt</name> ...
FILENAME_RE = re.compile('<entry\s+kind="file">\s*<name>(.*?)</name>',
    re.MULTILINE)


try:
    from subprocess import check_output
    check_output  # pyflakes
except ImportError:
    # Python 2.6 and before
    def check_output(*popenargs, **kwargs):
        from subprocess import Popen
        if 'stdout' in kwargs:
            raise ValueError(
                'stdout argument not allowed, it will be overridden.')
        process = Popen(stdout=PIPE, *popenargs, **kwargs)
        output, unused_err = process.communicate()
        retcode = process.poll()
        if retcode:
            cmd = kwargs.get("args")
            if cmd is None:
                cmd = popenargs[0]
            raise CalledProcessError(retcode, cmd)
        return output


def listfiles(directory='', __name__=__name__):
    # Return quietly if this is not a Subversion sandbox
    try:
        files = check_output(['svn', 'info', directory],
            stderr=PIPE)
    except (CalledProcessError, OSError):
        return []
    # Run 'svn list' and log an error if something goes wrong
    try:
        files = check_output(['svn', 'list', '-R', '--xml', directory],
            stderr=PIPE)
    except (CalledProcessError, OSError):
        log.warn("%s: Error running 'svn list'", __name__)
        return []
    # Return filesystem encoding in Python 2 and Unicode in Python 3
    return [encode(compose(m.group(1))) for m in FILENAME_RE.finditer(
        decode(files))]


def decode(text):
    # Decode to Unicode
    if sys.version_info >= (3,):
        return text.decode('utf-8')
    else:
        return text


def encode(text):
    # Encode to filesystem encoding
    if sys.version_info >= (3,):
        return text
    else:
        encoding = sys.stdout.encoding
        if encoding.lower() == 'utf-8':
            return text
        try:
            return text.decode('utf-8').encode(encoding)
        except UnicodeEncodeError:
            # Probably decomposed UTF-8
            return text


def compose(text):
    # Convert to NFC to make sure we can operate in non-UTF-8 locales
    # (HFS Plus uses decomposed UTF-8)
    if sys.version_info >= (3,):
        return unicodedata.normalize('NFC', text)
    else:
        return text


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('%s directory' % sys.argv[0])
        sys.exit(1)
    for name in listfiles(sys.argv[1], sys.argv[0]):
        print(name)


@@ -1707,16 +1707,53 @@
 ython 3%0A
+    if sys.version_info %3E= (3,):%0A    
     retu
@@ -1756,23 +1756,16 @@
 return %5B
-encode(
 compose(
@@ -1775,17 +1775,16 @@
 roup(1))
-)
  for m i
@@ -1806,25 +1806,16 @@
 inditer(
-%0A        
 decode(f
@@ -1826,159 +1826,148 @@
 ))%5D%0A
-%0A%0Adef decode(text):%0A    # Decode to Unicode%0A    if sys.version_info %3E= (3,):%0A        return text.decode('utf-8')%0A    else:%0A        return text
+    else:%0A        encoding = sys.stdout.encoding%0A        return %5Bencode(m.group(1), encoding) for m in FILENAME_RE.finditer(files)%5D
 %0A%0A%0Adef 
+d
 e
-n
 code
@@ -1984,18 +1984,18 @@
   # 
-En
+De
 code to 
 file
@@ -1994,121 +1994,104 @@
  to 
-filesystem encoding%0A    if sys.version_info %3E= (3,):%0A        return text%0A    else:%0A        encoding = sys.stdout.
+Unicode%0A    return text.decode('utf-8')%0A%0A%0Adef encode(text, encoding):%0A    # Encode to specified 
 enco
@@ -2091,28 +2091,24 @@
 ed encoding%0A
-    
     if encod
@@ -2131,36 +2131,32 @@
 utf-8':%0A        
- 
-   
 return text%0A    
@@ -2151,20 +2151,16 @@
 rn text%0A
-    
     try:
@@ -2156,28 +2156,24 @@
 xt%0A    try:%0A
-    
         retu
@@ -2217,20 +2217,16 @@
 ng)%0A    
- 
-   
 except U
@@ -2252,20 +2252,16 @@
         
-   
- 
 # Probab
@@ -2280,20 +2280,16 @@
 d UTF-8%0A
-    
         
@@ -2434,45 +2434,8 @@
 -8)%0A
-    if sys.version_info %3E= (3,):%0A    
     
@@ -2480,38 +2480,8 @@
 xt)%0A
-    else:%0A        return text%0A
 %0A%0Aif

Refactor crowded list comprehension.