def file_delete(filepath, samba=False): """Delete specified file""" print("Deleting file {}".format(filepath)) if samba: filename = remove_prefix(filepath) smb.delete_file(filename) else: rmFile(filepath)
job = 1 else: job = job + 1 try: # MQTT connection print("Connecting to broker ", broker) client.connect(broker, port=int(broker_port)) print("Connected.") except BaseException as ex: print("Cannot connect to MQTT broker '{}:{}': {}".format( broker, broker_port, ex)) # Delete *.RSP if available rspFile = os.path.join(filepath, "SESS" + session + '.RSP') rmFile(rspFile) # Delete *.DAT if available datFile = os.path.join(filepath, session + str(job).zfill(4) + '.DAT') rmFile(datFile) # Delete *.LOG if available logFile = os.path.join(filepath, session + str(job).zfill(4) + '.LOG') rmFile(logFile) # Create new *.JOB jobFile = os.path.join(filepath, session + str(job).zfill(4) + '.JOB') f_jobFile = open(jobFile, 'w+') jobFileHeaderDynamic1 = 'JOB 0000' + str(job).zfill(4) + \ ' RESPONSE "' + session + str(job).zfill(4) + '.LOG";\n' jobFileHeaderDynamic2 = 'REPORT ' + session + '_0000' + \ str(job).zfill(4) + ' REWRITE "' + \ session + str(job).zfill(4) + '.DAT"\n'
def run_EM63(): global varDATE, varTIME, filepathEM63, varFormEM63path, session, valEM63 session = session + 1 if session > 3: session = 1 valEM63refresh() varDATE = datetime.datetime.now().strftime("%Y%m%d") varTIME = datetime.datetime.now().strftime("%H:%M:%S") if varFormEM63path != '': filepathEM63 = varFormEM63path varFormEM63path = '' if filepathEM63 == '': print("EM63 path is not defined.") time.sleep(2) return else: #print("EM63 path is defined: " + filepathEM63) if filepathEM63.startswith('/') and not filepathEM63.endswith('/'): print("EM63 path needs / ") filepathEM63 = filepathEM63 + "/" if not os.path.exists(filepathEM63): print("EM63 path " + filepathEM63 + " does not exist.") time.sleep(2) return #else: #print("EM63 path exists.") # Open SESSnnnn.REQ file if it exists reqFile = filepathEM63 + "SESS" + str(session).zfill(4) + '.REQ' #print(reqFile) if os.path.exists(reqFile): print("---------------------------------------------") print("Request file found: " + reqFile + ". Processing ...") f_in = open(reqFile, 'r') reqFileContent = f_in.read() f_in.close() # Extract Job file name from REQ file try: jobFile = re.search('"(.+?)"', reqFileContent).group(1) print("Job file name found: " + jobFile + ".") except AttributeError: # ", " not found in the original string jobFile = '' print("No job file name found. Error ...") rmFile(reqFile) else: return # Look for job file named jobFile = filepathEM63 + jobFile if os.path.exists(jobFile): print("Job file found: " + jobFile + ". Processing ...") f_in = open(jobFile, 'r') jobFileLines = f_in.readlines() f_in.close() #print(jobFileLines) #rmFile(jobFile) # Extract instructions from job file txtLOG = '' for line in jobFileLines: # GETID if 'GETID ' in line: # Extract target file name from line datFile = re.search('"(.+?)"', line).group(1) datFile = filepathEM63 + datFile f_datFile = open(datFile, 'w+') f_datFile.write(txtGETID) f_datFile.close() txtLOG = 'COMMAND 2 PROCESSED "GETID command" ' + str( varDATE) + ' ' + str(varTIME) + ';' #print(txtLOG) # GETINFO if 'GETINFO ' in line: # Extract target file name from line datFile = re.search('"(.+?)"', line).group(1) datFile = filepathEM63 + datFile # Check if local copy of GETINFO.DAT exists: GETINFO.conf if os.path.exists("GETINFO.conf"): # use it f_in = open('GETINFO.conf', 'r') confFileBody = f_in.read() f_datFile = open(datFile, 'w+') f_datFile.write(confFileBody) f_datFile.close() f_in.close() else: f_datFile = open(datFile, 'w+') f_datFile.write(txtGETINFO) f_datFile.close() txtLOG = 'COMMAND 2 PROCESSED "GETINFO command" ' + str( varDATE) + ' ' + str(varTIME) + ';' #print(txtLOG) # REPORT if 'REPORT ' in line: # Extract target file name from line datFile = re.search('"(.+?)"', line).group(1) datFile = filepathEM63 + datFile # Write only parameters requested txtREPORT = '' valREPORT = '' for line2 in jobFileLines: for index in range(len(valEM63)): if valEM63[index][0] in line2: #print(valEM63[index][0], line2) newline2 = line2 newline2 = newline2.replace(",", "") newline2 = newline2.replace(" ", "") newline2 = newline2.replace("\n", "") #print(valEM63[index][0], newline2) if newline2 in valEM63[index][0]: txtREPORT = txtREPORT + valEM63[index][ 0] # use EM63 parameter names if index < len(valEM63): txtREPORT = txtREPORT + ',' valREPORT = valREPORT + str( valEM63[index] [1]) # use EM63 parameter values if index < len(valEM63): valREPORT = valREPORT + ',' txtREPORT = txtREPORT + '\n' + valREPORT + ';' txtREPORT = txtREPORT.replace(",\n", "\n") txtREPORT = txtREPORT.replace(",;", "") f_datFile = open(datFile, 'w+') f_datFile.write(txtREPORT) f_datFile.close() txtLOG = 'COMMAND 2 PROCESSED "REPORT command" ' + str( varDATE) + ' ' + str(varTIME) + ';' #print(txtLOG) # Create LOG file if 'RESPONSE ' in line: # Write log file # Extract target file name from line for log file logFile = re.search('"(.+?)"', line).group(1) logFile = filepathEM63 + logFile if logFile != '': txtLOG0 = 'COMMAND 1 PROCESSED "JOB command" ' + str( varDATE) + ' ' + str(varTIME) + ';\n' if os.path.exists(logFile): rmFile(logFile) #if txtLOG != '' and txtLOG0 != '': f_logFile = open(logFile, 'w+') txtLOG0 = txtLOG0 + txtLOG f_logFile.write(txtLOG0) f_logFile.close() #print("Log file was written: ", logFile) # Create RSP file rspFile = filepathEM63 + "SESS" + str(session).zfill(4) + '.RSP' if os.path.exists(rspFile): rmFile(rspFile) f_rspFile = open(rspFile, 'w+') txtRSP = '00000001 PROCESSED "EXECUTE ' + jobFile + '";' f_rspFile.write(txtRSP) f_rspFile.close() print("Response file was written: ", rspFile) sys.stdout.flush()
def file_delete(filepath, samba=False): """Delete specified file""" print("Deleting file {}".format(filepath)) rmFile(filepath)