Passed
Push — master ( 1713a6...bcb549 )
by Peter
02:05
created

pyxb_builder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 11
1
from SCons.Script import * 
2
3
def build_xmlschema_wrappers(target, source, env):
4
    '''
5
        SCons needs real file names, so the main script must use proper file
6
        name suffixes for the Python file.
7
8
        PyXBGen, however, wants no .py suffix, otherwise modules are generated.
9
        Therefore, we strip the suffix before calling PyXBGen.
10
11
        Wee also need to use a proper "--binding-root" parameter, otherwise
12
        the path names become part of the generated Python module names
13
    '''
14
15
    sources = [str(src) for src in source]
16
17
    # Targets are the Python modules to be generated
18
    # If they all share the same directory prefix, then strip it and
19
    # use it as binding root. This gives relative imports in the generated
20
    # Python code, which is nicer
21
    prefix = ""
22
    all_same_prefix = True
23
    for t in target:
24
        new_prefix = str(t).rsplit('/',1)[0]
25
        if new_prefix != prefix:
26
            if prefix == "":
27
                prefix = new_prefix
28
            else:
29
                all_same_prefix = False
30
        prefix = new_prefix
31
32
    if all_same_prefix:
33
        targets = [str(trg).rsplit('/',1)[1].rsplit('.')[0] for trg in target]
34
        modulespec = ['-u %s -m %s'%(src, trg) for src, trg in zip(sources,targets)]
35
        cmdline = 'pyxbgen --binding-root='+prefix+' '+' '.join(modulespec)
36
    else:
37
        targets = [str(trg).rsplit('.')[0] for trg in target]
38
        modulespec = ['-u %s -m %s'%(src, trg) for src, trg in zip(sources,targets)]
39
        cmdline = 'pyxbgen '+' '.join(modulespec)
40
41
    print cmdline
42
    if os.system(cmdline) != 0:
43
        raise Exception('Execution of pyxbgen failed.')
44
45
# Define Builder for Django config file from settings INI file
46
pyxbbuilder = Builder(action = build_xmlschema_wrappers)
47
48
49