'Find stale builds and remove build paths'

def handle(self, *args, **options):

    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:
            version = Version.objects.get(id=build['version'])
            latest_build = version.builds.latest('date')
            if (latest_build.date > max_date):
                log.warn('{0}    is    newer    than    {1}'.format(latest_build, max_date))
            path = version.get_build_path()
            if (path is not None):
                log.info('Found    stale    build    path    for    {0}    at    {1},    last    used    on    {2}'.format(version, path, latest_build.date))
                if (not options['dryrun']):
                    version.clean_build_path()
        except Version.DoesNotExist:
            pass
