excercises: uporzadkuj, luki, zastap
[redakcja.git] / scripts / fix_something.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import sys
7 sys.path.append('.')
8 sys.path.append('./apps')
9 sys.path.append('./lib')
10
11 from django.core.management import setup_environ
12 from redakcja import settings
13
14 setup_environ(settings)
15
16 from catalogue.models import Book, Chunk
17 import re
18
19 fixed = {}
20
21 tag_with_name = r"<([^>]+)name=\"([^>]+)>"
22
23 def fix(book, author, dry_run=True):
24     if len(book) == 0:
25         print "%s ==> does not contain chunks" % book.slug
26         return
27     fc = book[0]
28     txt = fc.materialize()
29
30     newtxt, cnt = re.subn(tag_with_name, r'<\1nazwa="\2>', txt)
31     if cnt == 0:
32         print "%s ==> nothing changed" % book.slug
33         return
34     
35     if not dry_run:
36         print "%s ==> changing" % book.slug
37         fc.commit(newtxt, author=author, description=u"Automatyczna zmiana atrybutu name na nazwa")
38     else:
39         print "%s ==> i would change this" % book.slug
40
41
42 import sys
43 import getopt
44 from django.contrib.auth.models import User
45 opts, oth_ = getopt.getopt(sys.argv[1:],
46     [],
47     [ "seriously"])
48 dry_run = not (("--seriously",'') in opts)
49 me = User.objects.get(username='marcinkoziej')
50 if dry_run:
51     print "This is a dry run, to really fix something, run with --seriously"
52 for b in Book.objects.all():
53     fix(b, me, dry_run)
54
55     
56