Commit 08853c0c authored by Cristiano Urban's avatar Cristiano Urban
Browse files

Added first test of deleted nodes cleanup script called 'cleaner.py'.

parent 2725e46f
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
#!/usr/bin/env python

import datetime
import os

from config import Config
from db_connector import DbConnector


config = Config("vos_ts.conf")
params = config.loadSection("file_catalog")
dbConn = DbConnector(params["user"],
                     params["password"],
                     params["host"],
                     params.getint("port"),
                     params["db"],
                     1,
                     1)

fileList = dbConn.getNodesToBeDeleted()
basePaths = []
for row in fileList:
    basePath = row["os_base_path"]
    relPath = row["os_rel_path"]    
    filePath = basePath + relPath
    dTime = row["deleted_on"]
    cTime = datetime.datetime.now()
    phyDeletedTstamp = row["phy_deleted_on"]
    nodeId = row["node_id"]
    delta = cTime - dTime
    if delta.seconds > 30 and phyDeletedTstamp is None:
        print(filePath + '\n')
        os.remove(filePath)
        dbConn.setPhyDeletedOn(nodeId)
        if basePath not in basePaths:
            basePaths.append(basePath)

print()

dirList = []
for basePath in basePaths:
    for root, dir, files in os.walk(basePath, topdown = False):
        if root != basePath and not os.listdir(root):
            dirList.append(root)

for dir in dirList:
    print(dir + '\n')
    os.rmdir(dir)

# 1) delete files
# 2) check for empty dirs and delete them if different from rootPath (second step), then update the db