Commit b22f512c authored by Cristiano Urban's avatar Cristiano Urban
Browse files

Added some file/dir scan goodies.

parent 9fdc2656
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
import os
import sys


class SystemUtils(object):
@@ -24,6 +25,43 @@ class SystemUtils(object):
    def setDefaultPerms(self, username):
        info = self.userInfo(username)

    def scanRecursive(self, path):
        dirList = []
        fileList = []
        #if os.path.isfile(path):
        #    p = path
        #    while p != '/':
        #        p = os.path.dirname(p)
        #        dirList.append(p)
        #    dirList.reverse()
        #    fileList.append([os.path.abspath(path)])
        #    return [ dirList, fileList ]
        for folder, subfolders, files in os.walk(path, topdown = True):
            cwd = os.path.basename(folder)
            if folder != path:
                parent = os.path.dirname(folder)
                dirList.append(parent + '/' + cwd)
                i = 0
                for f in files:
                    files[i] = parent + '/' + cwd + '/' + f
                    i += 1
                fileList.append(files)
        return [ dirList, fileList ]
    
    # Scan is performed only on the first level!
    def scan(self, path):
        dirList = []
        fileList = []
        elementList = os.listdir(path)
        for el in elementList:
            elPath = path + '/' + el
            if os.path.isdir(elPath):
                dirList.append(el)
            elif os.path.isfile(elPath):
                fileList.append(el)
            else:
                sys.exit("FATAL: invalid file/dir.")
        return [ dirList, fileList ]

# Test
#sysUtils = SystemUtils()