'''
Created on 21.01.2017

@author: gex
'''

from ss.jobs.job import Job
from ss.rsync import RSync

class SyncJob(Job):
    '''
    Synchronizes the 'to' directory with the 'from' directory.
    I.e., the directories will be exectly the same after the command
    '''
    
    FROM_KEY='from'
    TO_KEY='to'
    EXCLUDE_KEY='exclude'
    
    def __init__(self,config,lastExecConfig,name):
        super(SyncJob, self).__init__(config,lastExecConfig,name)
        self._fromPath=self.getRequiredConfPath(SyncJob.FROM_KEY)
        self._toPath=self.getRequiredConfPath(SyncJob.TO_KEY)
        self._excludes=self.getOptionalConfStr(SyncJob.EXCLUDE_KEY).split(';')
        # Only non-empty entries, strip whitespace
        self._excludes=[x.strip() for x in self._excludes if len(x.strip()) > 0]
        

    def execute(self):
        rsync=RSync()
        rsync.setArchive(True) # Archive mode, preserve file attributes and more
        rsync.setStats(True) # Print stats
        rsync.setUpdate(True) # Update only files that are newer than files on
        rsync.setDelete(True) # Delete files on the receiver side that don't exist on the sender side

        if len(self._excludes) > 0:
            rsync.setDeleteExcluded(True) # Delete excluded dirs from receiver, if present
            rsync.setExcludes(self._excludes)
        
        # Execute
        rsync.execute(self._fromPath,self._toPath)
        return True