Commit bbf78d99 authored by Romain Therville's avatar Romain Therville 🐭

Merge branch 'issue#129' into 'master'

Issue#129

See merge request !49
parents 7dfbdfa5 f9366424
......@@ -116,7 +116,7 @@ class NG6ConfigReader(object):
def get_retention_date(self, space_id="default"):
"""
return the directory corresponding to space_id
return the retention corresponding to space_id
@return: space_dir
"""
date = None
......@@ -131,7 +131,28 @@ class NG6ConfigReader(object):
return date
except:
raise Exception("Failed when parsing the config file !")
raise ValueError("Failed while gnerating retention date!")
raise ValueError("Failed while generating retention date!")
def compute_new_retention_date(self, space_id="default", old_retention = None):
"""
return the retention corresponding to space_id
@return: space_dir
"""
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")
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")
return date
except:
raise Exception("Failed when parsing the config file !")
raise ValueError("Failed while generating retention date!")
def get_log_file_path(self):
"""
......
......@@ -1031,3 +1031,38 @@ class t3MySQLdb(object):
self.update_fields('tx_nG6_purge_demand',demand_ids,
["processed_date","demand_state"],
[date,"deleted"])
def set_run_retention_from_epoch_timestamp(self, run_id, date):
"""
Updates a run's retention from a given duration (in months)
@param run_id : the run id
@param duration : the epoch timestamp
"""
sql = "UPDATE tx_nG6_run SET retention_date = '%s' WHERE uid= '%s' "% ( date, run_id )
self.execute(sql, commit = True )
def set_analysis_retention_from_epoch_timestamp(self, analysis_id, date):
"""
Updates a run's retention from a given duration (in months)
@param analysis_id : the analysis id
@param duration : the epoch timestamp
"""
sql = "UPDATE tx_nG6_analyze SET retention_date = '%s' WHERE uid= '%s' "% ( date, analysis_id )
self.execute(sql, commit = True )
def get_run_analyzes_id_from_project(self, project_id):
sql = "SELECT analyze_id FROM tx_nG6_view_project_run_analyze WHERE project_id='%s'" % project_id
qresult = self.execute(sql)
return [ r[0] for r in qresult.rows ]
def get_run_creation_date(self, run_id):
sql = "SELECT crdate FROM tx_nG6_run WHERE uid='%s'" % run_id
qresult = self.execute(sql)
return [ r[0] for r in qresult.rows ]
def get_analysis_creation_date(self, analysis_id):
sql = "SELECT crdate FROM tx_nG6_analyze WHERE uid='%s'" % analysis_id
qresult = self.execute(sql)
return [ r[0] for r in qresult.rows ]
\ No newline at end of file
#
# Copyright (C) 2015 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import os
import sys
import logging
import shutil
from ng6.ng6workflow import BasicNG6Workflow
from ng6.config_reader import NG6ConfigReader
from ng6.utils import Utils
from ng6.project import Project
from ng6.run import Run
from ng6.analysis import Analysis
from ng6.t3MySQLdb import t3MySQLdb
class SwitchProjectSpaceId (BasicNG6Workflow):
def get_description(self):
return """This workflow allows you to migrate a project from a space_id to another.
It updates the project's space_id in DB, moves the runs files, updates the runs and analyzes's files paths, and updates the corresponding retentions."""
def define_parameters(self, function="process"):
self.add_parameter('project_id', 'The project id', type=int, required = True)
self.add_parameter('space_id', 'The new space_id', required = True)
def process(self):
#self.space_id = self.space_id
#self.project_id = self.project_id
self.project_id = int(self.project_id)
if self.project_id and self.space_id :
self.project = Project.get_from_id(self.project_id)
old_space_id = self.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) + "'" )
return None
#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)] )
#Retrieve the old and new directories
ng6conf = NG6ConfigReader()
old_directory = ng6conf.get_space_directory(old_space_id)
new_directory = ng6conf.get_space_directory(self.space_id)
#new_retention = ng6conf.get_retention_date(self.space_id)
#new_retention = ng6conf.compute_new_retention_date(self.space_id, old_retention)
base_path = ng6conf.get_save_directory()
#Process for the project's runs
runs_ids = t3mysql.get_project_runs_ids(self.project_id)
for run_id in runs_ids :
path_old_run_directory = base_path + t3mysql.select_run_directory(run_id)
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 = new_run_directory.replace(old_directory, new_directory)
t3mysql.update_fields('tx_nG6_run', str(run_id), ['directory'], [str(new_run_directory)] )
#Moving the files
if not os.path.isdir(path_new_run_directory) :
os.mkdir(path_new_run_directory)
try:
shutil.move(path_old_run_directory, path_new_run_directory)
except Exception as err:
logging.getLogger("SwitchProjectSpaceId.process").error("Error while moving " + path_old_run_directory + " to " + path_new_run_directory + " \n" + str(err))
raise Exception("Error while moving " + path_old_run_directory + " to " + path_new_run_directory + " \n" + str(err))
#Delete the old directory if empty
if os.path.isdir(path_old_run_directory) and not os.listdir(path_old_run_directory) :
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)
#Process for the project's analyzes
analyzes_ids = t3mysql.get_project_analysis_ids(self.project_id)
for analysis_id in analyzes_ids :
path_old_analysis_directory = base_path + t3mysql.select_analysis_directory(analysis_id)
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 = new_analysis_directory.replace(old_directory, new_directory)
t3mysql.update_fields('tx_nG6_analyze', str(analysis_id), ['directory'], [str(new_analysis_directory)] )
#Moving the files
if not os.path.isdir(path_new_run_directory) :
os.mkdir(path_new_analysis_directory)
try:
shutil.move(path_old_analysis_directory, path_new_analysis_directory)
except Exception as err:
logging.getLogger("SwitchProjectSpaceId.process").error("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + " \n" + str(err))
raise Exception("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + " \n" + str(err))
#Delete the old directory if empty
if os.path.isdir(path_old_analysis_directory) and not os.listdir(path_old_analysis_directory) :
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)
#Process for the project's run's analyzes
run_analyzes_ids = t3mysql.get_run_analyzes_id_from_project(self.project_id)
for analysis_id in run_analyzes_ids :
path_old_analysis_directory = base_path + t3mysql.select_analysis_directory(analysis_id)
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 = new_analysis_directory.replace(old_directory, new_directory)
t3mysql.update_fields('tx_nG6_analyze', str(analysis_id), ['directory'], [str(new_analysis_directory)] )
#Moving the files
if not os.path.isdir(path_new_analysis_directory) :
os.mkdir(path_new_analysis_directory)
try:
shutil.move(path_old_analysis_directory, path_new_analysis_directory)
except Exception as err:
logging.getLogger("SwitchProjectSpaceId.process").error("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + " \n" + str(err))
raise Exception("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + " \n" + str(err))
#Delete the old directory if empty
if os.path.isdir(path_old_analysis_directory) and not os.listdir(path_old_analysis_directory) :
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
#
# Copyright (C) 2015 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
\ 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