|
a/scripts/purge_project.py |
|
b/scripts/purge_project.py |
1 |
import sys
|
1 |
import sys
|
2 |
import logging
|
2 |
import logging
|
3 |
|
3 |
|
4 |
from pylons import g
|
4 |
from pylons import g, c
|
5 |
|
5 |
|
6 |
from ming.orm import session
|
6 |
from ming.orm import session, mapper, MappedClass
|
7 |
|
7 |
|
8 |
from allura import model as M
|
8 |
from allura import model as M
|
9 |
|
9 |
|
10 |
log = logging.getLogger(__name__)
|
10 |
log = logging.getLogger(__name__)
|
11 |
|
11 |
|
|
|
12 |
|
12 |
def main():
|
13 |
def main():
|
|
|
14 |
USAGE='Usage: %s <shortname> [test]' % sys.argv[0]
|
13 |
if len(sys.argv) != 2:
|
15 |
if len(sys.argv) not in (2,3):
|
14 |
log.error('Usage: %s <shortname>', sys.argv[0])
|
16 |
log.error(USAGE)
|
|
|
17 |
return 1
|
|
|
18 |
if len(sys.argv) == 3 and sys.argv[2] != 'test':
|
|
|
19 |
log.error(USAGE)
|
15 |
return 1
|
20 |
return 1
|
16 |
pname = sys.argv[1]
|
21 |
pname = sys.argv[1]
|
17 |
log.info('Purging %s', pname)
|
22 |
log.info('Purging %s', pname)
|
18 |
project = M.Project.query.get(shortname=pname)
|
23 |
project = M.Project.query.get(shortname=pname)
|
19 |
if project is None:
|
24 |
if project is None:
|
20 |
log.fatal('Project %s not found', pname)
|
25 |
log.fatal('Project %s not found', pname)
|
21 |
return 2
|
26 |
return 2
|
|
|
27 |
if sys.argv[2] == 'test':
|
|
|
28 |
log.info('Test mode, not purging project')
|
22 |
purge_project(project)
|
29 |
purge_project(project)
|
23 |
|
30 |
|
24 |
def purge_project(project):
|
31 |
def purge_project(project):
|
25 |
gid = project.tool_data.get('sfx', {}).get('group_id', project._id)
|
32 |
gid = project.tool_data.get('sfx', {}).get('group_id', project._id)
|
26 |
project.shortname = 'deleted-%s' % gid
|
33 |
project.shortname = 'deleted-%s' % gid
|
27 |
project.deleted = True
|
34 |
project.deleted = True
|
28 |
g.solr.delete(q='project_id_s:%s' % project._id)
|
35 |
g.solr.delete(q='project_id_s:%s' % project._id)
|
29 |
session(project).flush()
|
36 |
session(project).flush()
|
|
|
37 |
c.project = project
|
|
|
38 |
app_config_ids = [
|
|
|
39 |
ac._id for ac in M.AppConfig.query.find(dict(project_id=c.project._id)) ]
|
|
|
40 |
for name, cls in MappedClass._registry.iteritems():
|
|
|
41 |
if 'project_id' in mapper(cls).property_index:
|
|
|
42 |
# Purge the things directly related to the project
|
|
|
43 |
cls.query.remove(
|
|
|
44 |
dict(project_id=project._id),
|
|
|
45 |
multiple=True)
|
|
|
46 |
elif 'app_config_id' in mapper(cls).property_index:
|
|
|
47 |
# ... and the things related to its apps
|
|
|
48 |
cls.query.remove(
|
|
|
49 |
dict(app_config_id={'$in':app_config_ids}),
|
|
|
50 |
multiple=True)
|
|
|
51 |
else:
|
|
|
52 |
# Don't dump other things
|
|
|
53 |
continue
|
30 |
|
54 |
|
31 |
if __name__ == '__main__':
|
55 |
if __name__ == '__main__':
|
32 |
sys.exit(main())
|
56 |
sys.exit(main())
|