Commit c8cbf99b authored by renzo's avatar renzo

telemac envia mail ante error + funcion para limpieza

parent 191635f2
import os
import shutil
from datetime import datetime
import metadata
def clean(root):
delete = []
for path, dirnames, filenames in os.walk(root):
try:
date = datetime.strptime(path.split("/")[-1], '%Y%m%d')
delete.append(path)
today = datetime.today()
for file in filenames:
if file.split(".")[-1] not in ["log", "m", "pickle", "csv"] or (today - date).days < 7:
delete = delete[:-1]
break
except ValueError:
pass
for path in delete:
# Remove dirs and files
if path != root:
shutil.rmtree(path)
# Remove locks
for filename in os.listdir(root):
if filename[-4:] == "lock" and path.split("/")[-1] in filename:
os.remove(os.path.join(root, filename))
for path in [metadata.logs_path, metadata.tmp_path]:
clean(path)
......@@ -3,7 +3,7 @@ import inspect
import os
import argparse
from datetime import datetime, date
from datetime import datetime
import logging
import sys
import time
......@@ -42,6 +42,7 @@ def get_downloader_args(f, terminal_args):
" ni se encuentra en el archivo de configuración del proyecto")
return args
def start_logging(args, subdir):
global log_handle
# Stops logging on previous file
......@@ -61,6 +62,7 @@ def start_logging(args, subdir):
def stop_logging():
log_handle.close()
def disable_same_calls(args):
lock_name = "".join([str(args[arg]) for arg in args if arg != "date"])
lock_name = lock_name.replace("/", "").replace(" ", "") + datetime.strftime(args["date"], '%Y%m%d') + ".lock"
......@@ -74,6 +76,7 @@ def enable_same_calls(handle):
fcntl.flock(handle, fcntl.LOCK_UN)
handle.close()
def start(args):
# Try to execute downloading and processing scripts
try:
......
......@@ -2,6 +2,8 @@ import subprocess
import datetime
import os
import mail
import metadata
......@@ -18,7 +20,7 @@ def run_model(source):
with open(log_dir + "/telemac.log", "a+") as log_file:
subprocess.check_call(metadata.telemac_script + " 'HYCOM' " + date + " " + date + " 1 8", shell=True, stderr=log_file, stdout=log_file)
except Exception as e:
print(e)
mail.send("telemac")
pass # file is not valid
if __name__ == "__main__":
......
......@@ -3,8 +3,6 @@ import sys
import subprocess
import re
import shutil
from datetime import datetime
import metadata
from shutil import copy
......@@ -54,27 +52,4 @@ def process(source, args):
copy(script_path, data_dir)
os.chdir(data_dir)
matlab_call = create_matlab_call(script, source, args)
subprocess.run(matlab_call, shell=True, check=True, stderr=sys.stderr, stdout=sys.stdout)
clean(metadata.tmp_path)
def clean(root):
delete = []
for path, dirnames, filenames in os.walk(root):
try:
delete.append(path)
date = datetime.strptime(path.split("/")[-1], '%Y%m%d')
today = datetime.today()
for file in filenames:
if file.split(".")[-1] not in ["m", "pickle", "csv"] or (today - date).days < 7:
delete = delete[:-1]
break
except ValueError:
pass
for path in delete:
if path != root:
shutil.rmtree(path)
# Remove locks
for filename in os.listdir(root):
if filename[-4:] == "lock" and path.split("/")[-1] in filename:
os.remove(os.path.join(root, filename))
\ No newline at end of file
subprocess.run(matlab_call, shell=True, check=True, stderr=sys.stderr, stdout=sys.stdout)
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment