|
a/scripts/migrations/011-fix-subroles.py |
|
b/scripts/migrations/011-fix-subroles.py |
|
... |
|
... |
10 |
Member
|
10 |
Member
|
11 |
"""
|
11 |
"""
|
12 |
import sys
|
12 |
import sys
|
13 |
import logging
|
13 |
import logging
|
14 |
|
14 |
|
15 |
from pylons import c
|
|
|
16 |
from ming.orm import session
|
15 |
from ming.orm import session
|
17 |
from ming.orm.ormsession import ThreadLocalORMSession
|
16 |
from ming.orm.ormsession import ThreadLocalORMSession
|
18 |
|
17 |
|
19 |
from allura import model as M
|
18 |
from allura import model as M
|
|
|
19 |
from allura.lib import utils
|
20 |
|
20 |
|
21 |
log = logging.getLogger('fix-subroles')
|
21 |
log = logging.getLogger('fix-subroles')
|
22 |
log.addHandler(logging.StreamHandler(sys.stdout))
|
22 |
log.addHandler(logging.StreamHandler(sys.stdout))
|
23 |
|
23 |
|
24 |
def main():
|
24 |
def main():
|
25 |
test = sys.argv[-1] == 'test'
|
25 |
test = sys.argv[-1] == 'test'
|
26 |
num_projects_examined = 0
|
26 |
num_projects_examined = 0
|
27 |
log.info('Examining subroles in all non-user projects.')
|
27 |
log.info('Examining subroles in all non-user projects.')
|
28 |
n_users = M.Neighborhood.query.get(name='Users')
|
28 |
n_users = M.Neighborhood.query.get(name='Users')
|
29 |
project_filter = dict(neighborhood_id={'$ne':n_users._id})
|
29 |
project_filter = dict(neighborhood_id={'$ne':n_users._id})
|
30 |
for some_projects in chunked_project_iterator(project_filter):
|
30 |
for some_projects in utils.chunked_find(M.Project, project_filter):
|
31 |
for project in some_projects:
|
31 |
for project in some_projects:
|
32 |
project_name = '%s.%s' % (project.neighborhood.name, project.shortname)
|
32 |
project_name = '%s.%s' % (project.neighborhood.name, project.shortname)
|
33 |
project_roles = {}
|
33 |
project_roles = {}
|
34 |
for parent, child in [('Admin', 'Developer'), ('Developer', 'Member')]:
|
34 |
for parent, child in [('Admin', 'Developer'), ('Developer', 'Member')]:
|
35 |
parent_role = M.ProjectRole.by_name(parent, project=project)
|
35 |
parent_role = M.ProjectRole.by_name(parent, project=project)
|
|
... |
|
... |
51 |
log.info('Skipping "%s": missing Admin, Developer, or Member roles' % project_name)
|
51 |
log.info('Skipping "%s": missing Admin, Developer, or Member roles' % project_name)
|
52 |
continue
|
52 |
continue
|
53 |
for user in project.users():
|
53 |
for user in project.users():
|
54 |
pr = user.project_role(project=project)
|
54 |
pr = user.project_role(project=project)
|
55 |
if not pr.roles: continue
|
55 |
if not pr.roles: continue
|
56 |
for parent, children in [('Admin', ('Developer', 'Member')),
|
56 |
for parent, children in [('Admin', ('Developer', 'Member')),
|
57 |
('Developer', ('Member',))]:
|
57 |
('Developer', ('Member',))]:
|
58 |
if project_roles[parent]._id not in pr.roles: continue
|
58 |
if project_roles[parent]._id not in pr.roles: continue
|
59 |
for role_name in children:
|
59 |
for role_name in children:
|
60 |
extra_role = project_roles[role_name]
|
60 |
extra_role = project_roles[role_name]
|
61 |
if extra_role._id in pr.roles:
|
61 |
if extra_role._id in pr.roles:
|
|
... |
|
... |
71 |
num_projects_examined += 1
|
71 |
num_projects_examined += 1
|
72 |
session(project).clear()
|
72 |
session(project).clear()
|
73 |
|
73 |
|
74 |
log.info('%s projects examined.' % num_projects_examined)
|
74 |
log.info('%s projects examined.' % num_projects_examined)
|
75 |
|
75 |
|
76 |
|
|
|
77 |
PAGESIZE=1024
|
|
|
78 |
|
|
|
79 |
def chunked_project_iterator(q_project):
|
|
|
80 |
'''shamelessly copied from refresh-all-repos.py'''
|
|
|
81 |
page = 0
|
|
|
82 |
while True:
|
|
|
83 |
results = (M.Project.query
|
|
|
84 |
.find(q_project)
|
|
|
85 |
.skip(PAGESIZE*page)
|
|
|
86 |
.limit(PAGESIZE)
|
|
|
87 |
.all())
|
|
|
88 |
if not results: break
|
|
|
89 |
yield results
|
|
|
90 |
page += 1
|
|
|
91 |
|
|
|
92 |
if __name__ == '__main__':
|
76 |
if __name__ == '__main__':
|
93 |
main()
|
77 |
main()
|