learn_CHOICE_enums.py 823 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python2
import os
import sys
enums = []
for line in open(sys.argv[1] + '.h', 'r'):
    if '_PRESENT' in line and not line.startswith('#define'):
        enums.append(line.strip().replace(",", ""))
enums_dump = "\n    ".join(
    'printf("%s = %%d\\n", %s);' % (e,e)
    for e in enums
)
uniq = os.getpid()
13
extractor_filename ="/tmp/enums_%d" % uniq
14
15
16
17
18
f = open(extractor_filename + ".c", 'w')
f.write("""
#include <stdio.h>
#include "%(base)s.h"

19
void main()
20
{
21
%(enums_dump)s
22
23
24
25
26
27
28
29
30
}""" % {"enums_dump":enums_dump, "base":sys.argv[1]})
f.close()
if 0 != os.system(
        "gcc -o %s -I. %s.c" % (extractor_filename, extractor_filename)):
    print "Failed to extract CHOICE enum values..."
    sys.exit(1)
os.system(extractor_filename + ">> DV.py")
os.unlink(extractor_filename + ".c")
os.unlink(extractor_filename)