tasks.py
2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from __future__ import absolute_import
from common.models import *
from utils.Logger import setupLogger
from django.conf import settings
import subprocess
import os
'''
Gets the folder path and the plan id
it makes an analysis of the calibrated images (in folder "calibrated")
'''
#class Analysis(Task):
class Analysis():
logger = setupLogger("analysis", "analysis")
def log(self, message: str) -> int:
if settings.DEBUG:
self.logger.info(message)
return 0
def logDB(self, message: str) -> int:
Log.objects.create(agent='Analyzer', message=message)
return 0
def execProcess(self, command: str) -> int:
self.process = subprocess.Popen(command, shell=True)
return 0
def start(self, plan_id: int, folder: str) -> int:
self.plan_id = plan_id
self.folder = folder
self.path_dir_file = os.path.dirname(os.path.realpath(__file__))
try:
self.plan = Plan.objects.get(id=self.plan_id)
except:
self.log("analysis not found")
self.logDB(message="Plan with id %d not found"%(self.plan_id))
return 1
self.dir_name = self.folder + os.sep + self.plan.name + "_" + str(self.plan_id) + os.sep + "analysis"
self.plan_folder = self.plan.name + "_" + str(self.plan_id)
if self.createDirectory():
self.log("analysis err dir")
self.logDB(message="Could not create the directory for analysis")
return 1
self.log("analysis start")
return self.logDB("Starting analysis for plan " + self.plan.name)
def createDirectory(self):
try:
if not os.path.isdir(self.dir_name):
os.makedirs(self.dir_name)
except:
return 1
return 0
def changeDirectory(self, path: str):
os.chdir(path)
return 0
def execute(self) -> int:
self.changeDirectory(self.path_dir_file)
return self.execProcess("./analyzer '"+str(self.folder)+"' '"+self.plan_folder+"' '"+str(self.plan.duration) + "'")
def end(self) -> int:
self.process.wait()
if self.process.returncode == 0:
self.log("analysis executed")
self.logDB(message="Analysis executed successfully for plan " + str(self.plan.name))
else:
self.log("analysis failed")
self.logDB(message="Could not make an analysis for plan " + str(self.plan.name))
return self.process.returncode
def run(self, plan_id, folder):
if self.start(plan_id, folder):
return 1
self.execute()
self.end()
return 0