Beispiel #1
0
def zapTrailingWhitespace(dirname):
    """Eliminates trailing spaces IN PLACE.  Use with extreme care
    and only after a backup or with version-controlled code."""
    assert os.path.isdir(dirname), "Directory not found!"
    print("This will eliminate all trailing spaces in py files under %s." %
          dirname)
    ok = raw_input("Shall I proceed?  type YES > ")
    if ok != 'YES':
        print('aborted by user')
        return
    w = GlobDirectoryWalker(dirname, '*.py')
    for filename in w:
        # trim off final newline and detect real changes
        txt = open(filename, 'r').read()
        badChars = 0
        cleaned = []
        for line in string.split(txt, '\n'):
            stripped = string.rstrip(line)
            cleaned.append(stripped)
            spaces = len(line) - len(
                stripped)  # OK, so they might be trailing tabs, who cares?
            if spaces:
                badChars = badChars + spaces

        if badChars != 0:
            open(filename, 'w').write(string.join(cleaned, '\n'))
            print("file %s contained %d trailing spaces, FIXED" %
                  (filename, badChars))
    print('done')
Beispiel #2
0
 def cleanup(folder,patterns=('*.pdf', '*.log','*.svg','runAll.txt', 'test_*.txt','_i_am_actually_a_*.*')):
     if not folder: return
     for pat in patterns:
         for filename in GlobDirectoryWalker(folder, pattern=pat):
             try:
                 os.remove(filename)
             except:
                 pass
Beispiel #3
0
    def testAscii(self):
        "Test if Python files are pure ASCII ones."
        from reportlab.lib.testutils import RL_HOME
        allPyFiles = GlobDirectoryWalker(RL_HOME, '*.py')

        for path in allPyFiles:
            fileContent = open_and_read(path,'r')
            nonAscii = u''.join(list(set([c for c in fileContent if ord(c)>127])))

            truncPath = path[path.find('reportlab'):]
            args = (truncPath, repr(map(ord, nonAscii)))
            msg = "File %s contains characters: %s." % args
            assert len(nonAscii) == 0, msg
Beispiel #4
0
    def findSuspiciousModules(self, folder, rootName):
        "Get all modul paths with non-Unix-like first line."

        firstLinePat = re.compile('^#!.*python.*')

        paths = []
        for file in GlobDirectoryWalker(folder, '*.py'):
            if os.path.basename(file) == '__init__.py':
                continue
            firstLine = open_and_readlines(file)[0]
            if not firstLinePat.match(firstLine):
                paths.append(file)

        return paths
Beispiel #5
0
    def testTrailingDigits(self):
        "Test if Python files contain trailing digits."

        from reportlab.lib.testutils import RL_HOME
        allPyFiles = GlobDirectoryWalker(RL_HOME, '*.py')

        for path in allPyFiles:
            #hack - exclude barcode extensions from this test
            if string.find(path, 'barcode'):
                pass
            else:
                basename = os.path.splitext(path)[0]
                truncPath = path[string.find(path, 'reportlab'):]
                msg = "Filename %s contains trailing digits." % truncPath
                assert basename[-1] not in string.digits, msg
Beispiel #6
0
    def testAscii(self):
        "Test if Python files are pure ASCII ones."
        from reportlab.lib.testutils import RL_HOME
        allPyFiles = GlobDirectoryWalker(RL_HOME, '*.py')

        for path in allPyFiles:
            fileContent = open_and_read(path, 'r')
            nonAscii = filter(lambda c: ord(c) > 127, fileContent)
            nonAscii = unique(nonAscii)

            truncPath = path[string.find(path, 'reportlab'):]
            args = (truncPath, repr(map(ord, nonAscii)))
            msg = "File %s contains characters: %s." % args
            ##            if nonAscii:
            ##                print msg
            assert nonAscii == '', msg
Beispiel #7
0
def makeSuite(folder, exclude=[],nonImportable=[],pattern='test_*.py'):
    "Build a test suite of all available test files."
    allTests = unittest.TestSuite()

    if os.path.isdir(folder): sys.path.insert(0, folder)
    for filename in GlobDirectoryWalker(folder, pattern):
        modname = os.path.splitext(os.path.basename(filename))[0]
        if modname not in exclude:
            try:
                exec('import %s' % modname)
                allTests.addTest(sys.modules[modname].makeSuite())
            except:
                tt, tv, tb = sys.exc_info()[:]
                nonImportable.append((filename,traceback.format_exception(tt,tv,tb)))
                del tt,tv,tb
    del sys.path[0]

    return allTests
Beispiel #8
0
 def testFiles(self):
     w = GlobDirectoryWalker(RL_HOME, '*.py')
     for filename in w:
         self.checkFileForTabs(filename)
         self.checkFileForTrailingSpaces(filename)
Beispiel #9
0
def getModuleObjects(folder, rootName, typ, pattern='*.py'):
    "Get a list of all objects defined *somewhere* in a package."

    # Define some abbreviations.
    find = string.find
    split = string.split
    replace = string.replace

    objects = []
    lookup = {}
    for file in GlobDirectoryWalker(folder, pattern):
        folder = os.path.dirname(file)

        if os.path.basename(file) == '__init__.py':
            continue

##        if os.path.exists(os.path.join(folder, '__init__.py')):
####            print 'skipping', os.path.join(folder, '__init__.py')
##            continue

        sys.path.insert(0, folder)
        cwd = os.getcwd()
        os.chdir(folder)

        modName = os.path.splitext(os.path.basename(file))[0]
        prefix = folder[find(folder, rootName):]
        prefix = replace(prefix, os.sep, '.')
        mName = prefix + '.' + modName

        try:
            module = __import__(mName)
        except ImportError:
            # Restore sys.path and working directory.
            os.chdir(cwd)
            del sys.path[0]
            continue

        # Get the 'real' (leaf) module
        # (__import__ loads only the top-level one).
        if find(mName, '.') != -1:
            for part in split(mName, '.')[1:]:
                module = getattr(module, part)

            # Find the objects in the module's content.
            modContentNames = dir(module)

            # Handle modules.
            if typ == ModuleType:
                if find(module.__name__, 'reportlab') > -1:
                    objects.append((mName, module))
                    continue

            for n in modContentNames:
                obj = eval(mName + '.' + n)
                # Handle functions and classes.
                if typ in (FunctionType, ClassType):
                    if type(obj) == typ and obj not in lookup:
                        if typ == ClassType:
                            if find(obj.__module__, rootName) != 0:
                                continue
                        objects.append((mName, obj))
                        lookup[obj] = 1
                # Handle methods.
                elif typ == MethodType:
                    if type(obj) == ClassType:
                        for m in dir(obj):
                            a = getattr(obj, m)
                            if type(a) == typ and a not in lookup:
                                if find(a.im_class.__module__, rootName) != 0:
                                    continue
                                cName = obj.__name__
                                objects.append((mName, a))
                                lookup[a] = 1

        # Restore sys.path and working directory.
        os.chdir(cwd)
        del sys.path[0]
    return objects