summaryrefslogtreecommitdiffstats
path: root/src/lib/util/python
diff options
context:
space:
mode:
authorMichal 'vorner' Vaner <michal.vaner@nic.cz>2013-02-11 09:56:42 +0100
committerMichal 'vorner' Vaner <michal.vaner@nic.cz>2013-02-11 09:58:20 +0100
commitbeebff23a438a65506ea2af860686ed0272da31f (patch)
tree8e4db81c537a97fac9a9a76c536d242e5a4fe3b2 /src/lib/util/python
parent[1924] constify (diff)
downloadkea-beebff23a438a65506ea2af860686ed0272da31f.tar.xz
kea-beebff23a438a65506ea2af860686ed0272da31f.zip
[1924] Move the common_defs to the cc directory
Currently, it contains only the definitions related to CC. If we have other definitions for other purposes, we may create further definition files. Also, rename it to proto_defs, since all is protocol constants. Move the generator scripts to better place. There should be no difference in functionality.
Diffstat (limited to 'src/lib/util/python')
-rw-r--r--src/lib/util/python/Makefile.am3
-rw-r--r--src/lib/util/python/const2hdr.py65
-rw-r--r--src/lib/util/python/pythonize_constants.py57
3 files changed, 124 insertions, 1 deletions
diff --git a/src/lib/util/python/Makefile.am b/src/lib/util/python/Makefile.am
index 81d528c5c2..c92af7e2bd 100644
--- a/src/lib/util/python/Makefile.am
+++ b/src/lib/util/python/Makefile.am
@@ -1 +1,2 @@
-noinst_SCRIPTS = gen_wiredata.py mkpywrapper.py
+noinst_SCRIPTS = gen_wiredata.py mkpywrapper.py const2hdr.py \
+ pythonize_constants.py
diff --git a/src/lib/util/python/const2hdr.py b/src/lib/util/python/const2hdr.py
new file mode 100644
index 0000000000..b8c56ea47c
--- /dev/null
+++ b/src/lib/util/python/const2hdr.py
@@ -0,0 +1,65 @@
+# Copyright (C) 2013 Internet Systems Consortium.
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
+# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
+# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+The script takes a C++ file with constant definitions and creates a
+header file for the constants. It, however, does not understand C++
+syntax, it only does some heuristics to guess what looks like
+a constant and strips the values.
+
+The purpose is just to save some work with keeping both the source and
+header. The source syntax must be limited already, because it's used to
+generate the python module (by the
+lib/python/isc/util/pythonize_constants.py script).
+'''
+
+import sys
+import re
+
+if len(sys.argv) != 3:
+ sys.stderr.write("Usage: python3 ./const2hdr.py input.cc output.h\n")
+ sys.exit(1)
+
+[filename_in, filename_out] = sys.argv[1:3]
+
+preproc = re.compile('^#')
+constant = re.compile('^([a-zA-Z].*?[a-zA-Z_0-9]+)\\s*=.*;')
+
+with open(filename_in) as file_in, open(filename_out, "w") as file_out:
+ file_out.write("// This file is generated from " + filename_in + "\n" +
+ "// by the const2hdr.py script.\n" +
+ "// Do not edit, all changes will be lost.\n\n")
+ for line in file_in:
+ if preproc.match(line):
+ # There's only one preprocessor line in the .cc file. We abuse
+ # that to position the top part of the header.
+ file_out.write("#ifndef BIND10_COMMON_DEFS_H\n" +
+ "#define BIND10_COMMON_DEFS_H\n" +
+ "\n" +
+ "// \\file " + filename_out + "\n" +
+'''// \\brief Common shared constants\n
+// This file contains common definitions of constasts used across the sources.
+// It includes, but is not limited to the definitions of messages sent from
+// one process to another. Since the names should be self-explanatory and
+// the variables here are used mostly to synchronize the same values across
+// multiple programs, separate documentation for each variable is not provided.
+''')
+ continue
+ # Extract the constant. Remove the values and add "extern"
+ line = constant.sub('extern \\1;', line)
+
+ file_out.write(line)
+
+ file_out.write("#endif\n")
diff --git a/src/lib/util/python/pythonize_constants.py b/src/lib/util/python/pythonize_constants.py
new file mode 100644
index 0000000000..cc6d9b23ff
--- /dev/null
+++ b/src/lib/util/python/pythonize_constants.py
@@ -0,0 +1,57 @@
+# Copyright (C) 2013 Internet Systems Consortium.
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
+# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
+# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+This script takes a C++ file with constants and converts it to a python
+module. However, the syntax it parses is very limited (it doesn't understand
+C++ at all, it just looks for lines containing the equal sign and strips
+what it thinks might be type).
+
+The purpose is to keep the same values of constants in C++ and python. This
+saves the work of keeping the constants in sync manually and is less error
+prone.
+'''
+
+import sys
+import re
+
+if len(sys.argv) != 3:
+ sys.stderr.write("Usage: python3 ./pythonize_constants.py input.cc output.py\n")
+ sys.exit(1)
+
+[filename_in, filename_out] = sys.argv[1:3]
+
+# Ignore preprocessor, namespaces and the ends of namespaces.
+ignore = re.compile('^(#|namespace|})')
+comment = re.compile('^//(.*)')
+constant = re.compile('^[a-zA-Z].*?([a-zA-Z_0-9]+\\s*=.*);')
+
+with open(filename_in) as file_in, open(filename_out, "w") as file_out:
+ file_out.write("# This file is generated from " + filename_in + "\n" +
+ "# by the pythonize_constants.py script.\n" +
+ "# Do not edit, all changes will be lost.\n\n")
+ for line in file_in:
+ if ignore.match(line):
+ continue
+ # Mangle comments to be python-like
+ line = comment.sub('#\\1', line)
+ # Extract the constant.
+
+ # TODO: We may want to do something with the true vs. True and
+ # NULL vs. None and such. Left out for now, since none are in
+ # the input file currently.
+ line = constant.sub('\\1', line)
+
+ file_out.write(line)