Skip to content

Commit 296a3e3

Browse files
committed
adding a delete_file_from_central method, which does NOT delete when in https mode
1 parent 7dcaa1e commit 296a3e3

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

qiita_client/qiita_client.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -820,20 +820,23 @@ def fetch_file_from_central(self, filepath, prefix=None):
820820
"configuration is NOT defined.") % self._plugincoupling)
821821

822822
def push_file_to_central(self, filepath):
823-
"""Pushs filecontent to Qiita's central BASE_DATA_DIR directory.
823+
"""Pushs file- or directory content to Qiita's central BASE_DATA_DIR
824+
directory.
824825
825826
By default, plugin and Qiita's central BASE_DATA_DIR filesystems are
826827
identical. In this case, no files are touched and the filepath is
827828
directly returned.
828829
If however, plugincoupling is set to 'https', the content of the file
829-
is sent via https POST to Qiita's master/worker, which has to receive
830-
and store in an appropriate location.
830+
(or content of recursively all files in the given directory) is sent
831+
via https POST to Qiita's master/worker, which has to receive and store
832+
in an appropriate location.
831833
832834
Parameters
833835
----------
834836
filepath : str
835-
The filepath of the files whos content shall be send to Qiita's
836-
central BASE_DATA_DIR
837+
The filepath of the file(s) whos content shall be send to Qiita's
838+
central BASE_DATA_DIR.
839+
Can be a path to a directory as well.
837840
838841
Returns
839842
-------
@@ -872,3 +875,31 @@ def push_file_to_central(self, filepath):
872875
raise ValueError(
873876
("File communication protocol '%s' as defined in plugins "
874877
"configuration is NOT defined.") % self._plugincoupling)
878+
879+
def delete_file_from_central(self, filepath):
880+
"""Deletes a file in Qiita's central BASE_DATA_DIR directory.
881+
882+
I currently (2025-09-25) assess this operation to be too dangerous for
883+
protocols other than "filesystem", i.e. on "https" the files are NOT
884+
deleted.
885+
However, this might change in the future and since I don't want to
886+
touch every plugin's code again, I am adding this function here already
887+
and use it in according plugin code locations, e.g. function _gzip_file
888+
in qtp-sequencing.
889+
890+
Parameters
891+
----------
892+
filepath : str
893+
The filepath of the file that shall be deletes in Qiita's
894+
central BASE_DATA_DIR
895+
896+
Returns
897+
-------
898+
The given filepath - to be transparent in plugin code.
899+
"""
900+
if self._plugincoupling == 'filesystem':
901+
os.remove(filepath)
902+
elif self._plugincoupling == 'https':
903+
pass
904+
905+
return filepath

qiita_client/tests/test_qiita_client.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from unittest import TestCase, main
1010
import filecmp
1111
from os import remove, close, makedirs
12-
from os.path import basename, exists, expanduser, join, isdir
12+
from os.path import basename, exists, expanduser, join, isdir, dirname
1313
from tempfile import mkstemp
1414
from json import dumps
1515
import pandas as pd
@@ -488,6 +488,43 @@ def test_push_file_to_central_dir(self):
488488
# As we don't necessarily know the QIITA_BASE_DIR, we cannot fetch one
489489
# of the files to double check for it's content
490490

491+
def test_delete_file_from_central(self):
492+
# obtain current filepaths to infer QIITA_BASE_DIR
493+
ainfo = self.tester.get("/qiita_db/artifacts/%s/" % 1)
494+
cwd = dirname(ainfo['files']['raw_forward_seqs'][0]['filepath'])
495+
496+
for protocol in ['filesystem', 'https']:
497+
self.qclient._plugincoupling = protocol
498+
499+
# deposit a test file
500+
fp_test = join(cwd, 'deletme_%s.txt' % protocol)
501+
makedirs(cwd, exist_ok=True)
502+
with open(fp_test, 'w') as f:
503+
f.write('This is a testfile content\n')
504+
self.clean_up_files.append(fp_test)
505+
self.qclient.push_file_to_central(fp_test)
506+
507+
# sanity check that test file has been deposited correctly
508+
fp_obs = self.qclient.fetch_file_from_central(fp_test)
509+
self.assertTrue(exists(fp_obs))
510+
511+
# delete file and test if it is gone
512+
fp_deleted = self.qclient.delete_file_from_central(fp_test)
513+
if protocol == 'filesystem':
514+
# all three fp should point to the same filepath
515+
self.assertFalse(exists(fp_obs))
516+
self.assertFalse(exists(fp_test))
517+
self.assertFalse(exists(fp_deleted))
518+
elif protocol == 'https':
519+
# as of 2025-09-26, I don't allow deletion of qiita main files
520+
# through API endpoints. Thus, the file is NOT deleted!
521+
# local version of the file
522+
self.assertTrue(exists(fp_test))
523+
# qiita main filepath
524+
self.assertTrue(exists(fp_obs))
525+
# qiita main filepath, returned by delete_file_from_central
526+
self.assertTrue(exists(fp_deleted))
527+
491528

492529
if __name__ == '__main__':
493530
main()

0 commit comments

Comments
 (0)