__init__.py 10.6 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
#
# 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):
44
        logging.getLogger("SwitchProjectSpaceId.process").debug("Begin SwitchProjectSpaceId workflow.")
45
46
47
48
49
        #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 :
50
            logging.getLogger("SwitchProjectSpaceId.process").debug("project_id =" + str(self.project_id)  + " , new space_id = " + str(self.space_id) )
51
52
53
            self.project = Project.get_from_id(self.project_id)
            old_space_id = self.project.space_id
            
Romain Therville's avatar
Romain Therville committed
54
55
56
57
58
            #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
            
59
60
61
            #Retrieve the old and new directories
            ng6conf = NG6ConfigReader()
            old_directory = ng6conf.get_space_directory(old_space_id)
62
63
64
65
66
            try:
                new_directory = ng6conf.get_space_directory(self.space_id)
            except Exception as err:
                logging.getLogger("SwitchProjectSpaceId.process").error("Error while searching space_id '" + str(self.space_id) + "' in application.properties, it certainly doesn't exist. \n" + str(err))
                raise Exception("Error while searching space_id '" + str(self.space_id) + "' in application.properties, it certainly doesn't exist. \n" + str(err))
67
            
68
69
70
            #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)] )
Romain Therville's avatar
Romain Therville committed
71
            
72
73
74
75
76
            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 :
77
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating run " + str(run_id) )
78
79
                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)
80
81
                logging.getLogger("SwitchProjectSpaceId.process").debug("path_old_run_directory = " + str(path_old_run_directory) )
                logging.getLogger("SwitchProjectSpaceId.process").debug("path_new_run_directory = " + str(path_new_run_directory) )
82
83
84
85
                
                #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)
86
                logging.getLogger("SwitchProjectSpaceId.process").debug("new_run_directory (DB) = " + str(new_run_directory) )
87
88
89
                t3mysql.update_fields('tx_nG6_run', str(run_id), ['directory'], [str(new_run_directory)] )
                
                #Moving the files
90
91
                #if not os.path.isdir(path_new_run_directory) :
                #    os.mkdir(path_new_run_directory,0o775)
92
                
93
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating run files from " + str(path_old_run_directory) + " to " + str(path_new_run_directory) )
94
95
96
97
98
99
100
101
102
103
104
                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
105
106
                old_retention = t3mysql.get_run_creation_date(run_id)
                new_retention = ng6conf.compute_new_retention_date(self.space_id, next(iter(old_retention)))
107
108
109
110
111
                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 :
112
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis " + str(analysis_id) )
113
114
115
116
117
118
119
120
121
                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
122
123
                #if not os.path.isdir(path_new_analysis_directory) :
                #    os.mkdir(path_new_analysis_directory,0o775) 
124
                
125
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis files from " + str(path_old_analysis_directory) + " to " + str(path_new_analysis_directory) )
126
127
128
129
130
131
132
133
134
135
136
                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
137
138
139
                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)
140
141
142
143
                
            #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 :
144
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis " + str(analysis_id) )
145
146
147
148
149
150
151
152
153
                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
154
155
                #if not os.path.isdir(path_new_analysis_directory) :
                #    os.mkdir(path_new_analysis_directory,0o775) 
156
                
157
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis files from " + str(path_old_analysis_directory) + " to " + str(path_new_analysis_directory) )
158
159
160
161
162
163
164
165
166
167
168
                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
169
170
171
                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)