Skip to content

Commit ace249a

Browse files
committed
addressing @ElDeveloper comments
1 parent 6d8674a commit ace249a

File tree

4 files changed

+30
-44
lines changed

4 files changed

+30
-44
lines changed

qiita_db/analysis.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
# -----------------------------------------------------------------------------
1919
from __future__ import division
2020
from itertools import product
21-
from os.path import join
22-
from subprocess import Popen, PIPE
21+
from os.path import join, basename
22+
from tarfile import open as taropen
2323

2424
from future.utils import viewitems
2525
from biom import load_table
@@ -798,17 +798,20 @@ def generate_tgz(self):
798798

799799
_, analysis_mp = qdb.util.get_mountpoint('analysis')[0]
800800
tgz = join(analysis_mp, '%d_files.tgz' % self.id)
801-
cmd = 'tar zcf %s %s' % (tgz, ' '.join(full_fps))
802-
803-
proc = Popen(cmd, universal_newlines=True, shell=True, stdout=PIPE,
804-
stderr=PIPE)
805-
stdout, stderr = proc.communicate()
806-
return_value = proc.returncode
801+
try:
802+
with taropen(tgz, "w:gz") as tar:
803+
for f in full_fps:
804+
tar.add(f, arcname=basename(f))
805+
error_txt = ''
806+
return_value = 0
807+
except Exception as e:
808+
error_txt = str(e)
809+
return_value = 1
807810

808811
if return_value == 0:
809812
self._add_file(tgz, 'tgz')
810813

811-
return stdout, stderr, return_value
814+
return '', error_txt, return_value
812815

813816
def build_files(self,
814817
rarefaction_depth=None,

qiita_db/support_files/patches/python_patches/41.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
2-
from subprocess import Popen, PIPE
3-
from os.path import exists, join
1+
from os.path import exists, join, basename
2+
from tarfile import open as taropen
43

54
from qiita_db.sql_connection import TRN
65
from qiita_db.artifact import Artifact
@@ -34,15 +33,8 @@
3433

3534
tgz = to_tgz + '.tgz'
3635
if not exists(tgz):
37-
cmd = 'tar zcf %s %s' % (tgz, to_tgz)
38-
proc = Popen(cmd, universal_newlines=True, shell=True, stdout=PIPE,
39-
stderr=PIPE)
40-
stdout, stderr = proc.communicate()
41-
return_value = proc.returncode
42-
if return_value != 0:
43-
raise ValueError(
44-
"There was an error:\nstdout:\n%s\n\nstderr:\n%s"
45-
% (stdout, stderr))
36+
with taropen(tgz, "w:gz") as tar:
37+
tar.add(to_tgz, arcname=basename(to_tgz))
4638

4739
a_id = a.id
4840
# Add the new tgz file to the artifact.
@@ -88,15 +80,9 @@
8880
if not exists(tgz):
8981
full_fps = [join(get_mountpoint_path_by_id(mid), f)
9082
for f, mid in fps]
91-
cmd = 'tar zcf %s %s' % (tgz, ' '.join(full_fps))
92-
proc = Popen(cmd, universal_newlines=True, shell=True, stdout=PIPE,
93-
stderr=PIPE)
94-
stdout, stderr = proc.communicate()
95-
return_value = proc.returncode
96-
if return_value != 0:
97-
raise ValueError(
98-
"There was an error:\nstdout:\n%s\n\nstderr:\n%s"
99-
% (stdout, stderr))
83+
with taropen(tgz, "w:gz") as tar:
84+
for f in full_fps:
85+
tar.add(f, arcname=basename(f))
10086

10187
# Add the new tgz file to the analysis.
10288
fp_ids = insert_filepaths([(tgz, tgz_id)], analysis_id, 'analysis',

qiita_plugins/target_gene/tgp/pick_otus.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
# The full license is in the file LICENSE, distributed with this software.
77
# -----------------------------------------------------------------------------
88

9-
from os.path import join
9+
from os.path import join, basename
1010
from functools import partial
1111
from glob import glob
12+
from tarfile import open as taropen
1213

1314
from qiita_client import format_payload
1415

@@ -103,9 +104,8 @@ def generate_sortmerna_tgz(out_dir):
103104
"""
104105
to_tgz = join(out_dir, 'sortmerna_picked_otus')
105106
tgz = to_tgz + '.tgz'
106-
cmd = 'tar zcf %s %s' % (tgz, to_tgz)
107-
108-
return cmd
107+
with taropen(tgz, "w:gz") as tar:
108+
tar.add(to_tgz, arcname=basename(to_tgz))
109109

110110

111111
def generate_artifact_info(pick_out):
@@ -189,11 +189,10 @@ def pick_closed_reference_otus(qclient, job_id, parameters, out_dir):
189189

190190
qclient.update_job_step(job_id,
191191
"Step 4 of 4: Generating tgz sortmerna folder")
192-
command = generate_sortmerna_tgz(pick_out)
193-
std_out, std_err, return_value = system_call(command)
194-
if return_value != 0:
195-
error_msg = ("Error while tgz failures:\nStd out: %s\nStd err: %s"
196-
% (std_out, std_err))
192+
try:
193+
generate_sortmerna_tgz(pick_out)
194+
except Exception as e:
195+
error_msg = ("Error while tgz failures:\nError: %s" % str(e))
197196
return format_payload(False, error_msg=error_msg)
198197

199198
artifacts_info = generate_artifact_info(pick_out)

qiita_plugins/target_gene/tgp/tests/test_pick_otus.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from unittest import TestCase, main
1010
from os.path import isdir, exists, join
11-
from os import remove, close
11+
from os import remove, close, mkdir
1212
from shutil import rmtree
1313
from tempfile import mkstemp, mkdtemp
1414

@@ -70,10 +70,8 @@ def test_generate_pick_closed_reference_otus_cmd(self):
7070
def test_generate_sortmerna_tgz(self):
7171
outdir = mkdtemp()
7272
self._clean_up_files.append(outdir)
73-
obs = generate_sortmerna_tgz(outdir)
74-
exp = ("tar zcf %s/sortmerna_picked_otus.tgz "
75-
"%s/sortmerna_picked_otus" % (outdir, outdir))
76-
self.assertEqual(obs, exp)
73+
mkdir(join(outdir, 'sortmerna_picked_otus'))
74+
self.assertIsNone(generate_sortmerna_tgz(outdir))
7775

7876
def test_generate_pick_closed_reference_otus_cmd_valueerror(self):
7977
filepaths = [('/directory/seqs.log', 'log'),

0 commit comments

Comments
 (0)