Newer
Older
Hicham Brahimi
committed
# Gestion de wvdial deplacee dans rapatriement.py
import os
import os.path
from datetime import datetime
import psutil
Hicham Brahimi
committed
#def check_pid(pid):
# # Verifie l'existence d'un pid
# if psutil.pid_exists(pid):
# p = psutil.Process(pid)
# if p.status() != psutil.STATUS_ZOMBIE:
# return True
# return False
# Verifie qu'il n'y a pas d'autres processus d'envoi en verifiant le fichier lock
if os.path.isfile("./.lockprocess"): print("Locked")
while os.path.isfile("./.lockprocess"): time.sleep(1)
# Creer un nouveau fichier lock pour verrouiller les autres processus
if not os.path.isfile("./.lockprocess"):
open(".lockprocess", "a").close()
subprocess.Popen(["chmod", "777", ".lockprocess"])
else: # Si un processus s'est activée entre la 1ere boucle et la condition
while os.path.isfile("./.lockprocess"): time.sleep(1)
open(".lockprocess", "a").close()
subprocess.Popen(["chmod", "777", ".lockprocess"])
FOLDER = "data/"
TRAITEES = "traitees/"
chemin = nomFichier.split("/")
log = open("log/"+chemin[1]+".log", "w")
f = None
try:
Hicham Brahimi
committed
#pid = subprocess.Popen(["wvdial"])
Hicham Brahimi
committed
#print("============ WVDIAL LANCE ==============")
#print("PID = ",pid.pid)
Hicham Brahimi
committed
#time.sleep(10)
Hicham Brahimi
committed
#print("============ CORRECTION ROUTE ==============")
#subprocess.Popen(["route", "add", "default", "dev", "ppp0"]) # Corrige la 4G au demarrage du raspberry
print("============ CONNEXION ==============")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.connect((IP_PC, PORT))
f = open(nomFichier, "rb")
print("======== CALCUL TAILLE ========")
taille = os.path.getsize(nomFichier)
print("============ ENVOI FICHIER ==============")
totalEnvoyee = 0
with open(nomFichier, "rb") as f:
totalEnvoyee += server.sendfile(f, 0)
print(" => chunk %d/%d" % (totalEnvoyee, taille))
log.write(" => chunk "+str(totalEnvoyee)+"/"+str(taille)+"\n")
server.close()
shutil.move(FOLDER+chemin[1], TRAITEES+chemin[1])
except KeyboardInterrupt :
print("============= LIBERATION =============")
os.remove(".lockprocess")
Hicham Brahimi
committed
#print("============ WVDIAL ARRET ==============")
#subprocess.Popen(["kill", "-1", str(pid.pid)])
except Exception as ex:
print("Exception, voir log")
log.write(str(datetime.now())+" => Exception "+str(type(ex))+" ("+str(ex.args)+")\n")
time.sleep(1)
print("============= LIBERATION =============")
os.remove(".lockprocess")
Hicham Brahimi
committed
#print("============ WVDIAL ARRET ==============")
#subprocess.Popen(["kill", "-1", str(pid.pid)])
print("================ FIN =================")
if f is not None:
f.close()
server.close()
log.close()
Hicham Brahimi
committed
#if check_pid(pid.pid):
#print("============ WVDIAL ARRET ==============")
#subprocess.Popen(["kill", "-1", str(pid.pid)])
time.sleep(1)
if os.path.isfile("./.lockprocess"):
print("=========== LIBERATION ============")
os.remove(".lockprocess")