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

Fixed bug on dir size check + changed constructor + added setters.

parent 110b495c
Loading
Loading
Loading
Loading
+20 −10
Original line number Diff line number Diff line
@@ -5,11 +5,18 @@ import subprocess


class FileGrouper(object):
    # Constructor requiring the path of the folder, the minimum number of files
    # and the maximum folder size in bytes
    def __init__(self, folderPath, minNumOfFiles, maxFolderSize):
        self.folderPath = folderPath
    # Constructor requiring the minimum number of files
    # and the maximum folder size in bytes (constraints)
    def __init__(self, minNumOfFiles, maxFolderSize):
        self.minNumOfFiles = minNumOfFiles
        self.maxFolderSize = maxFolderSize

    # Sets the 'minimum number of files' constraint
    def setMinNumOfFiles(self, minNumOfFiles):
        self.minNumOfFiles = minNumOfFiles
      
    # Sets the 'maximum folder size' constraint  
    def setMaxFolderSize(self, maxFolderSize):
        self.maxFolderSize = maxFolderSize

    # Returns a list of all the 'leaves' (directories not
@@ -23,7 +30,9 @@ class FileGrouper(object):

    # Returns the folder size in bytes
    def getFolderSize(self, folder):
        files = [ os.path.abspath(f) for f in os.listdir() ]
        files = []
        for f in os.listdir(folder):
            files.append(os.path.abspath(folder) + '/' + f)  
        folderSize = 0
        for f in files:
            statinfo = os.stat(f)
@@ -35,7 +44,8 @@ class FileGrouper(object):
        return len(os.listdir(folder))

    # Start the .tar creation procedure
    def start(self):
    def start(self, folderPath):
        self.folderPath = folderPath
        leaves = self.getLeaves()
        for folder in leaves:
            if(self.getNumOfFiles(folder) > self.minNumOfFiles and self.getFolderSize(folder) < self.maxFolderSize):
@@ -49,11 +59,11 @@ class FileGrouper(object):
                    try:
                        shutil.rmtree(folder)
                    except OSError as e:
                        print(f"Error: {folder} => {e.strerror}")
                        sys.exit(f"Error: {folder} => {e.strerror}")
                os.chdir(cwd)


# Test (creates a .tar of all the leaves containing at least 2 files and having size lower than 100G,
# than removes those directories)
fg = FileGrouper("/home/cristiano/Lavoro/md5_test/store/a", 1, 100 * (2 ** 30))
fg.start()
fg = FileGrouper(1, 100 * (2 ** 30))
fg.start("/home/cristiano/Lavoro/md5_test/store/a")