move_project.py 5.21 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
#
# 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 logging
from jflow.component import Component
from workflows.types import ng6space,existingprojectid
from ng6.t3MySQLdb import t3MySQLdb

def migrate_project (project_id, new_space_id, output):
    import os
    from subprocess import call
    import logging

    from ng6.config_reader import NG6ConfigReader
    from ng6.analysis import Analysis
    from ng6.project import Project
    from ng6.run import Run

    with open( output,'w') as fh :
        ng6conf = NG6ConfigReader()
        base_path = ng6conf.get_save_directory()
        
        project = Project.get_from_id(project_id)
        
        logging.getLogger("migrate_project").debug("Migrating project '" + str(project.name) + "' (" + str(project.id) + ") to " + str(new_space_id) + "\n")
        fh.write("Migrating project '" + str(project.name) + "' (" + str(project.id) + ") to " + str(new_space_id) + "\n")
        
        old_space_id = project.space_id
43
        logging.getLogger("migrate_project").debug("old_space_id = " + old_space_id + ", new_space_id = " + new_space_id + "\n")
44
45
46
47
48
49
        fh.write("old_space_id = " + old_space_id + ", new_space_id = " + new_space_id + "\n")
        
        str_cmds = []
        runs = project.get_runs()
        for run in runs :
            fh.write("Migrating run '" + run.name + "' (" + str(run.id) + ")" + " to " + str(new_space_id) + "\n")
50
            logging.getLogger("migrate_project").debug("Migrating run '" + run.name + "' (" + str(run.id) + ")" + " to " + str(new_space_id) + "\n")
51
52
            str_cmds.append(run.change_space(new_space_id) )
            fh.write("Migrating successful! \n")
53
            logging.getLogger("migrate_project").debug("Migrating successful! \n")
54
55
56
57
            
            run_analyzes = run.get_analysis()
            for analysis in run_analyzes :
                fh.write("Migrating analysis '" + analysis.name + "' (" + str(analysis.id) + ")" + " to " + str(new_space_id) + "\n")
58
                logging.getLogger("migrate_project").debug("Migrating analysis '" + analysis.name + "' (" + str(analysis.id) + ")" + " to " + str(new_space_id) + "\n")
59
60
                str_cmds.append(analysis.change_space(new_space_id) )
                fh.write("Migrating successful! \n")
61
                logging.getLogger("migrate_project").debug("Migrating successful! \n")
62
63
64
65
            
        analyzes = project.get_analysis()
        for analysis in analyzes :
            fh.write("Migrating analysis '" + analysis.name + "' (" + str(analysis.id) + ")" + " to " + str(new_space_id) + "\n")
66
            logging.getLogger("migrate_project").debug("Migrating analysis '" + analysis.name + "' (" + str(analysis.id) + ")" + " to " + str(new_space_id) + "\n")
67
68
            str_cmds.append(analysis.change_space(new_space_id) )
            fh.write("Migrating successful! \n")
69
            logging.getLogger("migrate_project").debug("Migrating successful! \n")
70
71
72
        
        for command in str_cmds:
            fh.write("Command launched : "+ str(command[1]) + "\n" )
73
            logging.getLogger("migrate_project").debug("Command launched : "+ str(command[1]) + "\n" )
74
            fh.write("Returned code : "+ str(command[0]) + "\n" )
75
            logging.getLogger("migrate_project").debug("Returned code : "+ str(command[0]) + "\n" )
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        
        project.update_space_id(new_space_id)
        

class MoveProject (Component):
    
    def define_parameters(self, project_id, space_id,):
        self.add_parameter('project_id', 'The project id', type = existingprojectid, required = True, default = project_id )
        self.add_parameter('space_id', 'The new space_id', type = ng6space, required = True, default = space_id)
        self.add_output_file("stdout", "MoveProject stdout", filename="MoveProject.stdout")
        
    def process(self):
        logging.getLogger("MoveProject.process").debug(" Begin MoveProject Component.\n")
        logging.getLogger("MoveProject.process").debug(" self.space_id="+str(self.space_id)+"\n")
        logging.getLogger("MoveProject.process").debug(" self.project_id="+str(self.project_id)+"\n")
        logging.getLogger("MoveProject.process").debug(" stdout = "+str(self.stdout)+"\n")
        
        self.add_python_execution(migrate_project, cmd_format="{EXE} {ARG} {OUT}",
94
                                      outputs = self.stdout, arguments = [self.project_id, self.space_id], local = True)
95
96
97
        #self.add_python_execution(migrate_project, cmd_format="{EXE} {ARG}",
        #                              arguments = [self.project_id, self.space_id])