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