Revision d99d1e36
b/.gitignore | ||
---|---|---|
109 | 109 |
/.hpc/ |
110 | 110 |
|
111 | 111 |
/htools/Ganeti/HTools/Version.hs |
112 |
/htools/Ganeti/Constants.hs |
b/Makefile.am | ||
---|---|---|
21 | 21 |
CHECK_NEWS = $(top_srcdir)/autotools/check-news |
22 | 22 |
DOCPP = $(top_srcdir)/autotools/docpp |
23 | 23 |
REPLACE_VARS_SED = autotools/replace_vars.sed |
24 |
CONVERT_CONSTANTS = $(top_srcdir)/autotools/convert-constants |
|
24 | 25 |
|
25 | 26 |
# Note: these are automake-specific variables, and must be named after |
26 | 27 |
# the directory + 'dir' suffix |
... | ... | |
337 | 338 |
htools/Ganeti/Luxi.hs \ |
338 | 339 |
htools/Ganeti/OpCodes.hs |
339 | 340 |
|
340 |
HS_BUILT_SRCS = htools/Ganeti/HTools/Version.hs |
|
341 |
HS_BUILT_SRCS = htools/Ganeti/HTools/Version.hs htools/Ganeti/Constants.hs
|
|
341 | 342 |
HS_BUILT_SRCS_IN = $(patsubst %,%.in,$(HS_BUILT_SRCS)) |
342 | 343 |
|
343 | 344 |
$(RUN_IN_TEMPDIR): | $(all_dirfiles) |
... | ... | |
508 | 509 |
autotools/check-news \ |
509 | 510 |
autotools/check-tar \ |
510 | 511 |
autotools/check-version \ |
512 |
autotools/convert-constants \ |
|
511 | 513 |
autotools/docpp \ |
512 | 514 |
autotools/gen-coverage \ |
513 | 515 |
autotools/testrunner \ |
... | ... | |
814 | 816 |
VCSVER=`cat $(abs_top_srcdir)/vcs-version`; \ |
815 | 817 |
sed -e "s/%ver%/$$VCSVER/" < $< > $@ |
816 | 818 |
|
819 |
htools/Ganeti/Constants.hs: htools/Ganeti/Constants.hs.in \ |
|
820 |
lib/constants.py lib/_autoconf.py $(CONVERT_CONSTANTS) |
|
821 |
set -e; { cat $< ; $(CONVERT_CONSTANTS); } > $@ |
|
822 |
|
|
817 | 823 |
lib/_autoconf.py: Makefile vcs-version | lib/.dir |
818 | 824 |
set -e; \ |
819 | 825 |
VCSVER=`cat $(abs_top_srcdir)/vcs-version`; \ |
b/autotools/convert-constants | ||
---|---|---|
1 |
#!/usr/bin/python |
|
2 |
# |
|
3 |
|
|
4 |
# Copyright (C) 2011 Google Inc. |
|
5 |
# |
|
6 |
# This program is free software; you can redistribute it and/or modify |
|
7 |
# it under the terms of the GNU General Public License as published by |
|
8 |
# the Free Software Foundation; either version 2 of the License, or |
|
9 |
# (at your option) any later version. |
|
10 |
# |
|
11 |
# This program is distributed in the hope that it will be useful, but |
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
# General Public License for more details. |
|
15 |
# |
|
16 |
# You should have received a copy of the GNU General Public License |
|
17 |
# along with this program; if not, write to the Free Software |
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
19 |
# 02110-1301, USA. |
|
20 |
|
|
21 |
"""Script for converting Python constants to Haskell code fragments. |
|
22 |
|
|
23 |
""" |
|
24 |
|
|
25 |
import re |
|
26 |
|
|
27 |
from ganeti import constants |
|
28 |
|
|
29 |
CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_]+$") |
|
30 |
|
|
31 |
|
|
32 |
def NameRules(name): |
|
33 |
"""Converts the upper-cased Python name to Haskell camelCase. |
|
34 |
|
|
35 |
""" |
|
36 |
elems = name.split("_") |
|
37 |
return elems[0].lower() + "".join(e.capitalize() for e in elems[1:]) |
|
38 |
|
|
39 |
|
|
40 |
def StringValueRules(value): |
|
41 |
"""Converts a string value from Python to Haskell. |
|
42 |
|
|
43 |
""" |
|
44 |
value = value.encode("string_escape") # escapes backslashes |
|
45 |
value = value.replace("\"", "\\\"") |
|
46 |
return value |
|
47 |
|
|
48 |
|
|
49 |
def Convert(): |
|
50 |
"""Converts the constants to Haskell. |
|
51 |
|
|
52 |
""" |
|
53 |
lines = [""] |
|
54 |
|
|
55 |
all_names = dir(constants) |
|
56 |
|
|
57 |
for name in all_names: |
|
58 |
value = getattr(constants, name) |
|
59 |
hs_name = NameRules(name) |
|
60 |
if not CONSTANT_RE.match(name): |
|
61 |
lines.append("-- Skipped %s, not constant" % name) |
|
62 |
elif isinstance(value, basestring): |
|
63 |
lines.append("%s :: String" % hs_name) |
|
64 |
lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value))) |
|
65 |
elif isinstance(value, int): |
|
66 |
lines.append("%s :: Int" % hs_name) |
|
67 |
lines.append("%s = %d" % (hs_name, value)) |
|
68 |
elif isinstance(value, long): |
|
69 |
lines.append("%s :: Integer" % hs_name) |
|
70 |
lines.append("%s = %d" % (hs_name, value)) |
|
71 |
elif isinstance(value, float): |
|
72 |
lines.append("%s :: Double" % hs_name) |
|
73 |
lines.append("%s = %f" % (hs_name, value)) |
|
74 |
else: |
|
75 |
lines.append("-- Skipped %s, %s not handled" % (name, type(value))) |
|
76 |
lines.append("") |
|
77 |
|
|
78 |
return "\n".join(lines) |
|
79 |
|
|
80 |
|
|
81 |
def main(): |
|
82 |
print Convert() |
|
83 |
|
|
84 |
|
|
85 |
if __name__ == "__main__": |
|
86 |
main() |
b/htools/Ganeti/Constants.hs.in | ||
---|---|---|
1 |
{-| Ganeti constants. |
|
2 |
|
|
3 |
These are duplicated from the Python code. |
|
4 |
|
|
5 |
-} |
|
6 |
|
|
7 |
{- |
|
8 |
|
|
9 |
Copyright (C) 2009, 2010, 2011 Google Inc. |
|
10 |
|
|
11 |
This program is free software; you can redistribute it and/or modify |
|
12 |
it under the terms of the GNU General Public License as published by |
|
13 |
the Free Software Foundation; either version 2 of the License, or |
|
14 |
(at your option) any later version. |
|
15 |
|
|
16 |
This program is distributed in the hope that it will be useful, but |
|
17 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
General Public License for more details. |
|
20 |
|
|
21 |
You should have received a copy of the GNU General Public License |
|
22 |
along with this program; if not, write to the Free Software |
|
23 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
24 |
02110-1301, USA. |
|
25 |
|
|
26 |
-} |
|
27 |
|
|
28 |
module Ganeti.Constants where |
Also available in: Unified diff