Sleds/buildvm/testtools/watcherfeeder.py

100 lines
3.2 KiB
Python

#!python3
'''
Created on Sep 21, 20-16
author: tpweis
file name: watcherfeeder.py
This function feeds files into a directory that is watched. (input argument 1)
The first argument FEEDdir supplies the files.
The second argument TARGETdir is where the copies are placed.
A file is copied every n milliseconds, the third argument (interval)
After all the files in FEEDdir have been copied once, the process begins again.
That is, copying every file in FEEDdir to TARGETdir. Each file gets its own name,
formed by added an integer count to the filename.
the watcher.
'''
import os, time
import subprocess
import sys
import shutil
def main():
"function main"
arglist = sys.argv[1:]
if(len(arglist)< 1):
print("Missing Feed Directory argument. Try again")
sys.exit(3)
else:
FEEDdir = arglist[0]
if(len(arglist) < 2):
print("Missing Target directory argument. Try again")
sys.exit(4)
else:
TARGETdir =arglist[1]
if(len(arglist) < 3):
print("Missing Timing parameter (ms between copies) . Defaulting to 1000 (1 sec)")
interval = 1000
else:
interval =int(arglist[2])
if interval < 100:
print("Minimum interval is 100 ms")
interval = 100
elif interval > 60000:
print("Maximum interval is 60,000 ms (60 sec)")
interval = 30000
# Now ensure that both directories exist
if not os.path.isdir(FEEDdir):
print("First arg (FEED directory) " + FEEDdir + " is not a valid directory")
sys.exit(5)
if not os.path.isdir(TARGETdir):
print("Second arg (TARGET directory) " + TARGETdir + " is not a valid file name")
sys.exit(6)
print("Copy ZIP files from " + FEEDdir + " to " + TARGETdir + ", one very " + str(interval) + " milliseconds")
feedlist = dict ([(f, None) for f in os.listdir (FEEDdir)])
# Only proceed if the feedlist is none empty. If it is non-empty, but has no .pdf files in it,
# we will find that out later
if not feedlist:
print("Feed directory is empty. Try again")
sys.exit(7)
pause = interval / 1000
copy_count = 0
while 1:
print("check for files in directory")
copy_count += 1
for sourcefile in feedlist:
try:
print("examine name contents: " + sourcefile)
file_name, sep, file_ext = sourcefile.rpartition('.')
print("File extension is " + file_ext)
except:
print("Could not get a valid filename from " + sourcefile)
sys.exit(8)
# We are only copying .pdf files. Check now
if file_ext == "pdf":
# begin the copying
# fiddle with the file name so that we can produce numbered copies
oldfile = os.path.normpath(FEEDdir + "/" + sourcefile)
newfile = os.path.normpath(TARGETdir + "/" + file_name + str(copy_count) + sep + file_ext)
print(oldfile + " to " + newfile)
shutil.copy(oldfile,newfile)
time.sleep(pause)
sys.exit(0)
if __name__ == '__main__':
# execute only if run as a script
main()