source: asadb/groups/gather_constitutions.py

space-accessstablestage
Last change on this file was c632b1c, checked in by Alex Dehnert <adehnert@…>, 13 years ago

Treat 4XX response codes as constitution failures

Fixes ASA-#57. This changes GroupConstitution?.update() to use urllib2. It also
improves the error handling to detect more errors (such as 404 response codes)
and store more information about the various errors.

  • Property mode set to 100755
File size: 3.3 KB
Line 
1#!/usr/bin/python
2import os
3import sys
4
5if __name__ == '__main__':
6    cur_file = os.path.abspath(__file__)
7    django_dir = os.path.abspath(os.path.join(os.path.dirname(cur_file), '..'))
8    sys.path.append(django_dir)
9    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
10
11import collections
12import datetime
13import subprocess
14
15import django.contrib.auth.models
16import reversion
17
18import groups.models
19
20def gather_constitutions():
21    gs = groups.models.Group.active_groups.all()
22    additions = []
23    changed = []
24    for group in gs:
25        defaults = dict(
26            last_update=datetime.datetime.now(),
27            last_download=datetime.datetime.now(),
28        )
29        constitution, created = groups.models.GroupConstitution.objects.get_or_create(group=group, defaults=defaults)
30        if created: print "created record for %s" % (group, )
31        constitution.source_url = group.constitution_url
32        success, message, old_success = constitution.update()
33        if success:
34            if message == "new path":
35                additions.append(constitution.dest_file)
36            if message == 'updated in place' or message == 'new path':
37                changed.append(group, )
38        if (success != old_success) and (message != "no url"):
39            print "%s: success=%s: %s, url=%s" % (group, success, message, constitution.source_url, )
40    return additions, changed
41
42def update_repo(additions, changed, ):
43    git_dir = groups.models.constitution_dir
44    if additions:
45        subprocess.check_call(['git', 'add', ] + additions, cwd=git_dir, )
46    msg = "Updated constitutions on %s\n\n%d added, %d changed total.\n\n %4s\tGroup\n%s" % (
47        datetime.datetime.now(),
48        len(additions),
49        len(changed),
50        "ID#",
51        "\n".join(["%4d:\t%s" % (group.pk, group.name, ) for group in changed]),
52    )
53    subprocess.check_call(['git', 'commit', '--allow-empty', '-a', '-m', msg, ], cwd=git_dir, )
54
55def webstat():
56    constitutions = groups.models.GroupConstitution.objects.all()
57    codes = collections.defaultdict(list)
58    count = 0
59    for const in constitutions:
60        if count % 10 == 0: print count,
61        code = const.webstat()
62        codes[code].append(const)
63        count += 1
64    for code, gs in codes.items():
65        print "\nCode: %s (count: %d)" % (code, len(gs), )
66        for const in gs:
67            print const.group
68    for code, gs in codes.items():
69        print "\nCode: %s (count: %d)" % (code, len(gs), )
70        for const in gs:
71            print const.source_url
72    print "\n\n"
73    for code, gs in codes.items():
74        print "%4d\t%s" % (len(gs), code, )
75
76def list_constitutions():
77    constitutions = groups.models.GroupConstitution.objects.all()
78    for const in constitutions:
79        if const.dest_file: print const.dest_file
80
81if __name__ == '__main__':
82    if len(sys.argv) == 1 or sys.argv[1] == "gather":
83        with reversion.create_revision():
84            additions, changed = gather_constitutions()
85            importer = django.contrib.auth.models.User.objects.get(username='gather-constitutions@SYSTEM', )
86            reversion.set_user(importer)
87            reversion.set_comment("gather constitutions")
88
89        update_repo(additions, changed)
90    elif sys.argv[1] == "webstat":
91        webstat()
92    elif sys.argv[1] == "list":
93        list_constitutions()
94    else:
95        raise NotImplementedError
Note: See TracBrowser for help on using the repository browser.