1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
18 This simple script checks whether the `_numConferences` attribute is consistent for
19 every category, and fixs them otherwise.
21 It can be added as a periodic cronjob.
26 from indico
.core
.db
import DBMgr
27 from MaKaC
.conference
import CategoryManager
, ConferenceHolder
31 def set_num_conferences(categ
, num
):
32 categ
._numConferences
= num
35 def check_event_number_consistency(root
):
37 for categ
in root
.subcategories
.itervalues():
38 check_event_number_consistency(categ
)
44 for scat
in root
.subcategories
.itervalues():
45 total
+= scat
._numConferences
46 if root
._numConferences
!= total
:
47 sys
.stderr
.write("(n) '%s': expected %d, got %d! " % (root
.getId(), total
, root
._numConferences
))
48 set_num_conferences(root
, total
)
49 sys
.stderr
.write("[FIXED]\n")
53 def check_event_number_leaves(categ
, expected
, dbi
):
54 obtained
= categ
._numConferences
56 if expected
!= obtained
:
58 sys
.stderr
.write( "(l) '%s': expected %d, got %d! " % (cid
, expected
, obtained
))
61 set_num_conferences(categ
, expected
)
62 sys
.stderr
.write("[FIXED]\n")
66 if __name__
== '__main__':
67 dbi
= DBMgr
.getInstance()
70 confIndex
= ConferenceHolder()._getIdx
()
71 index
= CategoryManager()._getIdx
()
73 for cid
, categ
in index
.iteritems():
74 # conferece-containing categories
77 # since we're here check consistency of TreeSet
78 categ
.conferences
._check
()
81 for conf
in categ
.conferences
:
83 if conf
.getId() not in confIndex
:
84 sys
.stderr
.write("[%s] '%s' not in ConferenceHolder!\n" % (cid
, conf
.getId()))
87 check_event_number_leaves(categ
, lenConfs
, dbi
)
89 # check that there are no subcategories
90 if categ
.subcategories
:
91 sys
.stderr
.write("'%s' has subcategories AND conferences!\n")
93 # as for category-containing categories
95 for subcat
in categ
.subcategories
:
96 if subcat
not in index
:
97 sys
.stderr
.write("'%s' is a child of '%s' but is not in the CategoryManager" % (subcat
, cid
))
99 # check that the the category has a parent
100 if categ
.getOwner() == None and cid
!= '0':
101 sys
.stderr
.write("'%s' has no owner!\n")
103 # check that the contained id is the same as the indexation key
104 if categ
.getId() != cid
:
105 sys
.stderr
.write("'%s' indexed as '%s'\n" % (categ
.getId(), cid
))
107 # now check that the event number totals are OK
108 check_event_number_consistency(CategoryManager().getById('0'))