Sleds/buildvm/scat/filewatcher.py

118 lines
4.3 KiB
Python

#!python3
'''
Created on Sep 21, 20-16
author: tpweis
file name: filewatcher.py
This function watches a directory (input argument 1)
when a new file is detected, it is pushed to the Server using WinSCP.
The method is to create a small "put" script, and execute it.
When the put is complete, the file is moved (locally) to the processed
directory.
'''
import os, time
import subprocess
import shutil
import subprocess
import sys
def bld_transfer_code(fromdir, processdir, todir, filename):
"function bld_transfer_code"
# filename argument is the name of the file we are uploading. The generated code file name
# is formed from this.
line1 = 'open sftp://thomas:ijfijfijf@10.130.30.154/ -hostkey="ssh-ed25519 256 ff:98:b5:5c:20:0b:18:f9:14:a5:e3:f4:b5:a6:1f:84" -rawsettings FSProtocol=2'
line2 = "lcd " + fromdir
line3 = "cd " + todir
line4 = "put " + filename
line5 = "exit"
tmp_name = processdir + "/transfer_" + filename
gen_file_name = os.path.normpath(tmp_name)
print("transfer code " + gen_file_name)
# Save the transfer code and return its name
f = open(gen_file_name,'w')
print(line1, line2, line3, line4, line5, file= f, sep="\n")
f.close()
return gen_file_name
def main():
"function main"
# WinSCP gets the server host name from the transfer code. This specifices the directory
# on that machine
UPLOADdir = "/home/thomas/tmp"
arglist = sys.argv[1:]
if(len(arglist)< 1):
print("Missing Watch Directory argument. Try again")
sys.exit(3)
else:
WATCHdir = arglist[0]
if(len(arglist) < 2):
print("Missing Processed directory argument. Try again")
sys.exit(4)
else:
PROCESSdir =arglist[1]
# Now ensure that both directorys exist. And, that the the process directory is different
# from the watch directory
if not os.path.isdir(WATCHdir):
print("First arg (watch directory) " + WATCHdir + " is not a valid directory")
sys.exit(5)
if not os.path.isdir(PROCESSdir):
print("Second arg (processed directory) " + PROCESSdir + " is not a valid directory")
sys.exit(6)
print("Uploading files from " + WATCHdir + ", then moving them to " + PROCESSdir)
before = dict ([(f, None) for f in os.listdir (WATCHdir)])
winscp = os.path.normpath("C:\Program Files (x86)\WinSCP\WinSCP.exe")
if not os.path.isfile(winscp):
print("cannot locate WinSCP at " + winscp + " *HALT*")
sys.exit(7)
# Go into a timed loop to check for new files arriving.
while 1:
time.sleep (3)
print("Check for changes")
after = dict ([(f, None) for f in os.listdir (WATCHdir)])
added = [f for f in after if not f in before]
removed = [f for f in before if not f in after]
if added:
print ("Added: ", ", ".join (added))
for filename in added:
logfile = "/log=" + os.path.normpath(PROCESSdir + "\\" + filename + ".log")
scriptname = bld_transfer_code(WATCHdir,PROCESSdir,UPLOADdir,filename)
scriptfile = "/script=" + os.path.normpath(scriptname)
CMD = winscp + " " + logfile + " /ini=nul" + " " + scriptfile
print(CMD)
# Upload added file
p1 = subprocess.Popen([winscp, logfile, "/ini=nul", scriptfile], stdout=subprocess.PIPE)
p1.wait()
# Upload associated log file
scriptname = bld_transfer_code(PROCESSdir, PROCESSdir, UPLOADdir, filename + ".log")
scriptfile = "/script=" + os.path.normpath(scriptname)
log_log = logfile + ".log"
CMD = winscp + " " + logfile + " /ini=nul" + " " + scriptfile
p2 = subprocess.Popen([winscp, logfile, "/ini=nul", scriptfile], stdout=subprocess.PIPE)
# Move added file to "processed" directory
oldfile = WATCHdir + "/" + filename
newfile = PROCESSdir + "/" + filename
print("mv " + oldfile + " to " + newfile)
shutil.move(oldfile,newfile)
if removed: print ("Removed: ", ", ".join (removed))
before = after
sys.exit(0)
if __name__ == '__main__':
# execute only if run as a script
main()