Advertisement
kneefer

Scheduler

Nov 26th, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import os
  4. import sys
  5. import os.path
  6. import getopt
  7. import time
  8. from os import listdir
  9. from os.path import isfile, join
  10.  
  11. sendSubmissionScriptName = 'send_submission.py'
  12. toSendFolderName         = 'to_send'
  13. alreadySentFolderName    = 'already_sent'
  14.  
  15. def parse_arguments():
  16.     userName = ''
  17.     password = ''
  18.  
  19.     try:
  20.         myopts, args = getopt.getopt(sys.argv[1:],"u:p:", ['username=', 'password='])
  21.     except getopt.GetoptError as e:
  22.         print (str(e))
  23.         print("Usage: %s -u username -p password" % sys.argv[0])
  24.         sys.exit(2)
  25.  
  26.     for o, a in myopts:
  27.         if o in ('-u', '--username'):
  28.             userName = a
  29.         elif o in ('-p', '--password'):
  30.             password = a
  31.  
  32.     if not userName or not password:
  33.         print("Usage: %s -u username -p password" % sys.argv[0])
  34.         exit(2)
  35.  
  36.     print 'USER_NAME :', userName
  37.     print 'PASSWORD  :', '*' * len(password)
  38.  
  39.     return [userName, password]
  40.  
  41. def log(textToLog):
  42.     formattedToLog = "-- %s -- %s" % (time.ctime(), textToLog)
  43.     print formattedToLog
  44.     with open(getFileOrFolderPath('log.txt'), 'a') as logFile:
  45.         logFile.write('%s\n' % (formattedToLog))
  46.  
  47. def getFileOrFolderPath(fileOrFolderName):
  48.     thisScriptFullPath = os.path.realpath(__file__)
  49.     path, fileName = os.path.split(thisScriptFullPath)
  50.     return os.path.join(path, fileOrFolderName)
  51.  
  52. ########################################
  53. #              POCZĄTEK                #
  54. ########################################
  55.  
  56. username, password = parse_arguments()
  57.  
  58. toSendFolderPath      = getFileOrFolderPath(toSendFolderName)
  59. alreadySentFolderPath = getFileOrFolderPath(alreadySentFolderName)
  60. sendSubmissionScriptPath = getFileOrFolderPath(sendSubmissionScriptName)
  61.  
  62. log('Started scheduler task')
  63.  
  64. filesToSend = [f for f in listdir(toSendFolderPath) if isfile(join(toSendFolderPath, f))]
  65. filesToSend = sorted(filesToSend)
  66.  
  67. if len(filesToSend) == 0:
  68.     log('Not found any files to send in %s directory' % (toSendFolderName))
  69.     exit(0)
  70. else:
  71.     log('Found %s not sent file(s) in %s directory: %s\n' % (len(filesToSend), toSendFolderName, filesToSend))
  72.  
  73. fileNameToSubmit = filesToSend[0]
  74. log('File %s is going to be submitted' % (fileNameToSubmit))
  75.  
  76. # Call send submission script
  77. filePathToSubmit = getFileOrFolderPath(os.path.join(toSendFolderPath, fileNameToSubmit))
  78. callScriptInfo = 'python %s -u %s -p %s -f %s' % (sendSubmissionScriptPath, username, password, filePathToSubmit)
  79. log('Calling: %s' % (callScriptInfo))
  80.  
  81. returnCode = os.system(callScriptInfo)
  82. if returnCode != 0:
  83.     log('Problems with sending the submission')
  84.     exit(1)
  85.  
  86. # Move submitted file to the already sent folder
  87. log('Moving submitted file to the %s folder' % (alreadySentFolderName))
  88. os.rename(filePathToSubmit, os.path.join(alreadySentFolderPath, fileNameToSubmit))
  89.  
  90. log('Exited scheduler task')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement