__init__.py 8.8 KB
Newer Older
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
#
# 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
            
Romain Therville's avatar
Romain Therville committed
53
54
55
56
57
            #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
            
58
59
60
61
62
63
64
65
66
            #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)
            
Romain Therville's avatar
Romain Therville committed
67
68
69
            #new_retention = ng6conf.get_retention_date(self.space_id)
            #new_retention = ng6conf.compute_new_retention_date(self.space_id, old_retention)
            
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
            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
Romain Therville's avatar
Romain Therville committed
98
99
                old_retention = t3mysql.get_run_creation_date(run_id)
                new_retention = ng6conf.compute_new_retention_date(self.space_id, next(iter(old_retention)))
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
                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
Romain Therville's avatar
Romain Therville committed
128
129
130
                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)
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
                
            #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
Romain Therville's avatar
Romain Therville committed
158
159
160
                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)