Now uses logging to file.

This commit is contained in:
2019-04-29 21:53:29 +02:00
parent 48266f7d1a
commit af136e7ce2

View File

@@ -3,6 +3,9 @@ import os, sys
from subprocess import check_output, Popen, PIPE from subprocess import check_output, Popen, PIPE
from configparser import ConfigParser from configparser import ConfigParser
# Local files
from logger import logger
def getConfig(): def getConfig():
print('Reading config') print('Reading config')
pwd = os.path.dirname(os.path.abspath(__file__)) pwd = os.path.dirname(os.path.abspath(__file__))
@@ -46,6 +49,7 @@ def transferFiles(files, localPath, remotePath, host=None, user=None):
transferedFiles = [] transferedFiles = []
for file in files: for file in files:
logger.info('Moving file: ', file)
file = os.path.join(localPath, file) file = os.path.join(localPath, file)
if (host and user): if (host and user):
@@ -57,7 +61,7 @@ def transferFiles(files, localPath, remotePath, host=None, user=None):
stdout, stderr = rsyncProcess.communicate() stdout, stderr = rsyncProcess.communicate()
if stderr: if stderr:
print('Unable', stderr) logger.error('Error when rsyncing', stderr)
print(stdout) print(stdout)
transferedFiles.append(file) transferedFiles.append(file)
@@ -69,6 +73,7 @@ def removeFromDeluge(execScript, files):
for file in files: for file in files:
print('Removing {} from deluge'.format(file)) print('Removing {} from deluge'.format(file))
logger.info('Removing {} from deluge'.format(file))
cmd = "{} {} rm '{}'".format(execPython, execScript, file) cmd = "{} {} rm '{}'".format(execPython, execScript, file)
delugeProcess = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) delugeProcess = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
@@ -77,7 +82,7 @@ def removeFromDeluge(execScript, files):
if stderr: if stderr:
print('Deluge unable', stderr) print('Deluge unable', stderr)
print('Successfully removed: ', file) logger.info('Successfully removed: ', file)
def main(): def main():
config = getConfig() config = getConfig()
@@ -95,8 +100,8 @@ def main():
newFiles = filesNotShared(localFiles, remoteFiles) newFiles = filesNotShared(localFiles, remoteFiles)
if (newFiles): if (newFiles):
print('New files: {}'.format(newFiles)) logger.info('New files: {}'.format(newFiles))
print('Existing files: {}'.format(list(set(remoteFiles).intersection(localFiles)))) logger.info('Existing files: {}'.format(list(set(remoteFiles).intersection(localFiles))))
transferedFiles = transferFiles(newFiles, localPath, remotePath, host, user) transferedFiles = transferFiles(newFiles, localPath, remotePath, host, user)
removeFromDeluge(delugeScript, transferedFiles) removeFromDeluge(delugeScript, transferedFiles)