diff options
author | Timothy Edmund Crosley <timothy.crosley@gmail.com> | 2017-10-30 00:29:57 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-30 00:29:57 -0700 |
commit | fdcaad1ab3ff0c6349d8b66444c82ced2f81616b (patch) | |
tree | 11c8f83108d5b66255b938b7c8b1529b6a4c1e8f | |
parent | bb64a644dcd91b47a18bcd4ebb1b9c8f84ac3b79 (diff) | |
parent | 33845851c7155cf81f9c71638e2fd4a556b92c17 (diff) | |
download | isort-fdcaad1ab3ff0c6349d8b66444c82ced2f81616b.tar.gz |
Merge pull request #604 from jdufresne/scripts
In iter_source_code() include Python scripts without an extension
-rwxr-xr-x | isort/main.py | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/isort/main.py b/isort/main.py index 9a2e49b7..0c409bc4 100755 --- a/isort/main.py +++ b/isort/main.py @@ -23,6 +23,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera import argparse import glob import os +import re import sys import setuptools @@ -32,7 +33,6 @@ from isort.settings import DEFAULT_SECTIONS, default, from_path, should_skip from .pie_slice import itemsview - INTRO = r""" /#######################################################################\ @@ -56,6 +56,17 @@ INTRO = r""" \########################################################################/ """.format(__version__) +shebang_re = re.compile(br'^#!.*\bpython[23w]?\b') + + +def is_python_file(path): + if path.endswith('.py'): + return True + + with open(path, 'rb') as fp: + line = fp.readline(100) + return bool(shebang_re.match(line)) + def iter_source_code(paths, config, skipped): """Iterate over all Python source files defined in paths.""" @@ -71,11 +82,12 @@ def iter_source_code(paths, config, skipped): skipped.append(dirname) dirnames.remove(dirname) for filename in filenames: - if filename.endswith('.py'): + filepath = os.path.join(dirpath, filename) + if is_python_file(filepath): if should_skip(filename, config, dirpath): skipped.append(filename) else: - yield os.path.join(dirpath, filename) + yield filepath else: yield path |