Skip to content

Commit 7dcaa1e

Browse files
committed
add ability to fetch all files of a given directory (ensure compatibility with old py27 for tests)
1 parent 878df24 commit 7dcaa1e

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

qiita_client/qiita_client.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import pandas as pd
1515
from json import dumps
1616
from random import randint
17+
import fnmatch
1718

1819
try:
1920
from itertools import zip_longest
@@ -842,16 +843,29 @@ def push_file_to_central(self, filepath):
842843
return filepath
843844

844845
elif self._plugincoupling == 'https':
845-
logger.debug('Submitting file %s to qiita server.' % filepath)
846+
logger.debug('Submitting %s %s to qiita server.' % (
847+
'directory' if os.path.isdir(filepath) else 'file', filepath))
846848

847849
# target path, i.e. without filename
848850
dirpath = os.path.dirname(filepath)
849851
if dirpath == "":
850852
dirpath = "/"
851853

852-
self.post(
853-
'/cloud/push_file_to_central/',
854-
files={dirpath: open(filepath, 'rb')})
854+
if os.path.isdir(filepath):
855+
# Pushing all files of a directory, not only a single file.
856+
# Cannot use "glob" as it lacks the "recursive" parameter in
857+
# py27. This is used e.g. in qp-target-gene
858+
for root, dirnames, filenames in os.walk(filepath):
859+
for filename in fnmatch.filter(filenames, "*"):
860+
fp = os.path.join(root, filename)
861+
self.post('/cloud/push_file_to_central/',
862+
files={os.path.join(
863+
dirpath,
864+
os.path.dirname(fp)): open(fp, 'rb')})
865+
else:
866+
self.post(
867+
'/cloud/push_file_to_central/',
868+
files={dirpath: open(filepath, 'rb')})
855869

856870
return filepath
857871

qiita_client/tests/test_qiita_client.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
from unittest import TestCase, main
1010
import filecmp
11-
from os import remove, close
12-
from os.path import basename, exists, expanduser, join
11+
from os import remove, close, makedirs
12+
from os.path import basename, exists, expanduser, join, isdir
1313
from tempfile import mkstemp
1414
from json import dumps
1515
import pandas as pd
16+
from shutil import rmtree
1617

1718
from qiita_client.qiita_client import (QiitaClient, _format_payload,
1819
ArtifactInfo)
@@ -110,7 +111,10 @@ def setUp(self):
110111
def tearDown(self):
111112
for fp in self.clean_up_files:
112113
if exists(fp):
113-
remove(fp)
114+
if isdir(fp):
115+
rmtree(fp)
116+
else:
117+
remove(fp)
114118

115119
def test_init(self):
116120
obs = QiitaClient(URL,
@@ -184,7 +188,7 @@ def test_patch(self):
184188
with open(fp, 'w') as f:
185189
f.write('\n')
186190
self.clean_up_files.append(fp)
187-
obs = self.tester.patch(f'/qiita_db/artifacts/{artifact_id}/', 'add',
191+
obs = self.tester.patch('/qiita_db/artifacts/%s/' % artifact_id, 'add',
188192
'/html_summary/', value=fp)
189193
self.assertIsNone(obs)
190194

@@ -440,6 +444,50 @@ def test_push_file_to_central(self):
440444
fp_obs = self.tester.push_file_to_central(fp_source)
441445
self.assertEqual(fp_source, fp_obs)
442446

447+
def _create_test_dir(self, prefix=None):
448+
"""Creates a test directory with files and subdirs."""
449+
# prefix
450+
# |- testdir/
451+
# |---- fileA.txt
452+
# |---- subdirA_l1/
453+
# |-------- fileB.fna
454+
# |-------- subdirC_l2/
455+
# |------------ fileC.log
456+
# |------------ fileD.seq
457+
# |---- subdirB_l1/
458+
# |-------- fileE.sff
459+
if (prefix is not None) and (prefix != ""):
460+
prefix = join(prefix, 'testdir')
461+
else:
462+
prefix = 'testdir'
463+
464+
for dir in [join(prefix, 'subdirA_l1', 'subdirC_l2'),
465+
join(prefix, 'subdirB_l1')]:
466+
if not exists(dir):
467+
makedirs(dir)
468+
for file, cont in [(join(prefix, 'fileA.txt'), 'contentA'),
469+
(join(prefix, 'subdirA_l1',
470+
'fileB.fna'), 'this is B'),
471+
(join(prefix, 'subdirA_l1', 'subdirC_l2',
472+
'fileC.log'), 'call me c'),
473+
(join(prefix, 'subdirA_l1', 'subdirC_l2',
474+
'fileD.seq'), 'I d'),
475+
(join(prefix, 'subdirB_l1', 'fileE.sff'), 'oh e')]:
476+
with open(file, "w") as f:
477+
f.write(cont + "\n")
478+
self.clean_up_files.append(prefix)
479+
480+
return prefix
481+
482+
def test_push_file_to_central_dir(self):
483+
self.tester._plugincoupling = 'https'
484+
485+
fp_source = self._create_test_dir('/tmp/test_push_dir/')
486+
fp_obs = self.tester.push_file_to_central(fp_source)
487+
self.assertEqual(fp_source, fp_obs)
488+
# As we don't necessarily know the QIITA_BASE_DIR, we cannot fetch one
489+
# of the files to double check for it's content
490+
443491

444492
if __name__ == '__main__':
445493
main()

0 commit comments

Comments
 (0)