-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
clean_builds.py
63 lines (54 loc) · 2.17 KB
/
clean_builds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Clean up stable build paths per project version"""
from __future__ import absolute_import
from datetime import datetime, timedelta
import logging
from optparse import make_option
from django.core.management.base import BaseCommand
from django.db.models import Max
from readthedocs.builds.models import Build, Version
log = logging.getLogger(__name__)
class Command(BaseCommand):
help = __doc__
option_list = BaseCommand.option_list + (
make_option('--days',
dest='days',
type='int',
default=365,
help='Find builds older than DAYS days, default: 365'),
make_option('--dryrun',
action='store_true',
dest='dryrun',
help='Perform dry run on build cleanup'),
)
def handle(self, *args, **options):
"""Find stale builds and remove build paths"""
max_date = datetime.now() - timedelta(days=options['days'])
queryset = (Build.objects
.values('project', 'version')
.annotate(max_date=Max('date'))
.filter(max_date__lt=max_date)
.order_by('-max_date'))
for build in queryset:
try:
# Get version from build version id, perform sanity check on
# latest build date
version = Version.objects.get(id=build['version'])
latest_build = version.builds.latest('date')
if latest_build.date > max_date:
log.warning(
'%s is newer than %s',
latest_build,
max_date,
)
path = version.get_build_path()
if path is not None:
log.info(
'Found stale build path for %s at %s, last used on %s',
version,
path,
latest_build.date,
)
if not options['dryrun']:
version.clean_build_path()
except Version.DoesNotExist:
pass