def _run_pyflakes_analysis(self, package):
     package_path = os.path.dirname(package.__file__)
     for dir_path, dir_names, file_names in os.walk(package_path):
         for file_name in file_names:
             if file_name.endswith('.py'):
                 file_path = os.path.join(dir_path, file_name)
                 checkPath(file_path)
    def run(self, apps_locations, **options):
        output = open(os.path.join(options['output_dir'], 'pyflakes.report'), 'w')

        # run pyflakes tool with captured output
        old_stdout, pyflakes_output = sys.stdout, StringIO()
        sys.stdout = pyflakes_output
        try:
            for location in apps_locations:
                if os.path.isdir(location):
                    for dirpath, dirnames, filenames in os.walk(os.path.relpath(location)):
                        if dirpath.endswith(tuple(
                                ''.join([os.sep, exclude_dir]) for exclude_dir in options['pyflakes_exclude_dirs'])):
                            continue

                        for filename in filenames:
                            if filename.endswith('.py'):
                                pyflakes.checkPath(os.path.join(dirpath, filename))
                else:
                    pyflakes.checkPath(os.path.relpath(location))
        finally:
            sys.stdout = old_stdout

        # save report
        pyflakes_output.seek(0)

        while True:
            line = pyflakes_output.readline()
            if not line:
                break
            message = re.sub(r': ', r': [E] PYFLAKES:', line)
            output.write(message)

        output.close()
Example #3
0
    def teardown_test_environment(self, **kwargs):
        locations = get_apps_locations(self.test_labels, self.test_all)

        # run pyflakes tool with captured output
        old_stdout, pyflakes_output = sys.stdout, StringIO()
        sys.stdout = pyflakes_output
        try:
            for location in locations:
                if os.path.isdir(location):
                    for dirpath, dirnames, filenames in os.walk(relpath(location)):
                        for filename in filenames:
                            if filename.endswith('.py'):
                                pyflakes.checkPath(os.path.join(dirpath, filename))
                else:
                    pyflakes.checkPath(relpath(location))
        finally:
            sys.stdout = old_stdout

        # save report
        pyflakes_output.reset()
        while True:
            line = pyflakes_output.readline()
            if not line:
                break
            message = re.sub(r': ', r': [E] PYFLAKES:', line)
            self.output.write(message)

        self.output.close()
Example #4
0
 def directory_py_files(self, parent_directory):
     import pyflakes.scripts.pyflakes as pyflake
     directory_generator = os.walk(parent_directory)
     directory_info = directory_generator.next()
     for file in directory_info[2]:
         if file.endswith('py') and not file.startswith('__init__'):
             pyflake.checkPath('%s/%s' % (directory_info[0], file))
     for directory in directory_info[1]:
         if not directory.startswith('.') and not directory.startswith('migrations'):
             self.directory_py_files('%s/%s' % (parent_directory, directory))
Example #5
0
 def __check_path(self, path):
     old_stdout = sys.stdout
     stream = FakeStream()
     try:
         sys.stdout = stream
         for dirpath, dirnames, filenames in os.walk(path):
             for filename in filenames:
                 if filename.endswith('.py'):
                     pyflakes.checkPath(os.path.join(dirpath, filename))
     finally:
         sys.stdout = old_stdout
     stream.check()
    def test_pyflakes(self):
        """Check all the code."""
        stdout = StringIO()
        with patch('sys.stdout', stdout):
            for dirpath, _, filenames in os.walk('src'):
                for filename in filenames:
                    if filename.endswith('.py'):
                        checkPath(os.path.join(dirpath, filename))

        errors = [line.strip() for line in stdout.getvalue().splitlines()
                  if line.strip()]
        if errors:
            self.fail('\n'.join(errors))
Example #7
0
 def _run(self, path, **kwargs):
     old_stdout = sys.stdout
     stream = FakeStream(**kwargs)
     try:
         sys.stdout = stream
         for dirpath, dirnames, filenames in os.walk(path):
             for filename in filenames:
                 if _compat.PY3 and filename in ("m4a.py", "test_m4a.py"):
                     continue
                 if filename.endswith('.py'):
                     pyflakes.checkPath(os.path.join(dirpath, filename))
     finally:
         sys.stdout = old_stdout
     stream.check()
Example #8
0
 def _run(self, path):
     old_stdout = sys.stdout
     stream = StringIO()
     try:
         sys.stdout = stream
         for dirpath, dirnames, filenames in os.walk(path):
             for filename in filenames:
                 if filename.endswith('.py'):
                     pyflakes.checkPath(os.path.join(dirpath, filename))
     finally:
         sys.stdout = old_stdout
     lines = stream.getvalue()
     if lines:
         raise Exception(lines)
Example #9
0
    def __check_path(self, path):
        if not pyflakes:
            raise Exception("pyflakes missing; please install")

        old_stdout = sys.stdout
        stream = FakeStream()
        try:
            sys.stdout = stream
            for dirpath, dirnames, filenames in os.walk(path):
                for filename in filenames:
                    if filename.endswith('.py'):
                        pyflakes.checkPath(os.path.join(dirpath, filename))
        finally:
            sys.stdout = old_stdout
        stream.check()
Example #10
0
def check_pyflakes(srcdir):
    print(">>> Running pyflakes...")
    clean = True
    for pyfile in findpy(srcdir):
        if pyflakes.checkPath(pyfile) != 0:
            clean = False
    return clean
Example #11
0
File: test.py Project: wxgeo/ptyx
def audit():
    """Audit WxGeometrie's source with PyFlakes.

    Audit WxGeometrie source code for following issues:
        - Names which are used but not defined or used before they are defined.
        - Names which are redefined without having been used.
    """
    os.chdir(WXGEODIR)
    try:
        import pyflakes.scripts.pyflakes as flakes
    except ImportError:
        print("""In order to run the audit, you need to have PyFlakes installed.""")
        sys.exit(-1)
    print('\n == Auditing files... ==')
    warns = 0
    for dirpath, dirnames, filenames in os.walk('.'):
        if not any((dirpath.startswith('./' + dir) or dir + '/' == dirpath) for dir in SKIP_DIRS):
            print('\nAuditing ' + dirpath + '...')
            print([('./' + dir, dirpath.startswith('./' + dir)) for dir in SKIP_DIRS])
            for filename in filenames:
                if filename.endswith('.py') and filename != '__init__.py':
                    warns += flakes.checkPath(os.path.join(dirpath, filename))
    if warns > 0:
        print("Audit finished with total %d warnings" % warns)
    else:
        print("Audit finished without any warning. :)")
Example #12
0
    def run(self):
        import sys
        try:
            import pyflakes.scripts.pyflakes as flakes
        except ImportError:
            print "Audit requires PyFlakes installed in your system."
            sys.exit(-1)

        warns = 0
        # Define top-level directories
        dirs = ('.')
        for dir in dirs:
            for root, _, files in os.walk(dir):
                if root.startswith(('./build', './doc')):
                    continue
                for file in files:
                    if not file.endswith(('__init__.py', 'upload.py')) \
                            and file.endswith('.py'):
                        warns += flakes.checkPath(os.path.join(root, file))
        if warns > 0:
            print "Audit finished with total %d warnings." % warns
            sys.exit(-1)
        else:
            print "No problems found in sourcecode."
            sys.exit(0)
Example #13
0
 def test_missingTrailingNewline(self):
     """
     Source which doesn't end with a newline shouldn't cause any
     exception to be raised nor an error indicator to be returned by
     L{check}.
     """
     fName = fileWithContents("def foo():\n\tpass\n\t")
     self.assertFalse(checkPath(fName))
Example #14
0
 def test_checkPathNonExisting(self):
     """
     L{checkPath} handles non-existing files.
     """
     err = StringIO()
     count = withStderrTo(err, lambda: checkPath('extremo'))
     self.assertEquals(err.getvalue(), 'extremo: No such file or directory\n')
     self.assertEquals(count, 1)
Example #15
0
    def output(self):

        stderr, sys.stderr = sys.stderr, StringIO.StringIO()
        stdout, sys.stdout = sys.stdout, StringIO.StringIO()
        try:

            from pyflakes.scripts import pyflakes
            pyflakes.checkPath(self.path)

            errors = sys.stderr.getvalue().strip()
            if errors:
                return errors
            else:
                output = sys.stdout.getvalue().strip()
                return output

        finally:
            sys.stderr = stderr
            sys.stdout = stdout
Example #16
0
def Pyflakes(path, modules):
    try:
        from pyflakes.scripts.pyflakes import checkPath
    except ImportError:
        print("PYFLAKES not installed. Skipping.")
        return
    warnings=0
    for m in modules:
        warnings+=checkPath(os.path.join(path,m))
    print("%d warnings occurred in pyflakes check" % warnings)
Example #17
0
 def test_permissionDenied(self):
     """
     If the a source file is not readable, this is reported on standard
     error.
     """
     sourcePath = fileWithContents('')
     os.chmod(sourcePath, 0)
     err = StringIO()
     count = withStderrTo(err, lambda: checkPath(sourcePath))
     self.assertEquals(count, 1)
     self.assertEquals(
         err.getvalue(), "%s: Permission denied\n" % (sourcePath,))
Example #18
0
    def test_misencodedFile(self):
        """
        If a source file contains bytes which cannot be decoded, this is
        reported on stderr.
        """
        source = """\
# coding: ascii
x = "\N{SNOWMAN}"
""".encode('utf-8')
        sourcePath = fileWithContents(source)
        err = StringIO()
        count = withStderrTo(err, lambda: checkPath(sourcePath))
        self.assertEquals(count, 1)
        self.assertEquals(
            err.getvalue(), "%s: problem decoding source\n" % (sourcePath,))
Example #19
0
 def run(self):
     import os
     try:
         import pyflakes.scripts.pyflakes as flakes
     except ImportError:
         print("In order to run the audit, you need to have PyFlakes installed.")
         sys.exit(-1)
     dirs = (os.path.join(*d) for d in (m.split('.') for m in modules))
     warns = 0
     for dir in dirs:
         for filename in os.listdir(dir):
             if filename.endswith('.py') and filename != '__init__.py':
                 warns += flakes.checkPath(os.path.join(dir, filename))
     if warns > 0:
         print("Audit finished with total %d warnings" % warns)
    def test_checkPathNonExisting(self):
        """
        L{checkPath} handles non-existing files.
        """
        err = StringIO()
        try:

            def mock_open(*k):
                raise IOError(None, "No such file or directory")

            pyflakes.open = mock_open
            count = checkPath("extremo", stderr=err)
        finally:
            del pyflakes.open
        self.assertEquals(err.getvalue(), "extremo: No such file or directory\n")
        self.assertEquals(count, 1)
Example #21
0
    def run(self):
        import os

        try:
            import pyflakes.scripts.pyflakes as flakes
        except:
            print """In order to run the audit, you need to have PyFlakes installed."""
            sys.exit(-1)
        dirs = [os.path.join(*i.split(".")) for i in modules]
        warns = 0
        for dir in dirs:
            filenames = os.listdir(dir)
            for filename in filenames:
                if filename.endswith(".py") and filename != "__init__.py":
                    warns += flakes.checkPath(os.path.join(dir, filename))
        if warns > 0:
            print ("Audit finished with total %d warnings" % warns)
Example #22
0
    def test_eofSyntaxError(self):
        """
        The error reported for source files which end prematurely causing a
        syntax error reflects the cause for the syntax error.
        """
        source = "def foo("
        sourcePath = fileWithContents(source)
        err = StringIO()
        count = withStderrTo(err, lambda: checkPath(sourcePath))
        self.assertEqual(count, 1)
        self.assertEqual(
            err.getvalue(),
            """\
%s:1: unexpected EOF while parsing
def foo(
         ^
""" % (sourcePath,))
Example #23
0
    def run(self):
        import os

        try:
            import pyflakes.scripts.pyflakes as flakes
        except ImportError:
            print("In order to run the audit, you need to have PyFlakes installed.")
            sys.exit(-1)
        # We don't want to audit external dependencies
        ext = ("mpmath",)
        dirs = (os.path.join(*d) for d in (m.split(".") for m in modules) if d[1] not in ext)
        warns = 0
        for dir in dirs:
            for filename in os.listdir(dir):
                if filename.endswith(".py") and filename != "__init__.py":
                    warns += flakes.checkPath(os.path.join(dir, filename))
        if warns > 0:
            print("Audit finished with total %d warnings" % warns)
    def test_permissionDenied(self):
        """
        If the a source file is not readable, this is reported on standard
        error.
        """
        err = StringIO()
        try:

            def mock_open(*k):
                raise IOError(None, "Permission denied")

            pyflakes.open = mock_open
            count = checkPath("dummy.py", stderr=err)
        finally:
            del pyflakes.open

        self.assertEquals(count, 1)
        self.assertEquals(err.getvalue(), "dummy.py: Permission denied\n")
Example #25
0
    def run(self):
        try:
            import pyflakes.scripts.pyflakes as flakes
        except ImportError:
            print "Audit requires PyFlakes installed in your system."
            sys.exit(-1)

        warns = 0
        # Define top-level directories
        dirs = 'deenurp',
        for dir in dirs:
            for root, _, files in os.walk(dir):
                for file in files:
                    if file != '__init__.py' and file.endswith('.py'):
                        warns += flakes.checkPath(os.path.join(root, file))
        if warns > 0:
            print "Audit finished with total %d warnings." % warns
        else:
            print "No problems found in sourcecode."
Example #26
0
    def test_load(self):
        for module in modules:
            loaded = 0

            # try to compile the module
            try:
                __import__(module)
                loaded = 1
            except:
                pass

            self.assertTrue(loaded, "loading %s" % module)

            # check the module with pyflakes, if available
            if loaded and have_pyflakes:
                path = re.sub('c$', '', sys.modules[module].__file__)
                has_flakes = checkPath(path)
                self.assertTrue(not has_flakes,
                    "checking %s with pyflakes" % module)
Example #27
0
    def run(self):
        import os, sys
        try:
            import pyflakes.scripts.pyflakes as flakes
        except ImportError:
            print "Audit requires PyFlakes installed in your system."""
            sys.exit(-1)

        dirs = ['fedex', 'tests']
        # Add example directories
        #dirs.append(os.path.join('examples', dir))
        warns = 0
        for dir in dirs:
            for filename in os.listdir(dir):
                if filename.endswith('.py') and filename != '__init__.py':
                    warns += flakes.checkPath(os.path.join(dir, filename))
        if warns > 0:
            print ("Audit finished with total %d warnings." % warns)
        else:
            print ("No problems found in sourcecode.")
Example #28
0
 def run(self):
     import os
     try:
         import pyflakes.scripts.pyflakes as flakes
     except ImportError:
         print(
             "In order to run the audit, you need to have PyFlakes installed."
         )
         sys.exit(-1)
     # We don't want to audit external dependencies
     ext = ('mpmath', )
     dirs = (os.path.join(*d) for d in (m.split('.') for m in modules)
             if d[1] not in ext)
     warns = 0
     for dir in dirs:
         for filename in os.listdir(dir):
             if filename.endswith('.py') and filename != '__init__.py':
                 warns += flakes.checkPath(os.path.join(dir, filename))
     if warns > 0:
         print("Audit finished with total %d warnings" % warns)
Example #29
0
    def run(self):
        import os, sys
        try:
            import pyflakes.scripts.pyflakes as flakes
        except ImportError:
            print("Audit requires PyFlakes installed in your system.")
            sys.exit(-1)

        warns = 0
        # Define top-level directories
        dirs = ('emails', 'scripts')
        for dir in dirs:
            for root, _, files in os.walk(dir):
                for file in files:
                    if file != '__init__.py' and file.endswith('.py'):
                        warns += flakes.checkPath(os.path.join(root, file))
        if warns > 0:
            print("Audit finished with total %d warnings." % warns)
        else:
            print("No problems found in sourcecode.")
Example #30
0
    def test_nonKeywordAfterKeywordSyntaxError(self):
        """
        Source which has a non-keyword argument after a keyword argument should
        include the line number of the syntax error.  However these exceptions
        do not include an offset.
        """
        source = """\
foo(bar=baz, bax)
"""
        sourcePath = FilePath(self.mktemp())
        sourcePath.setContent(source)
        err = StringIO()
        count = withStderrTo(err, lambda: checkPath(sourcePath.path))
        self.assertEqual(count, 1)
        self.assertEqual(
            err.getvalue(),
            """\
%s:1: non-keyword arg after keyword arg
foo(bar=baz, bax)
""" % (sourcePath.path,))
Example #31
0
    def test_nonKeywordAfterKeywordSyntaxError(self):
        """
        Source which has a non-keyword argument after a keyword argument should
        include the line number of the syntax error.  However these exceptions
        do not include an offset.
        """
        source = """\
foo(bar=baz, bax)
"""
        sourcePath = fileWithContents(source)
        err = StringIO()
        count = withStderrTo(err, lambda: checkPath(sourcePath))
        self.assertEqual(count, 1)
        self.assertEqual(
            err.getvalue(),
            """\
%s:1: non-keyword arg after keyword arg
foo(bar=baz, bax)
             ^
""" % (sourcePath,))
Example #32
0
    def test_nonDefaultFollowsDefaultSyntaxError(self):
        """
        Source which has a non-default argument following a default argument
        should include the line number of the syntax error.  However these
        exceptions do not include an offset.
        """
        source = """\
def foo(bar=baz, bax):
    pass
"""
        sourcePath = fileWithContents(source)
        err = StringIO()
        count = withStderrTo(err, lambda: checkPath(sourcePath))
        self.assertEqual(count, 1)
        self.assertEqual(
            err.getvalue(),
            """\
%s:1: non-default argument follows default argument
def foo(bar=baz, bax):
        ^
""" % (sourcePath,))
Example #33
0
    def test_nonDefaultFollowsDefaultSyntaxError(self):
        """
        Source which has a non-default argument following a default argument
        should include the line number of the syntax error.  However these
        exceptions do not include an offset.
        """
        source = """\
def foo(bar=baz, bax):
    pass
"""
        sourcePath = FilePath(self.mktemp())
        sourcePath.setContent(source)
        err = StringIO()
        count = withStderrTo(err, lambda: checkPath(sourcePath.path))
        self.assertEqual(count, 1)
        self.assertEqual(
            err.getvalue(),
            """\
%s:1: non-default argument follows default argument
def foo(bar=baz, bax):
""" % (sourcePath.path,))
Example #34
0
    def run(self):
        try:
            import pyflakes.scripts.pyflakes as flakes
        except ImportError:
            print "Audit requires PyFlakes installed in your system." ""
            sys.exit(-1)

        dirs = ['curl_proxies_checker']
        # Add example directories
        for dir in []:
            dirs.append(os.path.join('examples', dir))
        # TODO: Add test subdirectories
        warns = 0
        for dir in dirs:
            for filename in os.listdir(dir):
                if filename.endswith('.py') and filename != '__init__.py':
                    warns += flakes.checkPath(os.path.join(dir, filename))
        if warns > 0:
            print("Audit finished with total %d warnings." % warns)
        else:
            print("No problems found in sourcecode.")
Example #35
0
    def run(self):
        import os, sys

        try:
            import pyflakes.scripts.pyflakes as flakes
        except ImportError:
            print("Audit requires PyFlakes installed in your system.")
            sys.exit(-1)

        warns = 0
        # Define top-level directories
        dirs = ("flask", "examples", "scripts")
        for dir in dirs:
            for root, _, files in os.walk(dir):
                for file in files:
                    if file != "__init__.py" and file.endswith(".py"):
                        warns += flakes.checkPath(os.path.join(root, file))
        if warns > 0:
            print("Audit finished with total %d warnings." % warns)
        else:
            print("No problems found in sourcecode.")
Example #36
0
    def test_multilineSyntaxError(self):
        """
        Source which includes a syntax error which results in the raised
        L{SyntaxError.text} containing multiple lines of source are reported
        with only the last line of that source.
        """
        source = """\
def foo():
    '''

def bar():
    pass

def baz():
    '''quux'''
"""

        # Sanity check - SyntaxError.text should be multiple lines, if it
        # isn't, something this test was unprepared for has happened.
        def evaluate(source):
            exec(source)

        try:
            evaluate(source)
        except SyntaxError as e:
            self.assertTrue(e.text.count('\n') > 1)
        else:
            self.fail()

        sourcePath = fileWithContents(source)
        err = StringIO()
        count = withStderrTo(err, lambda: checkPath(sourcePath))
        self.assertEqual(count, 1)

        self.assertEqual(
            err.getvalue(), """\
%s:8: invalid syntax
    '''quux'''
           ^
""" % (sourcePath, ))
Example #37
0
def _check_file(f):
    with capture_output() as (o, e):
        pyflakes.checkPath(f)
    return o.getvalue().splitlines()
Example #38
0
import os, sys
try:
    import pyflakes.scripts.pyflakes as flakes
except ImportError:
    print "Missing dependency: PyFlakes"
    sys.exit(-1)

print

warnings = 0
for dir, subdirs, files in os.walk(
        os.path.join(os.path.dirname(os.path.abspath(__file__)), '../edacc')):
    for filename in files:
        if filename.endswith('.py'):
            warnings += flakes.checkPath(os.path.join(dir, filename))

if warnings > 0:
    print "%d warnings" % warnings
else:
    print "No problems found"