__init__.py 9.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
#
# 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
            project = Project.get_from_id(self.project_id)
            old_space_id = project.space_id
53
            
Romain Therville's avatar
Romain Therville committed
54
55
            #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) :
56
                raise Exception("The project " + project.name + " already belongs to the space_id '" + str(self.space_id) + "'" )
Romain Therville's avatar
Romain Therville committed
57
58
                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
            #Update to the projcet's space_id
            t3mysql = t3MySQLdb()
70
            project.update_space_id(self.space_id)
Romain Therville's avatar
Romain Therville committed
71
            
72
73
74
            base_path = ng6conf.get_save_directory()
            
            #Process for the project's runs
75
            runs_ids = project.get_project_run_ids()
76
            for run_id in runs_ids :
77
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating run " + str(run_id) )
78
79
80
                run = Run.get_from_id(run_id)
                
                path_old_run_directory = base_path + run.get_directory()
81
82
83
                path_new_run_directory = path_old_run_directory.replace(old_directory,new_directory)
                
                #Update the directory in DB
84
                new_run_directory = run.get_directory()
85
                new_run_directory = new_run_directory.replace(old_directory, new_directory)
86
                run.set_directory(new_run_directory)
87
88
                
                #Moving the files
89
90
                #if not os.path.isdir(path_new_run_directory) :
                #    os.mkdir(path_new_run_directory,0o775)
91
                
92
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating run files from " + str(path_old_run_directory) + " to " + str(path_new_run_directory) )
93
94
95
96
97
98
99
100
101
102
103
                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
104
105
106
                old_retention = run.get_creation_date()
                new_retention = ng6conf.compute_new_retention_date(self.space_id,old_retention)
                run.set_retention(new_retention)                
107
108
                
            #Process for the project's analyzes
109
            analyzes_ids = project.get_project_analyzes_ids()
110
            for analysis_id in analyzes_ids :
111
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis " + str(analysis_id) )
112
113
114
                analysis = Analysis.get_from_id(analysis_id)
                
                path_old_analysis_directory = base_path + analysis.get_directory()
115
116
117
                path_new_analysis_directory = path_old_analysis_directory.replace(old_directory,new_directory)
                
                #Update the directory in DB
118
                new_analysis_directory = analysis.get_directory()
119
                new_analysis_directory = new_analysis_directory.replace(old_directory, new_directory)
120
                analysis.set_directory(new_analysis_directory)
121
122
                
                #Moving the files
123
124
                #if not os.path.isdir(path_new_analysis_directory) :
                #    os.mkdir(path_new_analysis_directory,0o775) 
125
                
126
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis files from " + str(path_old_analysis_directory) + " to " + str(path_new_analysis_directory) )
127
128
129
130
131
132
133
134
135
136
137
                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
138
139
140
                old_retention = analysis.get_creation_date()
                new_retention = ng6conf.compute_new_retention_date(self.space_id,old_retention)
                analysis.set_retention(new_retention)
141
142
                
            #Process for the project's run's analyzes
143
            run_analyzes_ids = project.get_project_runs_analyzes_ids()
144
            for analysis_id in run_analyzes_ids :
145
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis " + str(analysis_id) )
146
147
148
                analysis = Analysis.get_from_id(analysis_id)
                
                path_old_analysis_directory = base_path + analysis.get_directory()
149
150
151
                path_new_analysis_directory = path_old_analysis_directory.replace(old_directory,new_directory)
                
                #Update the directory in DB
152
                new_analysis_directory = analysis.get_directory()
153
                new_analysis_directory = new_analysis_directory.replace(old_directory, new_directory)
154
                analysis.set_directory(new_analysis_directory)
155
156
                
                #Moving the files
157
158
                #if not os.path.isdir(path_new_analysis_directory) :
                #    os.mkdir(path_new_analysis_directory,0o775) 
159
                
160
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis files from " + str(path_old_analysis_directory) + " to " + str(path_new_analysis_directory) )
161
162
163
164
165
166
167
168
169
170
171
                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
172
173
174
                old_retention = analysis.get_creation_date()
                new_retention = ng6conf.compute_new_retention_date(self.space_id,old_retention)
                analysis.set_retention(new_retention)