Skip to content

Commit

Permalink
manage: implement the how_many_submissions_today sub-command
Browse files Browse the repository at this point in the history
  • Loading branch information
Loic Dachary committed Jan 27, 2018
1 parent 44942a8 commit adc9c5b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions securedrop/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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',
Expand All @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions securedrop/tests/test_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down

0 comments on commit adc9c5b

Please sign in to comment.