Exemple #1
0
 def __call__(self, txn, path):
     if txn.change_kind(path) == 'D':
         return (True, "No check on deleted files")
     self.log.debug("check %s for tabs", path)
     name = None
     try:
         import tabnanny
         data = txn.file_contents(path)
         (fd, name) = mkstemp('.py')
         f = os.fdopen(fd, 'w')
         f.write(data)
         f.close()
         sys.stdout, sys.stderr = StringIO(), StringIO()
         tabnanny.check(name)
         out, err = sys.stdout.getvalue(), sys.stderr.getvalue()
         if out or err:
             report = err
             if out:
                 report += 'Wrong use of tabs: ' + out
             result = (False, report.replace(name, path))
         else:
             result = (True, "Valid Python tabs in '%s'" % path)
     finally:
         # clean up
         sys.stdout, sys.stderr = sys.__stdout__, sys.__stderr__
         if name and os.path.exists(name):
             os.remove(name)
     return result
Exemple #2
0
def main():
	"""
	Can receive multiple arguments, all of which are assumed to be python
	files.
	Perform the following checks:
		1) Compile Python files
		2) Tabnanny Python files
	"""

	# get arguments
	if len(sys.argv) == 2 and sys.argv[1] == "--help":
		print_usage()
		sys.exit(0)
	elif len(sys.argv) < 2:
		print 'At least 2 arguments are required. Received: %d arguments' % len(sys.argv)
		print_usage()
		sys.exit(1)

	# check all files
	for index in range(1, len(sys.argv)):

		fileToCheck = sys.argv[index]

		# make sure the file exists
		if not os.path.exists(fileToCheck):
			print "Unable to find file %s!" % fileToCheck
			continue

		# compile the file
		print "compile %s...." % fileToCheck
		py_compile.compile(fileToCheck)

		# perform tab check on file
		print "tabnanny %s...." % fileToCheck
		tabnanny.check(fileToCheck)
    def test_correct_directory_verbose(self):
        """Directory containing few error free python source code files.

        Because order of files returned by `os.lsdir()` is not fixed, verify the
        existence of each output lines at `stdout` using `in` operator.
        `verbose` mode of `tabnanny.verbose` asserts `stdout`.
        """
        with tempfile.TemporaryDirectory() as tmp_dir:
            lines = [
                f"{tmp_dir!r}: listing directory\n",
            ]
            file1 = TemporaryPyFile(SOURCE_CODES["error_free"],
                                    directory=tmp_dir)
            file2 = TemporaryPyFile(SOURCE_CODES["error_free"],
                                    directory=tmp_dir)
            with file1 as file1_path, file2 as file2_path:
                for file_path in (file1_path, file2_path):
                    lines.append(f"{file_path!r}: Clean bill of health.\n")

                tabnanny.verbose = 1
                with captured_stdout() as stdout, captured_stderr() as stderr:
                    tabnanny.check(tmp_dir)
                stdout = stdout.getvalue()
                for line in lines:
                    with self.subTest(line=line):
                        self.assertIn(line, stdout)
                self.assertEqual(stderr.getvalue(), "")
Exemple #4
0
def RunTabNanny(background, filename):
    import cStringIO
    import tabnanny

    # Capture the tab-nanny output
    newout = cStringIO.StringIO()
    old_out = sys.stderr, sys.stdout
    sys.stderr = sys.stdout = newout
    try:
        tabnanny.check(filename)
    finally:
        # Restore output
        sys.stderr, sys.stdout = old_out
    data = newout.getvalue()
    if data:
        try:
            lineno = data.split()[1]
            lineno = int(lineno)
            _JumpToPosition(background, filename, lineno)
            try:  # Try and display whitespace
                #GetActiveEditControl().SCISetViewWS(1)
                pass
            except:
                pass
            #win32ui.SetStatusText("The TabNanny found trouble at line %d" % lineno)
            background.statusBar.text = "The TabNanny found trouble at line %d" % lineno
        except (IndexError, TypeError, ValueError):
            background.statusBar.text = "The tab nanny complained, but I cant see where!"
            print data
        return 0
    return 1
Exemple #5
0
def tnanny(path):
    file = open(path)
    for line in file.readlines():
        print repr(line)

    # let tabnanny look at it
    tabnanny.check(path)
Exemple #6
0
 def test_tabnanny(self):
     """ Tabnanny check """
     self.longMessage = False
     try:
         tabnanny.check(filename)
     except tabnanny.NannyNag:
         self.fail("File %s fails tabnanny.check." % filename)
Exemple #7
0
    def test_indentation(self):
        capture = Capture()
        capture.start()
        tabnanny.verbose = 0
        tabnanny.check(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..')))
        capture.stop()

        for line in capture.output():
            raise IndentationError('Ambiguous indentation detected in %s on line %s' % (line[0], line[1]))
Exemple #8
0
def tabcheck(options):
    """Run tabnanny against the current module.
    """
    args = getattr(options, 'args', [])
    if not args:
        args = path('PyMOTW').glob('*')
    tabnanny.verbose = 1
    for module in args:
        tabnanny.check(module)
    return
def tabcheck(options):
    """Run tabnanny against the current module.
    """
    args = getattr(options, 'args', [])
    if not args:
        args = path('PyMOTW').glob('*')
    tabnanny.verbose = 1
    for module in args:
        tabnanny.check(module)
    return
def process_path(p):
    stdout = sys.stdout
    output = ''
    try:
        sys.stdout = StringIO.StringIO()
        tabnanny.check(p)
        output = sys.stdout.getvalue()
    finally:
        sys.stdout = stdout
    return output
Exemple #11
0
 def run(string: str):
     """
     Run tabnanny if path exists.
     :param string:
     :return:
     """
     if allowed(string):
         path = Path(string)
         if path.exists():
             tabnanny.check(path)
Exemple #12
0
def process_path(p):
    stdout = sys.stdout
    output = ''
    try:
        sys.stdout = StringIO.StringIO()
        tabnanny.check(p)
        output = sys.stdout.getvalue()
    finally:
        sys.stdout = stdout
    return output
Exemple #13
0
    def verify_tabnanny_check(self, dir_or_file, out="", err=""):
        """Common verification for tabnanny.check().

        Use this method to assert expected values of `stdout` and `stderr` after
        running tabnanny.check() on given `dir` or `file` path. Because
        tabnanny.check() captures exceptions and writes to `stdout` and
        `stderr`, asserting standard outputs is the only way.
        """
        with captured_stdout() as stdout, captured_stderr() as stderr:
            tabnanny.check(dir_or_file)
        self.assertEqual(stdout.getvalue(), out)
        self.assertEqual(stderr.getvalue(), err)
Exemple #14
0
def main(argv):
    if len(argv[1:]) != 1:
        raise PyCompileError("Incorrect number of arguments: %r" % argv[1:])

    # Read in the file contents.
    filename = argv[1]
    fin = open(filename, 'r')
    s = fin.read()
    fin.close()

    # Get around a bug in python's compile() code on Unix that does not
    # like lines ending in CRLF.
    if not sys.platform[:3] == "win":
        s = s.replace('\r\n', '\n')

    # Get around bug in python's compile() code that requires terminal newlines
    s = s + '\n' 

    results = []

    # Run tabnanny on it if available.
    try:
        import tabnanny
    except ImportError:
        pass
    else:
        import StringIO
        o = StringIO.StringIO()
        oldStdout = sys.stdout
        sys.stdout = o
        try:
            tabnanny.check(filename)
        except IndentationError, e:
            pass # ignore these, we'll catch them later
        sys.stdout = oldStdout
        lineRe = re.compile("^(?P<fname>.*?) (?P<line>\d+) '(?P<content>.*)'$")
        for line in _splitlines(o.getvalue()):
            match = lineRe.match(line)
            if match:
                r = {}
                r["filename"] = filename
                r["description"] = "Ambiguous indentation (mixed tabs and spaces?)"
                r["lineno"] = int(match.group("line"))
                r["offset"] = None
                r["severity"] = "WARNING"
                r["text"] = match.group("content")
                results.append(r)
            else:
                errmsg = "Warning: could not parse tabnanny "\
                         "output line: %r\n" % line
Exemple #15
0
def tests_generator_per_file(python_file, debug):
    print(f'Run test generation for {python_file}')
    # run TestSetCreator to get list of expected test files
    append = False
    # validate '.py' file - check intends
    tabnanny.check(python_file)
    # run analyzer
    with open(python_file, "r") as source:
        source_massive = source.read()
        tree = parse(source_massive)
    try:
        an = Analyzer(source_massive, debug)
        an.visit(tree)
        an.report()
    except:
        print('Analyzer error during ')
    print(f'Generate file: {python_file}')
    cg = CodeGraph(paths=[python_file])
    # data with entities end line no and start line no
    code_lines = cg.get_lines_numbers()[python_file]

    # to get diff with existed tests
    signatures_list = {'classes': [], 'def': []}
    for class_ in an.tree['classes']:
        signatures_list['classes'].append(test_method_prefix + class_['name'])

    # run test file generator
    tf_content = generate_test_file_content(an, python_file, code_lines, debug)
    if tf_content.endswith('import '):
        return
    if append:
        # append new tests to tf
        # if new method in test case for class - insert
        pass

    # TODO: need to change on getting prefix from command line and config
    prefix = 'tests'
    if prefix in python_file:
        tests_dir = os.path.join(python_file.split(prefix)[0], prefix)
    else:
        tests_dir = os.path.join(os.path.dirname(os.path.dirname(python_file)),
                                 'tests')
    os.makedirs(tests_dir, exist_ok=True)
    test_file_path = dump_to_file(python_file, tf_content, tests_dir)
    q.put(test_file_path)
Exemple #16
0
def update(options):
    """Run cog against the named module, then re-build the HTML.
    
    Examples::
    
      $ paver update atexit
    """
    options.order('update', 'sphinx', add_rest=True)
    args = getattr(options, 'args', [])
    if args:
        module = args[0]
    else:
        module = MODULE
    module_dir = 'PyMOTW/' + module
    tabnanny.check(module_dir)
    options.order('cog', 'sphinx', add_rest=True)
    options.args = [module_dir]
    cog(options)
    html(options)
    return
Exemple #17
0
def update(options):
    """Run cog against the named module, then re-build the HTML.

    Examples::

      $ paver update atexit
    """
    options.order('update', 'sphinx', add_rest=True)
    args = getattr(options, 'args', [])
    if args:
        module = args[0]
    else:
        module = MODULE
    module_dir = 'PyMOTW/' + module
    tabnanny.check(module_dir)
    options.order('cog', 'sphinx', add_rest=True)
    options.args = [module_dir]
    cog(options)
    html(options)
    return
Exemple #18
0
    def test_correct_directory_verbose(self):
        """Directory containing few error free python source code files.

        Because order of files returned by `os.lsdir()` is not fixed, verify the
        existence of each output lines at `stdout` using `in` operator.
        `verbose` mode of `tabnanny.verbose` asserts `stdout`.
        """
        with tempfile.TemporaryDirectory() as tmp_dir:
            lines = [f"{tmp_dir!r}: listing directory\n",]
            file1 = TemporaryPyFile(SOURCE_CODES["error_free"], directory=tmp_dir)
            file2 = TemporaryPyFile(SOURCE_CODES["error_free"], directory=tmp_dir)
            with file1 as file1_path, file2 as file2_path:
                for file_path in (file1_path, file2_path):
                    lines.append(f"{file_path!r}: Clean bill of health.\n")

                tabnanny.verbose = 1
                with captured_stdout() as stdout, captured_stderr() as stderr:
                    tabnanny.check(tmp_dir)
                stdout = stdout.getvalue()
                for line in lines:
                    with self.subTest(line=line):
                        self.assertIn(line, stdout)
                self.assertEqual(stderr.getvalue(), "")
Exemple #19
0
def tests_generator_per_file(python_file):
    print(f'Run test generation for {python_file}')
    # run TestSetCreator to get list of expected test files
    append = False
    # validate '.py' file - check intends
    tabnanny.check(python_file)
    # run analyzer
    with open(python_file, "r") as source:
        source_massive = source.read()
        tree = parse(source_massive)
    an = Analyzer(source_massive)
    an.visit(tree)
    an.report()

    # to get diff with existed tests
    signatures_list = {'classes': [], 'def': []}
    for class_ in an.tree['classes']:
        signatures_list['classes'].append(test_method_prefix + class_['name'])

    # run test file generator
    tf_content = generate_test_file_content(an, python_file)
    if append:
        # append new tests to tf
        # if new method in test case for class - insert
        pass

    # TODO: need to change on getting prefix from command line and config
    prefix = 'tests'
    if prefix in python_file:
        tests_dir = os.path.join(python_file.split(prefix)[0], prefix)
    else:
        tests_dir = os.path.join(os.path.dirname(os.path.dirname(python_file)),
                                 'tests')
    os.makedirs(tests_dir, exist_ok=True)
    test_file_path = dump_to_file(python_file, tf_content, tests_dir)
    q.put(test_file_path)
Exemple #20
0
    except:
        traceback.print_exc()
        _HandlePythonFailure(what, status=status, jump=jump)


def RunTabNanny(filename, status=smPrintStatus, jump=smJumpToPosition):
    try:
        import cStringIO, tabnanny
    except Exception, message:
        print message
    # Capture the tab-nanny output
    newout = cStringIO.StringIO()
    old_out = sys.stderr, sys.stdout
    sys.stderr = sys.stdout = newout
    try:
        tabnanny.check(filename)
    finally:
        # Restore output
        sys.stderr, sys.stdout = old_out
    data = newout.getvalue()
    if data:
        try:
            lineno = string.split(data)[1]
            lineno = int(lineno)
            status("The TabNanny found trouble at line %d" % lineno)
            jump(filename, lineno)
        except (IndexError, TypeError, ValueError):
            print "The tab nanny complained, but I cant see where!"
            print data
        return 0
    return 1
Exemple #21
0
 def    testTabNanny(self):
     """Invoking the tabnanny"""
     import tabnanny
     self.assertFalse(tabnanny.check("." + os.sep + 'thoonk'))
Exemple #22
0
 def update_event(self, inp=-1):
     self.set_output_val(0, tabnanny.check(self.input(0)))
Exemple #23
0
def main(argv):
    if len(argv[1:]) != 1:
        raise PyCompileError("Incorrect number of arguments: %r" % argv[1:])

    # Read in the file contents.
    filename = argv[1]
    fin = open(filename, 'r')
    s = fin.read()
    fin.close()

    # Get around a bug in python's compile() code on Unix that does not
    # like lines ending in CRLF.
    if not sys.platform[:3] == "win":
        s = s.replace('\r\n', '\n')

    # Get around bug in python's compile() code that requires terminal newlines
    s = s + '\n' 

    results = []

    # Run tabnanny on it if available.
    try:
        import tabnanny
    except ImportError:
        pass
    else:
        import io
        o = io.StringIO()
        oldStdout = sys.stdout
        sys.stdout = o
        try:
            tabnanny.check(filename)
        except IndentationError as e:
            pass # ignore these, we'll catch them later
        sys.stdout = oldStdout
        lineRe = re.compile("^(?P<fname>.*?) (?P<line>\d+) '(?P<content>.*)'$")
        for line in _splitlines(o.getvalue()):
            match = lineRe.match(line)
            if match:
                r = {}
                r["filename"] = filename
                r["description"] = "Ambiguous indentation (mixed tabs and spaces?)"
                r["lineno"] = int(match.group("line"))
                r["offset"] = None
                r["severity"] = "WARNING"
                r["text"] = match.group("content")
                results.append(r)
            else:
                errmsg = "Warning: could not parse tabnanny "\
                         "output line: %r\n" % line
                #XXX Silently drop it for now because stderr is used for
                #    data as well.

    # Compile it.
    try:
        dummy = builtins.compile(s, filename, 'exec')
    except SyntaxError as ex:
        r = {}
        r["filename"] = filename
        # Prefix the description with the name of the exception class.
        # This is what Python does when it prints tracebacks.
        eClassName = ex.__class__.__name__
        r["description"] = "%s: %s" % (eClassName, ex.msg)
        r["lineno"] = ex.lineno
        # ex.offset is sometime unreliable. For example ex.offset for the
        # syntax error in this code:
        #      foo = """
        #      line1
        #      line2
        # is 21. Looking at ex.text shows us the problem:
        #      'foo = """\012line1\012line2'
        # We need to recalculate the offset if there are newlines in ex.text.
        if '\n' in ex.text:
            lines = _splitlines(ex.text, 1)
            for line in lines[:-1]:
                ex.offset = ex.offset - len(line)
            ex.text = lines[-1]
        r["offset"] = ex.offset
        try:
            if isinstance(ex, TabError):
                r["severity"] = "WARNING"
            else:
                r["severity"] = "ERROR"
        except NameError:
            # Python 1.5.2 doesn't have TabError
            r["severity"] = "ERROR"
        r["text"] = ex.text
        results.append(r)

    # Print the results.
    pprint.pprint(results)
Exemple #24
0
import tabnanny

FILE = "samples/badtabs.py"

file = open(FILE)
for line in file.readlines():
    print(repr(line))

# let tabnanny look at it
tabnanny.check(FILE)

## 'if 1:\012'
## '    \011print "hello"\012'
## '        print "world"\012'
## samples/badtabs.py 3 '        print "world"\012'
Exemple #25
0
from distutils.core import setup
import sys

try:
    import tabnanny
except ImportError:
    pass
else:
    import StringIO
    stdout, stderr = sys.stdout, sys.stderr
    sys.stdout = co = StringIO.StringIO()
    sys.stderr = ce = StringIO.StringIO()
    # Tabnanny doesn't provide any mechanism other than print outs so we have
    # to capture the output
    tabnanny.check("_unitTestHelpers")
    sys.stdout = stdout
    sys.stderr = stderr
    if len(co.getvalue().strip()) != 0:
        print "Incosistent tab usage:"
        print co.getvalue()
        sys.exit(-1)

unitTestHelper = [
    '_unitTestHelpers.scatest', '_unitTestHelpers.runtestHelpers',
    '_unitTestHelpers.buildconfig'
]

version = '2.2.1'

setup(
Exemple #26
0
 def testTabNanny(self):
     """Invoking the tabnanny"""
     import tabnanny
     self.failIf(tabnanny.check("." + os.sep + 'sleekxmpp'))
Exemple #27
0
 def testTabNanny(self):
     """Testing that indentation is consistent"""
     self.failIf(tabnanny.check('..%sslixmpp' % os.sep))
Exemple #28
0
    b = 4
    pdb.set_trace()
    c = a + b
    sys_logging.info(f'{a} + {b} + {c}')


def randomize_list_in_place():
    list_a = [1, 2, 3, 4, 5, 6]
    shuffle(list_a)
    sys_logging.info(
        f'list_a = {list_a}, after randomizing, list_a = {list_a}')


def remove_white_space_in_string():
    a = ' Stephen '
    sys_logging.info(f'a = |{a}|, a.lstrip() = |{a.lstrip()}|')
    sys_logging.info(f'a = |{a}|, a.rstrip() = |{a.rstrip()}|')
    sys_logging.info(f'a = |{a}|, a.strip() = |{a.strip()}|')


if __name__ == '__main__':
    add()
    # To convert bytecode into a more human-readable format, Python has the ‘dis’ module.
    # You can say that it compiles a script, disassembles the bytecode, and prints the output to the STDOUT.
    # dis.dis(add)
    sys_logging.info(tabnanny.check('decorator.py'))
    randomize_list_in_place()
    remove_white_space_in_string()
    x = (lambda a, b: a if a > b else b)(3, 3.5)
    sys_logging.info(x)
Exemple #29
0
from distutils.core import setup
import sys

try:
    import tabnanny
except ImportError:
    pass
else:
    import StringIO
    stdout, stderr = sys.stdout, sys.stderr
    sys.stdout = co = StringIO.StringIO()
    sys.stderr = ce = StringIO.StringIO()
    # Tabnanny doesn't provide any mechanism other than print outs so we have
    # to capture the output
    tabnanny.check("ossie")
    sys.stdout = stdout
    sys.stderr = stderr
    if len(co.getvalue().strip()) != 0:
        print "Incosistent tab usage:"
        print co.getvalue()
        sys.exit(-1)

ossiepy = ['ossie',
           'ossie/apps',
           'ossie/apps/qtbrowse',
           'ossie/apps/rhlauncher',
           'ossie/apps/rhlauncher/ui',
           'ossie/cf',
           'ossie/cf/CF',
           'ossie/cf/CF__POA',
#Question 1
#tabnanny info

import tabnanny

tabnanny.check("q1_test.py")
Exemple #31
0

from distutils.core import setup
import sys

try:
    import tabnanny
except ImportError:
    pass
else:
    import StringIO

    stdout, stderr = sys.stdout, sys.stderr
    sys.stdout = co = StringIO.StringIO()
    sys.stderr = ce = StringIO.StringIO()
    # Tabnanny doesn't provide any mechanism other than print outs so we have
    # to capture the output
    tabnanny.check("_unitTestHelpers")
    sys.stdout = stdout
    sys.stderr = stderr
    if len(co.getvalue().strip()) != 0:
        print "Incosistent tab usage:"
        print co.getvalue()
        sys.exit(-1)

unitTestHelper = ["_unitTestHelpers.scatest", "_unitTestHelpers.runtestHelpers", "_unitTestHelpers.buildconfig"]

version = "2.0.3"

setup(name="unitTestHelper", version=version, description="Unit Test Helpers", py_modules=unitTestHelper, scripts=[])
Exemple #32
0
def run_tabnanny(src_file):
    'Dispatch to tabnanny'
    tabnanny.check(src_file)
Exemple #33
0
#!/usr/bin/python -O

import tabnanny, sys

for x in sys.argv:
    tabnanny.check(x)
Exemple #34
0
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2009 Doug Hellmann.  All rights reserved.
#
"""Using tabnanny from your own code
"""
#end_pymotw_header

import sys
import tabnanny

# Turn on verbose mode
tabnanny.verbose = 1

for dirname in sys.argv[1:]:
    tabnanny.check(dirname)



	def	testTabNanny(self):
		"""Invoking the tabnanny"""
		import tabnanny
		self.failIf(tabnanny.check("." + os.sep + 'sleekxmpp'))
'''
tabnanny 模块

(2.0 新增) tabnanny 模块用于检查 Python 源文件中的含糊的缩进. 
当文件混合了 tab 和空格两种缩进时候, nanny (保姆)会立即给出提示.


在下边使用的 badtabs.py 文件中, if 语句后的第一行使用 4 个空格和 1 个tab . 第二行只使用了空格.
$ tabnanny.py -v samples/badtabs.py
';samples/badtabs.py': *** Line 3: trouble in tab city! ***
offending line: print "world"
indent not equal e.g. at tab sizes 1, 2, 3, 5, 6, 7, 9

因为 Python 解释器把 tab 作为 8 个空格来处理, 所以这个脚本可以正常运行. 
在所有符合代码标准(一个 tab 为 8 个空格)的编辑器中它也会正常显示.
当然, 这些都骗不过 nanny .

'''
import tabnanny

FILE = 'samples/badtabs.py'

file = open(FILE)
for line in file.readlines():
	print(line)

tabnanny.check(FILE)
Exemple #37
0
#!/usr/bin/python -bbO

import tabnanny,sys

for x in sys.argv:
	tabnanny.check(x)
Exemple #38
0
from distutils.core import setup
import sys

try:
    import tabnanny
except ImportError:
    pass
else:
    import StringIO
    stdout, stderr = sys.stdout, sys.stderr
    sys.stdout = co = StringIO.StringIO()
    sys.stderr = ce = StringIO.StringIO()
    # Tabnanny doesn't provide any mechanism other than print outs so we have
    # to capture the output
    tabnanny.check("ossie")
    sys.stdout = stdout
    sys.stderr = stderr
    if len(co.getvalue().strip()) != 0:
        print "Incosistent tab usage:"
        print co.getvalue()
        sys.exit(-1)

ossiepy = [
    'ossie', 'ossie/apps', 'ossie/apps/qtbrowse', 'ossie/cf', 'ossie/cf/CF',
    'ossie/cf/CF__POA', 'ossie/cf/PortTypes', 'ossie/cf/PortTypes__POA',
    'ossie/cf/StandardEvent', 'ossie/cf/StandardEvent__POA',
    'ossie/cf/ExtendedEvent', 'ossie/cf/ExtendedEvent__POA',
    'ossie/cf/ExtendedCF', 'ossie/cf/ExtendedCF__POA',
    'ossie/cf/ExtendedCF/WKP', 'ossie/cf/ExtendedCF__POA/WKP', 'ossie/parsers',
    'ossie/utils', 'ossie/utils/bluefile', 'ossie/utils/bulkio',
Exemple #39
0
 def testTabNanny(self):
     """Testing that indentation is consistent"""
     self.failIf(tabnanny.check('..%sslixmpp' % os.sep))
Exemple #40
0
def main(argv):
    if len(argv[1:]) != 1:
        raise PyCompileError("Incorrect number of arguments: %r" % argv[1:])

    # Read in the file contents.
    filename = argv[1]
    fin = open(filename, 'r')
    s = fin.read()
    fin.close()

    # Get around a bug in python's compile() code on Unix that does not
    # like lines ending in CRLF.
    if not sys.platform[:3] == "win":
        s = s.replace('\r\n', '\n')

    # Get around bug in python's compile() code that requires terminal newlines
    s = s + '\n'

    results = []

    # Run tabnanny on it if available.
    try:
        import tabnanny
    except ImportError:
        pass
    else:
        import io
        o = io.StringIO()
        oldStdout = sys.stdout
        sys.stdout = o
        try:
            tabnanny.check(filename)
        except IndentationError as e:
            pass  # ignore these, we'll catch them later
        sys.stdout = oldStdout
        lineRe = re.compile("^(?P<fname>.*?) (?P<line>\d+) '(?P<content>.*)'$")
        for line in _splitlines(o.getvalue()):
            match = lineRe.match(line)
            if match:
                r = {}
                r["filename"] = filename
                r["description"] = "Ambiguous indentation (mixed tabs and spaces?)"
                r["lineno"] = int(match.group("line"))
                r["offset"] = None
                r["severity"] = "WARNING"
                r["text"] = match.group("content")
                results.append(r)
            else:
                errmsg = "Warning: could not parse tabnanny "\
                         "output line: %r\n" % line
                #XXX Silently drop it for now because stderr is used for
                #    data as well.

    # Compile it.
    try:
        dummy = builtins.compile(s, filename, 'exec')
    except SyntaxError as ex:
        r = {}
        r["filename"] = filename
        # Prefix the description with the name of the exception class.
        # This is what Python does when it prints tracebacks.
        eClassName = ex.__class__.__name__
        r["description"] = "%s: %s" % (eClassName, ex.msg)
        r["lineno"] = ex.lineno
        # ex.offset is sometime unreliable. For example ex.offset for the
        # syntax error in this code:
        #      foo = """
        #      line1
        #      line2
        # is 21. Looking at ex.text shows us the problem:
        #      'foo = """\012line1\012line2'
        # We need to recalculate the offset if there are newlines in ex.text.
        if '\n' in ex.text:
            lines = _splitlines(ex.text, 1)
            for line in lines[:-1]:
                ex.offset = ex.offset - len(line)
            ex.text = lines[-1]
        r["offset"] = ex.offset
        try:
            if isinstance(ex, TabError):
                r["severity"] = "WARNING"
            else:
                r["severity"] = "ERROR"
        except NameError:
            # Python 1.5.2 doesn't have TabError
            r["severity"] = "ERROR"
        r["text"] = ex.text
        results.append(r)

    # Print the results.
    pprint.pprint(results)
Exemple #41
0
import sys
import tabnanny

# with open('example/test.py', 'w+') as f:
#     f.write("    \t\tfor i in range(10):\nprint(i)\n\tprint()\n")

tabnanny.verbose = 1

for dirname in sys.argv[1:]:
    print(tabnanny.check(dirname))
Exemple #42
0
from setuptools import setup
import sys

try:
    import tabnanny
except ImportError:
    pass
else:
    import StringIO
    stdout, stderr = sys.stdout, sys.stderr
    sys.stdout = co = StringIO.StringIO()
    sys.stderr = ce = StringIO.StringIO()
    # Tabnanny doesn't provide any mechanism other than print outs so we have
    # to capture the output
    tabnanny.check("sca")
    sys.stdout = stdout
    sys.stderr = stderr
    if len(co.getvalue().strip()) != 0:
        print "Incosistent tab usage:"
        print co.getvalue()
        sys.exit(-1)

import sca.version

setup(name='scapy',
      version=sca.version.__version__,
      description='SCA Python',
      packages=['sca',
                'sca/apps',
                'sca/apps/qtbrowse',
Exemple #43
0
    except:
        traceback.print_exc()
        _HandlePythonFailure(what, status=status, jump=jump)


def RunTabNanny(filename, status=smPrintStatus, jump=smJumpToPosition):
    try:
        import cStringIO, tabnanny
    except Exception, message:
        print message
    # Capture the tab-nanny output
    newout = cStringIO.StringIO()
    old_out = sys.stderr, sys.stdout
    sys.stderr = sys.stdout = newout
    try:
        tabnanny.check(filename)
    finally:
        # Restore output
        sys.stderr, sys.stdout = old_out
    data = newout.getvalue()
    if data:
        try:
            lineno = string.split(data)[1]
            lineno = int(lineno)
            status("The TabNanny found trouble at line %d" % lineno)
            jump(filename, lineno)
        except (IndexError, TypeError, ValueError):
            print "The tab nanny complained, but I cant see where!"
            print data
        return 0
    return 1
Exemple #44
0
# This is just a mini-overpowa-proleet-checker by Alfateam123

import tabnanny
from pprint import pprint

PATH = "./"

if __name__ == "__main__":
    tabnanny.check(PATH)
import tabnanny
import tokenize

print(tabnanny.verbose)

print(tabnanny.filename_only)

# 检查文件缩进问题.
print(tabnanny.check('180_language_tokenize.py'))

# with open('180_language_tokenize.py', 'rb') as f:
#     tokens = tokenize.tokenize(f.readline)
#     print(tabnanny.process_tokens(tokens))
#     for token in tokens:
#         print(tabnanny.process_tokens(token))