summaryrefslogtreecommitdiff
path: root/releng/management/commands/syncisos.py
blob: f182cc33ff3140a93d5ba7edf332f7110f50f84d (plain)
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
import re
import urllib
from HTMLParser import HTMLParser, HTMLParseError

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.utils.timezone import now

from releng.models import Iso


class IsoListParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)

        self.hyperlinks = []
        self.url_re = re.compile('(?!\.{2})/$')

    def handle_starttag(self, tag, attrs):
        if tag == 'a':
            for name, value in attrs:
                if name == "href":
                    if value != '../' and self.url_re.search(value) is not None:
                        self.hyperlinks.append(value[:-1])

    def parse(self, url):
        try:
            remote_file = urllib.urlopen(url)
            data = remote_file.read()
            remote_file.close()
            self.feed(data)
            self.close()
            return self.hyperlinks
        except HTMLParseError:
            raise CommandError('Couldn\'t parse "%s"' % url)

class Command(BaseCommand):
    help = 'Gets new ISOs from %s' % settings.ISO_LIST_URL

    def handle(self, *args, **options):
        parser = IsoListParser()
        isonames = Iso.objects.values_list('name', flat=True)
        active_isos = parser.parse(settings.ISO_LIST_URL)

        for iso in active_isos:
            # create any names that don't already exist
            if iso not in isonames:
                new = Iso(name=iso, active=True)
                new.save()
            # update those that do if they were marked inactive
            else:
                existing = Iso.objects.get(name=iso)
                if not existing.active:
                    existing.active = True
                    existing.removed = None
                    existing.save(update_fields=('active', 'removed'))
        # and then mark all other names as no longer active
        Iso.objects.filter(active=True).exclude(name__in=active_isos).update(
                active=False, removed=now())

# vim: set ts=4 sw=4 et: