Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import os
- import sys
- import os.path
- import getopt
- import time
- from os import listdir
- from os.path import isfile, join
- sendSubmissionScriptName = 'send_submission.py'
- toSendFolderName = 'to_send'
- alreadySentFolderName = 'already_sent'
- def parse_arguments():
- userName = ''
- password = ''
- try:
- myopts, args = getopt.getopt(sys.argv[1:],"u:p:", ['username=', 'password='])
- except getopt.GetoptError as e:
- print (str(e))
- print("Usage: %s -u username -p password" % sys.argv[0])
- sys.exit(2)
- for o, a in myopts:
- if o in ('-u', '--username'):
- userName = a
- elif o in ('-p', '--password'):
- password = a
- if not userName or not password:
- print("Usage: %s -u username -p password" % sys.argv[0])
- exit(2)
- print 'USER_NAME :', userName
- print 'PASSWORD :', '*' * len(password)
- return [userName, password]
- def log(textToLog):
- formattedToLog = "-- %s -- %s" % (time.ctime(), textToLog)
- print formattedToLog
- with open(getFileOrFolderPath('log.txt'), 'a') as logFile:
- logFile.write('%s\n' % (formattedToLog))
- def getFileOrFolderPath(fileOrFolderName):
- thisScriptFullPath = os.path.realpath(__file__)
- path, fileName = os.path.split(thisScriptFullPath)
- return os.path.join(path, fileOrFolderName)
- ########################################
- # POCZĄTEK #
- ########################################
- username, password = parse_arguments()
- toSendFolderPath = getFileOrFolderPath(toSendFolderName)
- alreadySentFolderPath = getFileOrFolderPath(alreadySentFolderName)
- sendSubmissionScriptPath = getFileOrFolderPath(sendSubmissionScriptName)
- log('Started scheduler task')
- filesToSend = [f for f in listdir(toSendFolderPath) if isfile(join(toSendFolderPath, f))]
- filesToSend = sorted(filesToSend)
- if len(filesToSend) == 0:
- log('Not found any files to send in %s directory' % (toSendFolderName))
- exit(0)
- else:
- log('Found %s not sent file(s) in %s directory: %s\n' % (len(filesToSend), toSendFolderName, filesToSend))
- fileNameToSubmit = filesToSend[0]
- log('File %s is going to be submitted' % (fileNameToSubmit))
- # Call send submission script
- filePathToSubmit = getFileOrFolderPath(os.path.join(toSendFolderPath, fileNameToSubmit))
- callScriptInfo = 'python %s -u %s -p %s -f %s' % (sendSubmissionScriptPath, username, password, filePathToSubmit)
- log('Calling: %s' % (callScriptInfo))
- returnCode = os.system(callScriptInfo)
- if returnCode != 0:
- log('Problems with sending the submission')
- exit(1)
- # Move submitted file to the already sent folder
- log('Moving submitted file to the %s folder' % (alreadySentFolderName))
- os.rename(filePathToSubmit, os.path.join(alreadySentFolderPath, fileNameToSubmit))
- log('Exited scheduler task')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement