#!/usr/bin/env python
"""
Test that autopep8 runs without crashing on various Python files.
"""
import os
import sys
import subprocess
import tempfile


def run(filename, log_file, fast_check=False, passes=2000,
        ignore=''):
    """Run autopep8 on file at filename.
    Return True on success.
    """
    ignore_option = '--ignore=' + ignore

    autopep8_path = os.path.split(os.path.abspath(
            os.path.dirname(__file__)))[0]
    autoppe8_bin = os.path.join(autopep8_path, 'autopep8.py')
    command = [autoppe8_bin, '--pep8-passes={p}'.format(p=passes),
               ignore_option, filename]

    if fast_check:
        if 0 != subprocess.call(command + ['--diff'], stderr=log_file):
            log_file.write('autopep8 crashed on ' + filename + '\n')
            return False
    else:
        with tempfile.NamedTemporaryFile(suffix='.py') as tmp_file:
            if 0 != subprocess.call(command, stdout=tmp_file, stderr=log_file):
                log_file.write('autopep8 crashed on ' + filename + '\n')
                return False

            if 0 != subprocess.call(['pep8', ignore_option,
                                     '--show-source', tmp_file.name],
                                    stderr=tmp_file):
                log_file.write('autopep8 did not completely fix ' +
                               filename + '\n')

            if _check_syntax(filename) and not _check_syntax(tmp_file.name):
                log_file.write('autopep8 broke ' + filename + '\n')
                return False

    return True


def _detect_encoding(filename):
    """Return file encoding."""
    try:
        # Python 3
        try:
            with open(filename, 'rb') as input_file:
                import tokenize
                encoding = tokenize.detect_encoding(input_file.readline)[0]

            # Check for correctness of encoding
            with open(filename, encoding=encoding) as input_file:
                input_file.read()

            return encoding
        except (SyntaxError, LookupError, UnicodeDecodeError):
            return 'latin-1'
    except AttributeError:
        return 'utf-8'


def _open_with_encoding(filename, encoding, mode='r'):
    """Open file with a specific encoding."""
    try:
        # Python 3
        return open(filename, mode=mode, encoding=encoding)
    except TypeError:
        return open(filename, mode=mode)


def _check_syntax(filename):
    """Return True if syntax is okay."""
    with _open_with_encoding(filename, _detect_encoding(filename)) as f:
        try:
            compile(f.read(), '<string>', 'exec')
            return True
        except (SyntaxError, TypeError):
            return False


def main():
    import optparse
    parser = optparse.OptionParser()
    parser.add_option('--fast-check', action='store_true',
                      help='ignore incomplete PEP8 fixes and broken files')
    parser.add_option('--log-errors',
                      help='log autopep8 errors instead of exiting')
    parser.add_option('--ignore',
                      help='comma-separated errors to ignore',
                      default='')
    opts, args = parser.parse_args()

    if opts.log_errors:
        log_file = open(opts.log_errors, 'w')
    else:
        log_file = sys.stderr

    if args:
        dir_paths = args
        for d in dir_paths:
            if not os.path.isdir(d):
                sys.stderr.write(d + ' is not a directory\n')
                sys.exit(1)
    else:
        dir_paths = sys.path

    try:
        for p in dir_paths:
            for root, dirnames, filenames in os.walk(p):
                import fnmatch
                for f in fnmatch.filter(filenames, '*.py'):
                    sys.stderr.write('--->  Testing with ' + f + '\n')

                    if not run(os.path.join(root, f),
                            log_file=log_file,
                            fast_check=opts.fast_check,
                            ignore=opts.ignore):
                        if not opts.log_errors:
                            sys.exit(1)
    finally:
        # Only close if it is an actual log file
        if opts.log_errors:
            log_file.close()


if __name__ == '__main__':
    main()

@@ -3325,236 +3325,184 @@
     
-    for d in dir_paths:%0A            if not os.path.isdir(d):%0A                sys.stderr.write(d + ' is not a directory%5Cn')%0A                sys.exit(1)%0A    else:%0A        dir_paths = sys.path%0A%0A    try:%0A        for p in dir_paths:%0A
+else:%0A        dir_paths = sys.path%0A%0A    try:%0A        filenames = dir_paths%0A        while filenames:%0A            name = filenames.pop(0)%0A            if os.path.isdir(name):%0A    
     
@@ -3523,27 +3523,19 @@
 ot, 
-dirnames, filenames
+_, children
  in 
@@ -3542,17 +3542,20 @@
 os.walk(
-p
+name
 ):%0A     
@@ -3569,83 +3569,123 @@
     
-import fnmatch%0A                for f in fnmatch.filter(filenames, '*
+    filenames += %5Bos.path.join(root, f) for f in children%0A                                  if f.endswith('
 .py')
-:%0A
+%5D%0A 
     
@@ -3683,32 +3683,38 @@
 ')%5D%0A            
+else:%0A
          sys.std
@@ -3702,24 +3702,31 @@
 e:%0A         
+       
 sys.stderr.w
@@ -3754,17 +3754,20 @@
 ith ' + 
-f
+name
  + '%5Cn')
@@ -3780,28 +3780,24 @@
             
-    
 if not run(o
@@ -3812,22 +3812,15 @@
 oin(
-root, f),%0A    
+name),%0A
     
@@ -3882,20 +3882,16 @@
         
-    
 fast_che
@@ -3930,28 +3930,24 @@
             
-    
 ignore=opts.
@@ -3951,28 +3951,24 @@
 ts.ignore):%0A
-    
             
@@ -3991,36 +3991,32 @@
 pts.log_errors:%0A
-    
                 

Modify to allow passing in filenames