Commit d99d1e36 authored by Iustin Pop's avatar Iustin Pop

Implement conversion of Python constants to Haskell

With the merge of the repositories, we can now auto-generate the code
for Haskell constants from the Python code.

Currently this only handles the basic types (strings and
integers). Handling containers such as lists and dictionaries is only
possible if we would use a parser such that we recognise the element
names. We could extend the convert-constants script if that becomes
necessary, right now I'm looking at just the simple constants such as
Iallocator modes, instance states, etc.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 83a2da0f
......@@ -109,3 +109,4 @@
/.hpc/
/htools/Ganeti/HTools/Version.hs
/htools/Ganeti/Constants.hs
......@@ -21,6 +21,7 @@ CHECK_VERSION = $(top_srcdir)/autotools/check-version
CHECK_NEWS = $(top_srcdir)/autotools/check-news
DOCPP = $(top_srcdir)/autotools/docpp
REPLACE_VARS_SED = autotools/replace_vars.sed
CONVERT_CONSTANTS = $(top_srcdir)/autotools/convert-constants
# Note: these are automake-specific variables, and must be named after
# the directory + 'dir' suffix
......@@ -337,7 +338,7 @@ HS_LIB_SRCS = \
htools/Ganeti/Luxi.hs \
htools/Ganeti/OpCodes.hs
HS_BUILT_SRCS = htools/Ganeti/HTools/Version.hs
HS_BUILT_SRCS = htools/Ganeti/HTools/Version.hs htools/Ganeti/Constants.hs
HS_BUILT_SRCS_IN = $(patsubst %,%.in,$(HS_BUILT_SRCS))
$(RUN_IN_TEMPDIR): | $(all_dirfiles)
......@@ -508,6 +509,7 @@ EXTRA_DIST = \
autotools/check-news \
autotools/check-tar \
autotools/check-version \
autotools/convert-constants \
autotools/docpp \
autotools/gen-coverage \
autotools/testrunner \
......@@ -814,6 +816,10 @@ htools/Ganeti/HTools/Version.hs: htools/Ganeti/HTools/Version.hs.in vcs-version
VCSVER=`cat $(abs_top_srcdir)/vcs-version`; \
sed -e "s/%ver%/$$VCSVER/" < $< > $@
htools/Ganeti/Constants.hs: htools/Ganeti/Constants.hs.in \
lib/constants.py lib/_autoconf.py $(CONVERT_CONSTANTS)
set -e; { cat $< ; $(CONVERT_CONSTANTS); } > $@
lib/_autoconf.py: Makefile vcs-version | lib/.dir
set -e; \
VCSVER=`cat $(abs_top_srcdir)/vcs-version`; \
......
#!/usr/bin/python
#
# Copyright (C) 2011 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
"""Script for converting Python constants to Haskell code fragments.
"""
import re
from ganeti import constants
CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_]+$")
def NameRules(name):
"""Converts the upper-cased Python name to Haskell camelCase.
"""
elems = name.split("_")
return elems[0].lower() + "".join(e.capitalize() for e in elems[1:])
def StringValueRules(value):
"""Converts a string value from Python to Haskell.
"""
value = value.encode("string_escape") # escapes backslashes
value = value.replace("\"", "\\\"")
return value
def Convert():
"""Converts the constants to Haskell.
"""
lines = [""]
all_names = dir(constants)
for name in all_names:
value = getattr(constants, name)
hs_name = NameRules(name)
if not CONSTANT_RE.match(name):
lines.append("-- Skipped %s, not constant" % name)
elif isinstance(value, basestring):
lines.append("%s :: String" % hs_name)
lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value)))
elif isinstance(value, int):
lines.append("%s :: Int" % hs_name)
lines.append("%s = %d" % (hs_name, value))
elif isinstance(value, long):
lines.append("%s :: Integer" % hs_name)
lines.append("%s = %d" % (hs_name, value))
elif isinstance(value, float):
lines.append("%s :: Double" % hs_name)
lines.append("%s = %f" % (hs_name, value))
else:
lines.append("-- Skipped %s, %s not handled" % (name, type(value)))
lines.append("")
return "\n".join(lines)
def main():
print Convert()
if __name__ == "__main__":
main()
{-| Ganeti constants.
These are duplicated from the Python code.
-}
{-
Copyright (C) 2009, 2010, 2011 Google Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
-}
module Ganeti.Constants where
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment