summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorDaniel P. Berrangé <berrange@redhat.com>2019-08-30 13:22:54 +0100
committerDaniel P. Berrangé <berrange@redhat.com>2019-12-20 14:23:40 +0000
commit6a64be6c780e2c5f49c5e6353fbff3ac6dccb267 (patch)
tree46a1fa357eed0d0099227c20a968530f3d051107 /scripts
parent52a2c5b06b6157d86ca6621d99b280cc0d57d857 (diff)
downloadlibvirt-6a64be6c780e2c5f49c5e6353fbff3ac6dccb267.tar.gz
docs: rewrite polkit docs generator in Python
As part of a goal to eliminate Perl from libvirt build tools, rewrite the genaclperms.pl tool in Python. This was a straight conversion, manually going line-by-line to change the syntax from Perl to Python. Thus the overall structure of the file and approach is the same. Tested-by: Cole Robinson <crobinso@redhat.com> Reviewed-by: Cole Robinson <crobinso@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/genaclperms.py121
1 files changed, 121 insertions, 0 deletions
diff --git a/scripts/genaclperms.py b/scripts/genaclperms.py
new file mode 100755
index 0000000000..e228b3ef60
--- /dev/null
+++ b/scripts/genaclperms.py
@@ -0,0 +1,121 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2013-2019 Red Hat, Inc.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library. If not, see
+# <http://www.gnu.org/licenses/>.
+#
+
+import re
+import sys
+
+objects = [
+ "CONNECT", "DOMAIN", "INTERFACE",
+ "NETWORK_PORT", "NETWORK", "NODE_DEVICE",
+ "NWFILTER_BINDING", "NWFILTER",
+ "SECRET", "STORAGE_POOL", "STORAGE_VOL",
+]
+
+
+classes = {}
+
+for obj in objects:
+ klass = obj.lower()
+
+ klass = re.sub(r'''(^\w|_\w)''', lambda a: a.group(1).upper(), klass)
+ klass = klass.replace("_", "")
+ klass = klass.replace("Nwfilter", "NWFilter")
+ klass = "vir" + klass + "Ptr"
+
+ classes[obj] = klass
+
+
+objectstr = "|".join(objects)
+
+opts = {}
+in_opts = {}
+
+perms = {}
+
+aclfile = sys.argv[1]
+with open(aclfile, "r") as fh:
+ for line in fh:
+ if in_opts:
+ if line.find("*/") != -1:
+ in_opts = False
+ else:
+ m = re.search(r'''\*\s*\@(\w+):\s*(.*?)\s*$''', line)
+ if m is not None:
+ opts[m.group(1)] = m.group(2)
+ elif line.find("**") != -1:
+ in_opts = True
+ else:
+ m = re.search(r'''VIR_ACCESS_PERM_(%s)_((?:\w|_)+),''' %
+ objectstr, line)
+ if m is not None:
+ obj = m.group(1)
+ perm = m.group(2).lower()
+ if perm == "last":
+ continue
+
+ perm = perm.replace("_", "-")
+
+ if obj not in perms:
+ perms[obj] = {}
+ perms[obj][perm] = {
+ "desc": opts.get("desc", None),
+ "message": opts.get("message", None),
+ "anonymous": opts.get("anonymous", None),
+ }
+ opts = {}
+
+print('<?xml version="1.0" encoding="UTF-8"?>')
+print('<!DOCTYPE html>')
+print('<html xmlns="http://www.w3.org/1999/xhtml">')
+print(' <body>')
+
+for obj in sorted(perms.keys()):
+ klass = classes[obj]
+
+ olink = "object_" + obj.lower()
+
+ print(' <h3><a id="%s">%s</a></h3>' % (olink, klass))
+ print(' <table>')
+ print(' <thead>')
+ print(' <tr>')
+ print(' <th>Permission</th>')
+ print(' <th>Description</th>')
+ print(' </tr>')
+ print(' </thead>')
+ print(' <tbody>')
+
+ for perm in sorted(perms[obj].keys()):
+ description = perms[obj][perm]["desc"]
+
+ if description is None:
+ raise Exception("missing description for %s.%s" % (obj, perm))
+
+ plink = "perm_" + obj.lower() + "_" + perm.lower()
+ plink = plink.replace("-", "_")
+
+ print(' <tr>')
+ print(' <td><a id="%s">%s</a></td>' % (plink, perm))
+ print(' <td>%s</td>' % description)
+ print(' </tr>')
+
+ print(' </tbody>')
+ print(' </table>')
+
+print(' </body>')
+print('</html>')