Commit b1ff6699 authored by Penom Nom's avatar Penom Nom
Browse files

No commit message

No commit message
parent 69411079
#
# Copyright (C) 2012 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/>.
#
from jflow.workflow import Workflow
from ng6.config_reader import NG6ConfigReader
class Download (Workflow):
def get_description(self):
return "Download selected data"
def define_parameters(self, function="process"):
config = NG6ConfigReader()
self.save_directory = config.get_save_directory()
self.add_parameter('login', 'The login of the user in ng6', required = True, type = 'ng6userlogin')
self.add_parameter_list('data_id', 'Ids of a run from which rawdata will be retrieved', type = 'existingrun')
self.add_parameter_list('run_id', 'Ids of run from which all data will be retrieved', type = 'existingrun')
self.add_parameter_list('analysis_id', 'Ids of analysis to retrieve', type = 'existinganalysis')
self.add_multiple_parameter("home_link", """The home link will create symbolic links of the selected data for the provided user in the destination.
The user must have write permission on the destination directory""")
self.add_parameter('username', 'A valid user username on the server', type = "localuser" ,required = True, add_to ="home_link")
self.add_parameter('password', 'The connection password for the username on the server', type="password", required = True, add_to ="home_link")
self.add_parameter('user_directory', 'The directory where the links will be created.This directory must exists on the server.', default = config.get_server_directory(),
required = True, add_to ="home_link", type="localdirectory")
self.add_parameter('link_mail', 'Provide an email to be warned when the process is over', display_name = "User mail", required = True, type = "email" , add_to ="home_link")
self.add_multiple_parameter("local_archive", "This option will create an archive that the user can download with a link")
self.add_parameter('archive_mail', 'Provide a valid email address to be warned when the process is over', display_name = "User mail", type="email", required = True, add_to ="local_archive")
self.add_exclusion_rule("home_link", "local_archive")
def process(self):
config = NG6ConfigReader()
save_directory = config.get_save_directory()
if self.home_link['username'] and self.home_link['password'] :
self.add_component('CreateSymlink', [self.login, self.home_link['username'], self.home_link['password'],
self.save_directory, self.home_link['user_directory'],self.home_link['link_mail'],
self.data_id, self.run_id, self.analysis_id])
elif self.local_archive['archive_mail'] :
self.add_component('CreateArchive', [self.login, self.save_directory, self.local_archive['archive_mail'], self.data_id, self.run_id, self.analysis_id])
#
# Copyright (C) 2012 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/>.
#
\ No newline at end of file
#
# 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 re
import pty
import sys
from ng6.t3MySQLdb import t3MySQLdb
class SSH(object):
def __init__(self, user, passwd, hostname, port=22):
self.username = user
self.password = passwd
self.hostname = hostname
self.port = port
def run_cmd(self, c):
(pid, f) = pty.fork()
if pid == 0:
os.execlp('ssh', 'ssh', '-p %d' % self.port, '-o StrictHostKeyChecking=no' ,
self.username + '@' + self.hostname, c)
else:
return (pid, f)
def _read(self, f):
x = ''
try:
x = os.read(f, 1024)
except Exception, e:
# this always fails with io error
pass
return x
def ssh_results(self, pid, f):
output = ''
got = self._read(f) # check for authenticity of host request
m = re.search( 'assword:', got)
if m:
# send passwd
os.write(f, self.password + '\n')
# read two lines
tmp = self._read(f)
tmp += self._read(f)
m = re.search('Permission denied', tmp)
if m:
raise Exception( 'Invalid password')
# passwd was accepted
got = tmp
while got and len(got) > 0:
output += got
got = self._read(f)
os.waitpid(pid, 0)
os.close(f)
return output
def cmd(self, c):
(pid, f) = self.run_cmd(c)
return self.ssh_results(pid, f)
def get_directories_structure_and_content(ng6_username, data_folder, output_folder, prefixed_ids):
src_directories = []
dest_directories = []
t3mysql = t3MySQLdb()
user_id = None
try : user_id = t3mysql.get_user_id(ng6_username)
except : pass
for prefixed_id in prefixed_ids.split(';') :
vals = prefixed_id.split('_')
prefix = vals[0]
id = vals[1]
if prefix == "data" or prefix == "run" :
run = t3mysql.select_run_informations(id)
source_dir = data_folder +'/'+ run['directory']
project_name = "Project_%s.%s" % ( re.sub( r"[\s\/]", "_", run['project_name']), run['project_id'] )
run_name = "Run_%s.%s" %( run['name'].replace(' ', '_').replace('/', '_'), id )
raw_data_dir = os.path.join( output_folder, project_name, run_name, "RawData" )
if source_dir not in src_directories :
src_directories.append(source_dir)
if raw_data_dir not in dest_directories :
dest_directories.append(raw_data_dir)
if prefix == "run" :
for analyse_id, analyse_values in t3mysql.get_user_run_analysis(user_id, id, 'tx_nG6_analyze.name').items() :
analysis_dir = data_folder + '/' + analyse_values["directory"]
analysis_name = "Analyse_%s.%s"%( re.sub( r"[\s\/]", "_", analyse_values['name']), analyse_id )
dest_analysis_dir = os.path.join( output_folder, project_name, run_name, analysis_name )
if analysis_dir not in src_directories :
src_directories.append(analysis_dir)
if dest_analysis_dir not in dest_directories :
dest_directories.append(dest_analysis_dir)
elif prefix == "analyse" :
analyse = t3mysql.select_analysis_informations(id)
source_dir = data_folder +'/'+ analyse["directory"]
if source_dir not in src_directories :
src_directories.append(source_dir)
project_name = "Project_%s.%s" % ( re.sub( r"[\s\/]", "_", analyse["project_name"]), analyse["project_id"])
# it's a run analysis
if analyse["run_id"]:
run_name = "Run_%s.%s" % ( re.sub( r"[\s\/]", "_", analyse["run_name"]), analyse["run_id"])
analysis_name = "Analyse_%s.%s" % ( re.sub( r"[\s\/]", "_", analyse["name"]), id)
dest_analysis_dir = os.path.join( output_folder, project_name, run_name, analysis_name )
if dest_analysis_dir not in dest_directories :
dest_directories.append(dest_analysis_dir)
# it's a project analysis
else :
analysis_name = "Project_analyses/%s.%s" % ( re.sub( r"[\s\/]", "_", analyse["name"]), id)
dest_analysis_dir = os.path.join( output_folder, project_name, analysis_name )
if dest_analysis_dir not in dest_directories :
dest_directories.append(dest_analysis_dir)
if len(src_directories) != len(dest_directories) :
raise Exception("Error while retrieveing directory structure and content")
sources, destinations = [], []
for i, e in enumerate(src_directories):
if os.path.isdir(e) :
sources.append(e)
destinations.append(dest_directories[i])
return sources, destinations
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment