petale/petale/management/commands/clean.py

52 lines
2.0 KiB
Python

# Petale - Simple App as Key/Value Storage Interface
# Copyright (C) 2017 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import itertools
from django.core.management.base import BaseCommand
from petale import models
from petale.api_views import check_unknown_cuts
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return itertools.zip_longest(fillvalue=fillvalue, *args)
class Command(BaseCommand):
help = 'Clean petale data'
def add_arguments(self, parser):
parser.add_argument('--delete', action='store_true', help='Delete dead petals.')
def handle(self, *args, delete=False, **options):
zombie_uuids = set()
for cuts in grouper(500, models.CUT.objects.iterator()):
zombie_uuids.update(check_unknown_cuts({cut.uuid for cut in cuts if cut is not None}))
if options['verbosity'] > 1:
print('Found %d dead cuts on %d known cuts.' % (len(zombie_uuids), models.CUT.objects.count()))
if delete:
count, count_by_models = models.CUT.objects.filter(uuid__in=zombie_uuids).delete()
if count and options['verbosity'] > 0:
print(
'Deleted ',
', '.join(
'%d %s' % (count, model.split('.')[-1]) for model, count in count_by_models.items()
),
)