source: orange/orange/buildC45.py @ 8042:ffcb93bc9028

Revision 8042:ffcb93bc9028, 2.8 KB checked in by markotoplak, 3 years ago (diff)

Hierarchical clustering: also catch RuntimeError when importing matplotlib (or the documentation could not be built on server).

Line 
1import os, os.path, re, string, sys
2from sys import argv
3
4# Prepare .h files that wrap .i files and prevent multiple inclusion
5for incf in ["buildex", "defns", "extern", "types"]:
6    fle = open(incf+".h", "wt")
7    fle.write('#ifndef __%s_H\n#define __%s_H\n#include "%s.i"\n#endif' % (incf, incf, incf))
8    fle.close()
9
10# Iterate through all .c and .i files and replace include *.i with include *.h
11incre = re.compile('\s*#include\s+"([^.]+)\.i"')
12mathre = re.compile('\s*#include\s+<math\.h>\s*')
13stdiore = re.compile('\s*#include\s+<stdio.h>\s*')
14
15for filename in filter(lambda x: x[-2:] in [".c", ".i"], os.listdir(".")):
16    if filename == "ensemble.c":
17        continue
18   
19    inf = open(filename, "rt")
20    str = inf.read()
21    inf.close()
22
23    str = incre.sub('\n#include "\\1.h"', str)
24    str = mathre.sub("\n", str)
25    str = stdiore.sub("\n", str)
26   
27    inf = open(filename, "wt")
28    inf.write(str+"\n")
29    inf.close()
30
31# Compile ensemble.c and prepare dynamic library
32
33import orange
34orangedir, orange = os.path.split(orange.__file__)
35
36if sys.platform == "win32":
37    def findprog(name):
38        path = os.environ["PATH"]
39        for p in path.split(";"):
40            if os.path.exists(p+"\\"+name):
41                return p+"\\"+name
42
43    compiler = findprog("cl.exe")
44    if not compiler:
45        print "Compiler (cl.exe) could not be found on PATH.\nCorrect the PATH or compile ensemble.c into c45.dll yourself."
46        sys.exit(1)
47
48    linker = findprog("link.exe")
49    if not linker:
50        print "Linker (link.exe) could not be found on PATH.\nCorrect the PATH or compile ensemble.c into c45.dll yourself."
51        sys.exit(2)
52       
53    if len(argv)>1 and argv[1]=="-d":
54        print "DEBUG version"
55        addargs_compile = ['/MTd', '/D', '"_DEBUG"', '/Od', '/Zi']
56        addargs_link = ['/debug', '/out:c:\\temp\\orange\\debug\\c45_d.dll']
57    else:
58        addargs_compile = ['/MT', '/D', '"NDEBUG"']
59        addargs_link = ['/out:%s\\c45.dll ' % orangedir]
60   
61    ret = os.spawnv(os.P_WAIT, compiler, string.split('/nologo /W0 /Od /Zi /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /FD /c ensemble.c ') + addargs_compile)
62    if ret:
63        print "C compiler exited abnormally"
64
65    ret = os.spawnv(os.P_WAIT, linker, string.split('/nologo /dll /incremental:no /machine:I386 ensemble.obj') + addargs_link)
66    if ret:
67        print "linker exited abnormally"
68
69   
70
71elif sys.platform == "linux2":
72    ret = os.system('gcc ensemble.c -o %s/c45.so -shared -lstdc++' % orangedir)
73    if ret:
74        print "compiler/linker exited abnormally"
75
76elif sys.platform == "darwin":
77    ret = os.system('gcc -F. -bundle -O3 -arch ppc -arch i386 -arch x86_64 ensemble.c -o %s/c45.so' % orangedir)
78    if ret:
79        print "compiler/linker exiter abnormally"
80
81else:
82    print "C4.5 is not supported for this platform"
83    exit(3)
84
85import orange   
Note: See TracBrowser for help on using the repository browser.