diff --git a/securedrop/manage.py b/securedrop/manage.py index b2b24838720..b848f1d1451 100755 --- a/securedrop/manage.py +++ b/securedrop/manage.py @@ -398,10 +398,22 @@ def init_db(args): os.chown('/var/lib/securedrop/db.sqlite', user.pw_uid, user.pw_gid) +def how_many_submissions_today(args): + count_file = os.path.join(args.data_root, 'submissions_today.txt') + sh(""" + find {dir} -type f -a -mmin -1440 | wc -l > {count_file} + """.format(dir=args.store_dir, + count_file=count_file)) + + def get_args(): parser = argparse.ArgumentParser(prog=__file__, description='Management ' 'and testing utility for SecureDrop.') parser.add_argument('-v', '--verbose', action='store_true') + parser.add_argument('--data-root', + default=config.SECUREDROP_DATA_ROOT, + help=('directory in which the securedrop ' + 'data is stored')) parser.add_argument('--store-dir', default=config.STORE_DIR, help=('directory in which the documents are stored')) @@ -439,6 +451,7 @@ def get_args(): set_translate_messages_parser(subps) set_translate_desktop_parser(subps) + set_how_many_submissions_today(subps) init_db_subp = subps.add_parser('init-db', help='initialize the DB') init_db_subp.add_argument('-u', '--user', @@ -449,6 +462,14 @@ def get_args(): return parser +def set_how_many_submissions_today(subps): + parser = subps.add_parser( + 'how-many-submissions-today', + help=('Update the file containing ' + 'the number of submissions in the past 24h')) + parser.set_defaults(func=how_many_submissions_today) + + def set_translate_parser(subps, parser, translations_dir, diff --git a/securedrop/tests/test_manage.py b/securedrop/tests/test_manage.py index edb91844432..13512847596 100644 --- a/securedrop/tests/test_manage.py +++ b/securedrop/tests/test_manage.py @@ -377,6 +377,21 @@ def test_clean_tmp_removed(self, caplog): manage.clean_tmp(args) assert 'FILE removed' in caplog.text + def test_how_many_submissions_today(self, tmpdir): + data_root = tmpdir + store_dir = tmpdir.join('store') + args = argparse.Namespace(data_root=str(data_root), + store_dir=str(store_dir), + verbose=logging.DEBUG) + + store_dir.join('recent').ensure() + older = store_dir.join('older') + older.ensure() + older.setmtime(time.time() - 2*24*60*60) + manage.how_many_submissions_today(args) + count_file = data_root.join('submissions_today.txt') + assert count_file.read() == "1\n" + class TestSh(object):