Commit 3e42c4d6 authored by Penom Nom's avatar Penom Nom
Browse files

No commit message

No commit message
parent b1ff6699
......@@ -29,6 +29,8 @@ import tarfile, tempfile
from shutil import copyfile, rmtree
import re
import base64
import pty
import sys
class Utils(object):
"""
......@@ -341,4 +343,130 @@ class Utils(object):
dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
@staticmethod
def get_directories_structure_and_content(ng6_username, data_folder, output_folder, prefixed_ids):
from ng6.t3MySQLdb import t3MySQLdb
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
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)
\ No newline at end of file
......@@ -30,14 +30,14 @@ def create_archive( ng6_username, email, data_folder, output_folder, ids_dump_pa
import tarfile
from jflow.utils import robust_rmtree
from workflows.download.lib.utils import get_directories_structure_and_content
from ng6.utils import Utils
fh = open(ids_dump_path, "rb")
prefixed_ids = pickle.load(fh)
fh.close()
prefixed_ids = ";".join(prefixed_ids)
src_directories, dest_directories = get_directories_structure_and_content(ng6_username, data_folder, output_folder, prefixed_ids)
src_directories, dest_directories = Utils.get_directories_structure_and_content(ng6_username, data_folder, output_folder, prefixed_ids)
for i, source_dir in enumerate(src_directories):
dest_dir = dest_directories[i]
......
......@@ -29,8 +29,7 @@ def create_symbolik_link (username, password, ng6_username, data_folder, output_
import subprocess
from ng6.config_reader import NG6ConfigReader
from ng6.utils import Utils
from workflows.download.lib.utils import get_directories_structure_and_content, SSH
from ng6.utils import Utils, SSH
password = Utils.decode_passwd(password)
......@@ -39,7 +38,7 @@ def create_symbolik_link (username, password, ng6_username, data_folder, output_
prefixed_ids = pickle.load(fh)
fh.close()
prefixed_ids = ";".join(prefixed_ids)
src_directories, dest_directories = get_directories_structure_and_content(ng6_username, data_folder, output_folder, prefixed_ids)
src_directories, dest_directories = Utils.get_directories_structure_and_content(ng6_username, data_folder, output_folder, prefixed_ids)
destinations = []
with open(user_script, "w") as us:
......
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