BIND 10 trac1924, updated. 1aa0a1179c5948cc63bee18a2a19106d6ed1243d [1924] (minor) Try to make the comment slightly cleaner

BIND 10 source code commits bind10-changes at lists.isc.org
Mon Feb 11 09:04:04 UTC 2013


The branch, trac1924 has been updated
       via  1aa0a1179c5948cc63bee18a2a19106d6ed1243d (commit)
       via  beebff23a438a65506ea2af860686ed0272da31f (commit)
      from  9d5e1f515860689e1d7723cd113c6a5fa2a8866a (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 1aa0a1179c5948cc63bee18a2a19106d6ed1243d
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Mon Feb 11 10:03:26 2013 +0100

    [1924] (minor) Try to make the comment slightly cleaner

commit beebff23a438a65506ea2af860686ed0272da31f
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Mon Feb 11 09:56:42 2013 +0100

    [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.

-----------------------------------------------------------------------

Summary of changes:
 src/bin/msgq/tests/msgq_test.py                    |    9 +++++----
 src/lib/cc/Makefile.am                             |    6 +++++-
 src/lib/{util/common_defs.cc => cc/proto_defs.cc}  |    8 ++++----
 src/lib/cc/session.cc                              |   16 ++++++++--------
 src/lib/cc/session.h                               |    8 +++-----
 src/lib/python/isc/cc/Makefile.am                  |    8 ++++++++
 src/lib/python/isc/cc/session.py                   |    2 +-
 src/lib/python/isc/util/Makefile.am                |    8 +-------
 src/lib/util/Makefile.am                           |    8 +-------
 src/lib/util/python/Makefile.am                    |    3 ++-
 src/lib/util/{ => python}/const2hdr.py             |    0
 .../util => util/python}/pythonize_constants.py    |    0
 12 files changed, 38 insertions(+), 38 deletions(-)
 rename src/lib/{util/common_defs.cc => cc/proto_defs.cc} (89%)
 rename src/lib/util/{ => python}/const2hdr.py (100%)
 rename src/lib/{python/isc/util => util/python}/pythonize_constants.py (100%)

-----------------------------------------------------------------------
diff --git a/src/bin/msgq/tests/msgq_test.py b/src/bin/msgq/tests/msgq_test.py
index 4f8d18a..1a1bbdd 100644
--- a/src/bin/msgq/tests/msgq_test.py
+++ b/src/bin/msgq/tests/msgq_test.py
@@ -170,10 +170,11 @@ class MsgQTest(unittest.TestCase):
         The test is not exhaustive as it doesn't test all combination
         of existence of the recipient, addressing schemes, want_answer
         header and the reply header. It is not needed, these should
-        be mostly independant (eg. if the recipient is missing, it
-        shouldn't matter by which means it was discovered the recipient
-        is missing to how we hendle the value in reply header). If
-        we included everything, the test would have too many scenarios.
+        be mostly independant. That means, for example, if the message
+        is a reply and there's no recipient to send it to, the error
+        would not be generated no matter if we addressed the recipient
+        by lname or group. If we included everything, the test would
+        have too many scenarios with little benefit.
         """
         self.__sent_messages = []
         def fake_send_prepared_msg(socket, msg):
diff --git a/src/lib/cc/Makefile.am b/src/lib/cc/Makefile.am
index ec478de..b3475d8 100644
--- a/src/lib/cc/Makefile.am
+++ b/src/lib/cc/Makefile.am
@@ -25,6 +25,7 @@ libb10_cc_la_SOURCES = data.cc data.h session.cc session.h
 libb10_cc_la_SOURCES += logger.cc logger.h
 nodist_libb10_cc_la_SOURCES = cc_messages.cc cc_messages.h
 libb10_cc_la_LIBADD = $(top_builddir)/src/lib/log/libb10-log.la
+nodist_libb10_cc_la_SOURCES += proto_defs.cc proto_defs.h
 
 CLEANFILES = *.gcno *.gcda session_config.h cc_messages.cc cc_messages.h
 
@@ -34,6 +35,9 @@ session_config.h: session_config.h.pre
 cc_messages.cc cc_messages.h: cc_messages.mes
 	$(top_builddir)/src/lib/log/compiler/message $(top_srcdir)/src/lib/cc/cc_messages.mes
 
-BUILT_SOURCES = session_config.h cc_messages.cc cc_messages.h
+BUILT_SOURCES = session_config.h cc_messages.cc cc_messages.h proto_defs.h
+
+proto_defs.h: $(top_srcdir)/src/lib/util/python/const2hdr.py proto_defs.cc
+	$(PYTHON) $(top_srcdir)/src/lib/util/python/const2hdr.py $(srcdir)/proto_defs.cc $@
 
 EXTRA_DIST = cc_messages.mes
diff --git a/src/lib/cc/proto_defs.cc b/src/lib/cc/proto_defs.cc
new file mode 100644
index 0000000..24d9650
--- /dev/null
+++ b/src/lib/cc/proto_defs.cc
@@ -0,0 +1,44 @@
+// Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or 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 ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC 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.
+
+#include <cc/proto_defs.h>
+
+namespace isc {
+namespace cc {
+
+// Aside from defining the values for the C++ library, this file is also
+// used as direct input of the generator of the python counterpart. Please,
+// keep the syntax here simple and check the generated file
+// (lib/python/isc/cc/proto_defs.py) is correct and sane.
+
+// The constants used in the CC protocol
+// First the header names
+const char* const CC_HEADER_TYPE = "type";
+const char* const CC_HEADER_FROM = "from";
+const char* const CC_HEADER_TO = "to";
+const char* const CC_HEADER_GROUP = "group";
+const char* const CC_HEADER_INSTANCE = "instance";
+const char* const CC_HEADER_SEQ = "seq";
+const char* const CC_HEADER_WANT_ANSWER = "want_answer";
+const char* const CC_HEADER_REPLY = "reply";
+// The commands in the "type" header
+const char* const CC_COMMAND_SEND = "send";
+// The wildcards of some headers
+const char* const CC_TO_WILDCARD = "*";
+const char* const CC_INSTANCE_WILDCARD = "*";
+// Reply codes
+const int CC_REPLY_NO_RECPT = -1;
+
+}
+}
diff --git a/src/lib/cc/session.cc b/src/lib/cc/session.cc
index b300aad..90f0d60 100644
--- a/src/lib/cc/session.cc
+++ b/src/lib/cc/session.cc
@@ -480,14 +480,14 @@ Session::group_sendmsg(ConstElementPtr msg, std::string group,
     ElementPtr env = Element::createMap();
     const long int nseq = ++impl_->sequence_;
 
-    env->set(isc::util::CC_HEADER_TYPE,
-             Element::create(isc::util::CC_COMMAND_SEND));
-    env->set(isc::util::CC_HEADER_FROM, Element::create(impl_->lname_));
-    env->set(isc::util::CC_HEADER_TO, Element::create(to));
-    env->set(isc::util::CC_HEADER_GROUP, Element::create(group));
-    env->set(isc::util::CC_HEADER_INSTANCE, Element::create(instance));
-    env->set(isc::util::CC_HEADER_SEQ, Element::create(nseq));
-    env->set(isc::util::CC_HEADER_WANT_ANSWER, Element::create(want_answer));
+    env->set(CC_HEADER_TYPE,
+             Element::create(CC_COMMAND_SEND));
+    env->set(CC_HEADER_FROM, Element::create(impl_->lname_));
+    env->set(CC_HEADER_TO, Element::create(to));
+    env->set(CC_HEADER_GROUP, Element::create(group));
+    env->set(CC_HEADER_INSTANCE, Element::create(instance));
+    env->set(CC_HEADER_SEQ, Element::create(nseq));
+    env->set(CC_HEADER_WANT_ANSWER, Element::create(want_answer));
 
     sendmsg(env, msg);
     return (nseq);
diff --git a/src/lib/cc/session.h b/src/lib/cc/session.h
index b5c29f9..63bb41c 100644
--- a/src/lib/cc/session.h
+++ b/src/lib/cc/session.h
@@ -17,8 +17,7 @@
 
 #include <cc/data.h>
 #include <cc/session_config.h>
-
-#include <util/common_defs.h>
+#include <cc/proto_defs.h>
 
 #include <exceptions/exceptions.h>
 
@@ -84,9 +83,8 @@ namespace isc {
             virtual int group_sendmsg(isc::data::ConstElementPtr msg,
                                       std::string group,
                                       std::string instance =
-                                          isc::util::CC_INSTANCE_WILDCARD,
-                                      std::string to =
-                                          isc::util::CC_TO_WILDCARD,
+                                          CC_INSTANCE_WILDCARD,
+                                      std::string to = CC_TO_WILDCARD,
                                       bool want_answer = false) = 0;
             virtual bool group_recvmsg(isc::data::ConstElementPtr& envelope,
                                        isc::data::ConstElementPtr& msg,
diff --git a/src/lib/python/isc/cc/Makefile.am b/src/lib/python/isc/cc/Makefile.am
index b0ba3b2..aa3d565 100644
--- a/src/lib/python/isc/cc/Makefile.am
+++ b/src/lib/python/isc/cc/Makefile.am
@@ -1,6 +1,14 @@
 SUBDIRS = . tests
 
 python_PYTHON =	__init__.py data.py session.py message.py
+nodist_python_PYTHON = proto_defs.py
+BUILT_SOURCES = proto_defs.py
+
+proto_defs.py: $(top_srcdir)/src/lib/cc/proto_defs.cc \
+	$(top_srcdir)/src/lib/util/python/pythonize_constants.py
+	$(PYTHON) $(top_srcdir)/src/lib/util/python/pythonize_constants.py \
+		$(top_srcdir)/src/lib/cc/proto_defs.cc $@
+
 
 pythondir = $(pyexecdir)/isc/cc
 
diff --git a/src/lib/python/isc/cc/session.py b/src/lib/python/isc/cc/session.py
index c13979a..89a5066 100644
--- a/src/lib/python/isc/cc/session.py
+++ b/src/lib/python/isc/cc/session.py
@@ -22,7 +22,7 @@ import threading
 import bind10_config
 
 import isc.cc.message
-from isc.util.common_defs import *
+from isc.cc.proto_defs import *
 
 class ProtocolError(Exception): pass
 class NetworkError(Exception): pass
diff --git a/src/lib/python/isc/util/Makefile.am b/src/lib/python/isc/util/Makefile.am
index e8f737a..3eaaa12 100644
--- a/src/lib/python/isc/util/Makefile.am
+++ b/src/lib/python/isc/util/Makefile.am
@@ -1,12 +1,6 @@
 SUBDIRS = . cio tests
 
-python_PYTHON = __init__.py process.py socketserver_mixin.py file.py \
-		common_defs.py
-BUILT_SOURCES = common_defs.py
-EXTRA_DIST = pythonize_constants.py
-
-common_defs.py: $(top_srcdir)/src/lib/util/common_defs.cc pythonize_constants.py
-	$(PYTHON) $(srcdir)/pythonize_constants.py $(top_srcdir)/src/lib/util/common_defs.cc $@
+python_PYTHON = __init__.py process.py socketserver_mixin.py file.py
 
 pythondir = $(pyexecdir)/isc/util
 
diff --git a/src/lib/python/isc/util/pythonize_constants.py b/src/lib/python/isc/util/pythonize_constants.py
deleted file mode 100644
index cc6d9b2..0000000
--- a/src/lib/python/isc/util/pythonize_constants.py
+++ /dev/null
@@ -1,57 +0,0 @@
-# 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)
diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am
index 40c3afc..3960a8b 100644
--- a/src/lib/util/Makefile.am
+++ b/src/lib/util/Makefile.am
@@ -19,7 +19,6 @@ libb10_util_la_SOURCES += interprocess_sync_null.h interprocess_sync_null.cc
 libb10_util_la_SOURCES += memory_segment.h
 libb10_util_la_SOURCES += memory_segment_local.h memory_segment_local.cc
 libb10_util_la_SOURCES += range_utilities.h
-libb10_util_la_SOURCES += common_defs.h common_defs.cc
 libb10_util_la_SOURCES += hash/sha1.h hash/sha1.cc
 libb10_util_la_SOURCES += encode/base16_from_binary.h
 libb10_util_la_SOURCES += encode/base32hex.h encode/base64.h
@@ -30,12 +29,7 @@ libb10_util_la_SOURCES += encode/binary_from_base16.h
 libb10_util_la_SOURCES += random/qid_gen.h random/qid_gen.cc
 libb10_util_la_SOURCES += random/random_number_generator.h
 
-EXTRA_DIST = python/pycppwrapper_util.h const2hdr.py
-BUILT_SOURCES = common_defs.h
-
-common_defs.h: const2hdr.py common_defs.cc
-	$(PYTHON) $(srcdir)/const2hdr.py $(srcdir)/common_defs.cc $@
-
+EXTRA_DIST = python/pycppwrapper_util.h
 libb10_util_la_LIBADD = $(top_builddir)/src/lib/exceptions/libb10-exceptions.la
 CLEANFILES = *.gcno *.gcda
 
diff --git a/src/lib/util/common_defs.cc b/src/lib/util/common_defs.cc
deleted file mode 100644
index 5e4d185..0000000
--- a/src/lib/util/common_defs.cc
+++ /dev/null
@@ -1,44 +0,0 @@
-// Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or 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 ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS.  IN NO EVENT SHALL ISC 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.
-
-#include <util/common_defs.h>
-
-namespace isc {
-namespace util {
-
-// Aside from defining the values for the C++ util library, this file is also
-// used as direct input of the generator of the python counterpart. Please,
-// keep the syntax here simple and check the generated file
-// (lib/python/isc/util/common_defs.py) is correct and sane.
-
-// The constants used in the CC protocol
-// First the header names
-const char* const CC_HEADER_TYPE = "type";
-const char* const CC_HEADER_FROM = "from";
-const char* const CC_HEADER_TO = "to";
-const char* const CC_HEADER_GROUP = "group";
-const char* const CC_HEADER_INSTANCE = "instance";
-const char* const CC_HEADER_SEQ = "seq";
-const char* const CC_HEADER_WANT_ANSWER = "want_answer";
-const char* const CC_HEADER_REPLY = "reply";
-// The commands in the "type" header
-const char* const CC_COMMAND_SEND = "send";
-// The wildcards of some headers
-const char* const CC_TO_WILDCARD = "*";
-const char* const CC_INSTANCE_WILDCARD = "*";
-// Reply codes
-const int CC_REPLY_NO_RECPT = -1;
-
-}
-}
diff --git a/src/lib/util/const2hdr.py b/src/lib/util/const2hdr.py
deleted file mode 100644
index b8c56ea..0000000
--- a/src/lib/util/const2hdr.py
+++ /dev/null
@@ -1,65 +0,0 @@
-# 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/Makefile.am b/src/lib/util/python/Makefile.am
index 81d528c..c92af7e 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 0000000..b8c56ea
--- /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 0000000..cc6d9b2
--- /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)



More information about the bind10-changes mailing list