~ubuntu-branches/ubuntu/quantal/xen-api/quantal

« back to all changes in this revision

Viewing changes to scripts/mtcerrno-to-ocaml.py

  • Committer: Package Import Robot
  • Author(s): Jon Ludlam
  • Date: 2011-07-07 21:50:18 UTC
  • Revision ID: package-import@ubuntu.com-20110707215018-3t9ekbh7qy5y2b1p
Tags: upstream-1.3
ImportĀ upstreamĀ versionĀ 1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Convert the MTC exit codes into a disjoint union type. Each line in the file looks like:
 
4
 
 
5
# errdef, MTC_EXIT_SUCCESS,                       0,  0,  "",
 
6
 
 
7
# Usage:
 
8
# cat ../xha.hg/include/mtcerrno.def | ./scripts/mtcerrno-to-ocaml.py > ocaml/xapi/xha_errno.ml
 
9
 
 
10
import sys
 
11
 
 
12
def parse(file):
 
13
    all = []
 
14
    while True:
 
15
        line = file.readline()
 
16
        if line == "":
 
17
            return all
 
18
        if line.startswith("errdef, MTC_EXIT"):
 
19
            bits = line.split(",")
 
20
            name = bits[1].strip()
 
21
            code = bits[2].strip()
 
22
            desc = bits[4].strip()
 
23
            this = { "name": name, "code": code, "desc": desc }
 
24
            all.append(this)
 
25
 
 
26
def ctor_name(x):
 
27
    ctor = x['name']
 
28
    return ctor[0].upper() + ctor[1:].lower()
 
29
 
 
30
def make_datatype(all):
 
31
    print "type code = "
 
32
    for x in all:
 
33
        print "| %s" % ctor_name(x)
 
34
 
 
35
def to_string(all):
 
36
    print "let to_string : code -> string = function"
 
37
    for x in all:
 
38
        print "| %s -> \"%s\"" % (ctor_name(x), x['name'])
 
39
        
 
40
def to_description_string(all):
 
41
    print "let to_description_string : code -> string = function"
 
42
    for x in all:
 
43
        print "| %s -> %s" % (ctor_name(x), x['desc'])
 
44
 
 
45
def of_int(all):
 
46
    print "let of_int : int -> code = function"
 
47
    for x in all:
 
48
        print "| %s -> %s" % (x['code'], ctor_name(x))
 
49
    print "| x -> failwith (Printf.sprintf \"Unrecognised MTC exit code: %d\" x)"
 
50
            
 
51
if __name__ == "__main__":
 
52
    all = parse(sys.stdin)
 
53
    print "(* Autogenerated by %s -- do not edit *)" % (sys.argv[0])
 
54
    make_datatype(all)
 
55
    to_string(all)
 
56
    to_description_string(all)
 
57
    of_int(all)
 
58
    
 
59
    
 
60
 
 
61
            
 
62