Commit 18a04beb authored by Romain Therville's avatar Romain Therville 🐭

'switch_project_space_id' workfolw refactoring

The workflow has been re-writed to put most of the BD treatments in the
corresponding classes (Run, Analysis, Project).

issue#129
parent ba7316e1
......@@ -143,14 +143,19 @@ class Analysis (Component):
logging.getLogger("Analysis.__init__").debug("tempfile.tempdir = " + tempfile.tempdir)
if not os.path.isdir(tempfile.tempdir):
os.makedirs(tempfile.tempdir, 0o751)
logging.getLogger("Analysis.__init__").debug("self.space_id = " + self.space_id)
if id != None : # If not a new analysis
logging.getLogger("Analysis.__init__").debug("Connexion à la BD")
t3mysql = t3MySQLdb()
logging.getLogger("Analysis.__init__").debug("Connexion effectuée")
self.run = Run.get_from_id(self.__get_run_id())
from ng6.project import Project
self.project = Project.get_from_run_id(self.__get_run_id())
if self.run != None :
self.project = Project.get_from_run_id(self.__get_run_id())
else :
self.project = Project.get_from_id(self.__get_project_id())
self.space_id=self.project.space_id
self.directory = t3mysql.select_analysis_directory(id)
logging.getLogger("Analysis.__init__").debug("Building analysis with id=" + str(id) + " [" + str(self) + "]")
......@@ -669,17 +674,17 @@ class Analysis (Component):
@param id : the analysis id
"""
logging.getLogger("Analysis.get_from_id").debug("Loading the analysis from id " + str(id))
try:
t3mysql = t3MySQLdb()
[name, date, description, software, options, version] = t3mysql.select_analysis(id)
my_analysis = Analysis(name, description, software, options, version, id)
if my_analysis == None:
logging.getLogger("Analysis.get_from_id").error("The analysis id=" + str(id) + " does not exists in the database.")
raise Exception("The analysis id=" + str(id) + " does not exists in the database.\n")
return my_analysis
except Exception as err:
logging.getLogger("Analysis.get_from_id").error("Error while loading the analysis from the database [" + str(err) + "]")
raise Exception("Error while loading the analysis from the database\n" + str(err))
#try:
t3mysql = t3MySQLdb()
[name, date, description, software, options, version] = t3mysql.select_analysis(id)
my_analysis = Analysis(name, description, software, options, version, id)
if my_analysis == None:
logging.getLogger("Analysis.get_from_id").error("The analysis id=" + str(id) + " does not exists in the database.")
raise Exception("The analysis id=" + str(id) + " does not exists in the database.\n")
return my_analysis
#except Exception as err:
# logging.getLogger("Analysis.qget_from_id").error("Error while loading the analysis from the database [" + str(err) + "]")
# raise Exception("Error while loading the analysis from the database\n" + str(err))
@staticmethod
......@@ -696,3 +701,39 @@ class Analysis (Component):
return my_analysis
except :
pass
def set_retention(self, retention):
"""
Updates an analysis's retention field
@param retention : the new retention value, in (unix) epoch timestamp format
"""
if self.id != None :
t3mysql = t3MySQLdb()
t3mysql.set_analysis_retention_from_epoch_timestamp(str(self.id), retention)
def set_directory(self, directory):
"""
Updates an analysis's directory field
@param directory : the new directory path
"""
if self.id != None :
t3mysql = t3MySQLdb()
t3mysql.update_fields('tx_nG6_analyze', str(self.id), ['directory'], [str(directory)] )
def get_creation_date(self):
"""
Returns an analysis's crdate value
"""
if self.id != None :
t3mysql = t3MySQLdb()
result = t3mysql.get_analysis_creation_date(str(self.id))
return next(iter(result))
def get_directory(self):
"""
Returns an analysis's directory
"""
if self.id != None :
t3mysql = t3MySQLdb()
return t3mysql.select_analysis_directory(str(self.id))
\ No newline at end of file
......@@ -133,22 +133,22 @@ class NG6ConfigReader(object):
raise Exception("Failed when parsing the config file !")
raise ValueError("Failed while generating retention date!")
def compute_new_retention_date(self, space_id="default", old_retention = None):
def compute_new_retention_date(self, space_id="default", creation_date = None):
"""
return the retention corresponding to space_id
@return: space_dir
return the retention corresponding to space_id and crdate
@return: unix epoch timestamp retention value
"""
date = None
try :
nb_month=self.reader.getint('space_'+space_id,"retention_period")
retention_date = time.mktime((datetime.datetime.fromtimestamp(old_retention)+ relativedelta(months=nb_month)).timetuple())
logging.getLogger("config_reader.compute_new_retention_date").error("IN retention period")
retention_date = time.mktime((datetime.datetime.fromtimestamp(creation_date)+ relativedelta(months=nb_month)).timetuple())
logging.getLogger("config_reader.compute_new_retention_date").debug("IN retention period")
return retention_date
except:
try :
(d,m,y)=self.reader.get('space_'+space_id,"retention_date").split('/')
date = time.mktime(datetime.date(int(y),int(m),int(d)).timetuple())
logging.getLogger("config_reader.compute_new_retention_date").error("IN retention date")
logging.getLogger("config_reader.compute_new_retention_date").debug("IN retention date")
return date
except:
raise Exception("Failed when parsing the config file !")
......
......@@ -184,3 +184,45 @@ class Project(object):
return Project.get_from_id(id)
except :
return None
def update_space_id (self, space_id):
"""
Updates a project's space_id field
@param space_id : the new project space_id
"""
if self.id != None :
t3mysql = t3MySQLdb()
t3mysql.update_fields('tx_nG6_project', str(self.id), ['space_id'], [str(space_id)] )
def get_project_run_ids (self):
"""
Retrieve a project's runs ids
@return : a table of run ids
"""
if self.id != None :
t3mysql = t3MySQLdb()
return t3mysql.get_project_runs_ids(self.id)
else:
return None
def get_project_analyzes_ids (self):
"""
Retrieve a project's analyzes ids
@return : a table of analyzes ids
"""
if self.id != None :
t3mysql = t3MySQLdb()
return t3mysql.get_project_analysis_ids(self.id)
else:
return None
def get_project_runs_analyzes_ids (self):
"""
Retrieve a project's runs's analyzes ids
@return : a table of analyzes ids
"""
if self.id != None :
t3mysql = t3MySQLdb()
return t3mysql.get_run_analyzes_id_from_project(self.id)
else:
return None
\ No newline at end of file
......@@ -429,3 +429,46 @@ class Run(object):
return my_run
except :
pass
def set_retention(self, retention):
"""
Updates a run's retention field
@param retention : the new retention value, in (unix) epoch timestamp format
"""
if self.id != None :
t3mysql = t3MySQLdb()
t3mysql.set_run_retention_from_epoch_timestamp(self.id, retention)
else :
raise UnsavedRunError()
def set_directory(self, directory):
"""
Updates a run's directory field
@param directory : the new directory path
"""
if self.id != None :
t3mysql = t3MySQLdb()
t3mysql.update_fields('tx_nG6_run', str(self.id), ['directory'], [str(directory)] )
else :
raise UnsavedRunError()
def get_creation_date(self):
"""
Returns a run's crdate value
"""
if self.id != None :
t3mysql = t3MySQLdb()
result = t3mysql.get_run_creation_date(self.id)
return next(iter(result))
else :
raise UnsavedRunError()
def get_directory(self):
"""
Returns a run's directory
"""
if self.id != None :
t3mysql = t3MySQLdb()
return t3mysql.select_run_directory(self.id)
else :
raise UnsavedRunError()
\ No newline at end of file
......@@ -48,12 +48,12 @@ class SwitchProjectSpaceId (BasicNG6Workflow):
if self.project_id and self.space_id :
logging.getLogger("SwitchProjectSpaceId.process").debug("project_id =" + str(self.project_id) + " , new space_id = " + str(self.space_id) )
self.project = Project.get_from_id(self.project_id)
old_space_id = self.project.space_id
project = Project.get_from_id(self.project_id)
old_space_id = project.space_id
#We raise an error and return None if the project already has the given space_id
if str(old_space_id) == str(self.space_id) :
raise Exception("The project " + self.project.name + " already belongs to the space_id '" + str(self.space_id) + "'" )
raise Exception("The project " + project.name + " already belongs to the space_id '" + str(self.space_id) + "'" )
return None
#Retrieve the old and new directories
......@@ -67,21 +67,23 @@ class SwitchProjectSpaceId (BasicNG6Workflow):
#Update to the projcet's space_id
t3mysql = t3MySQLdb()
t3mysql.update_fields('tx_nG6_project', str(self.project_id), ['space_id'], [str(self.space_id)] )
project.update_space_id(self.space_id)
base_path = ng6conf.get_save_directory()
#Process for the project's runs
runs_ids = t3mysql.get_project_runs_ids(self.project_id)
runs_ids = project.get_project_run_ids()
for run_id in runs_ids :
logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating run " + str(run_id) )
path_old_run_directory = base_path + t3mysql.select_run_directory(run_id)
run = Run.get_from_id(run_id)
path_old_run_directory = base_path + run.get_directory()
path_new_run_directory = path_old_run_directory.replace(old_directory,new_directory)
#Update the directory in DB
new_run_directory = t3mysql.select_run_directory(run_id)
new_run_directory = run.get_directory()
new_run_directory = new_run_directory.replace(old_directory, new_directory)
t3mysql.update_fields('tx_nG6_run', str(run_id), ['directory'], [str(new_run_directory)] )
run.set_directory(new_run_directory)
#Moving the files
#if not os.path.isdir(path_new_run_directory) :
......@@ -99,21 +101,23 @@ class SwitchProjectSpaceId (BasicNG6Workflow):
os.rmdir(path_old_run_directory)
#Update the retention in DB
old_retention = t3mysql.get_run_creation_date(run_id)
new_retention = ng6conf.compute_new_retention_date(self.space_id, next(iter(old_retention)))
t3mysql.set_run_retention_from_epoch_timestamp(run_id, new_retention)
old_retention = run.get_creation_date()
new_retention = ng6conf.compute_new_retention_date(self.space_id,old_retention)
run.set_retention(new_retention)
#Process for the project's analyzes
analyzes_ids = t3mysql.get_project_analysis_ids(self.project_id)
analyzes_ids = project.get_project_analyzes_ids()
for analysis_id in analyzes_ids :
logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis " + str(analysis_id) )
path_old_analysis_directory = base_path + t3mysql.select_analysis_directory(analysis_id)
analysis = Analysis.get_from_id(analysis_id)
path_old_analysis_directory = base_path + analysis.get_directory()
path_new_analysis_directory = path_old_analysis_directory.replace(old_directory,new_directory)
#Update the directory in DB
new_analysis_directory = t3mysql.select_analysis_directory(analysis_id)
new_analysis_directory = analysis.get_directory()
new_analysis_directory = new_analysis_directory.replace(old_directory, new_directory)
t3mysql.update_fields('tx_nG6_analyze', str(analysis_id), ['directory'], [str(new_analysis_directory)] )
analysis.set_directory(new_analysis_directory)
#Moving the files
#if not os.path.isdir(path_new_analysis_directory) :
......@@ -131,21 +135,23 @@ class SwitchProjectSpaceId (BasicNG6Workflow):
os.rmdir(path_old_analysis_directory)
#Update the retention in DB
old_retention = t3mysql.get_analysis_creation_date(analysis_id)
new_retention = ng6conf.compute_new_retention_date(self.space_id, next(iter(old_retention)))
t3mysql.set_analysis_retention_from_epoch_timestamp(analysis_id, new_retention)
old_retention = analysis.get_creation_date()
new_retention = ng6conf.compute_new_retention_date(self.space_id,old_retention)
analysis.set_retention(new_retention)
#Process for the project's run's analyzes
run_analyzes_ids = t3mysql.get_run_analyzes_id_from_project(self.project_id)
run_analyzes_ids = project.get_project_runs_analyzes_ids()
for analysis_id in run_analyzes_ids :
logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis " + str(analysis_id) )
path_old_analysis_directory = base_path + t3mysql.select_analysis_directory(analysis_id)
analysis = Analysis.get_from_id(analysis_id)
path_old_analysis_directory = base_path + analysis.get_directory()
path_new_analysis_directory = path_old_analysis_directory.replace(old_directory,new_directory)
#Update the directory in DB
new_analysis_directory = t3mysql.select_analysis_directory(analysis_id)
new_analysis_directory = analysis.get_directory()
new_analysis_directory = new_analysis_directory.replace(old_directory, new_directory)
t3mysql.update_fields('tx_nG6_analyze', str(analysis_id), ['directory'], [str(new_analysis_directory)] )
analysis.set_directory(new_analysis_directory)
#Moving the files
#if not os.path.isdir(path_new_analysis_directory) :
......@@ -163,6 +169,6 @@ class SwitchProjectSpaceId (BasicNG6Workflow):
os.rmdir(path_old_analysis_directory)
#Update the retention in DB
old_retention = t3mysql.get_analysis_creation_date(analysis_id)
new_retention = ng6conf.compute_new_retention_date(self.space_id, next(iter(old_retention)))
t3mysql.set_analysis_retention_from_epoch_timestamp(analysis_id, new_retention)
\ No newline at end of file
old_retention = analysis.get_creation_date()
new_retention = ng6conf.compute_new_retention_date(self.space_id,old_retention)
analysis.set_retention(new_retention)
\ 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