-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdumprules.py
More file actions
55 lines (48 loc) · 1.58 KB
/
dumprules.py
File metadata and controls
55 lines (48 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/python
#
# Dump the rules including the related groups and the public steps
# from the ICAT to a file or to stdout.
#
import logging
import icat
import icat.config
from icat.query import Query
from icat.dumpfile import open_dumpfile
try:
import icat.dumpfile_xml
except ImportError:
pass
try:
import icat.dumpfile_yaml
except ImportError:
pass
logging.basicConfig(level=logging.INFO)
formats = icat.dumpfile.Backends.keys()
if len(formats) == 0:
raise RuntimeError("No datafile backends available.")
config = icat.config.Config()
config.add_variable('file', ("-o", "--outputfile"),
dict(help="output file name or '-' for stdout"),
default='-')
config.add_variable('format', ("-f", "--format"),
dict(help="output file format", choices=formats),
default='YAML')
client, conf = config.getconfig()
client.login(conf.auth, conf.credentials)
groups = set()
query = Query(client, "Rule",
conditions=[("grouping", "IS NOT NULL")],
includes={"grouping.userGroups.user"})
for r in client.search(query):
groups.add(r.grouping)
items = [
sorted(groups, key=icat.entity.Entity.__sortkey__),
Query(client, "PublicStep"),
Query(client, "Rule", order=["what", "id"],
conditions=[("grouping", "IS NULL")]),
Query(client, "Rule", order=["grouping.name", "what", "id"],
conditions=[("grouping", "IS NOT NULL")],
includes={"grouping"}),
]
with open_dumpfile(client, conf.file, conf.format, 'w') as dumpfile:
dumpfile.writedata(items)