summaryrefslogtreecommitdiff
path: root/bin/SConsExamples.py
blob: 77f404182bc81caa738e955c41801e5b9e6bb25b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python
#
# Module for handling SCons examples processing.
#

__doc__ = """
"""

import os
import re
import SConsDoc
from SConsDoc import tf as stf

#
# The available types for ExampleFile entries
#
FT_FILE = 0     # a physical file (=<file>)
FT_FILEREF = 1  # a reference     (=<scons_example_file>)

class ExampleFile:
    def __init__(self, type_=FT_FILE):
        self.type = type_
        self.name = ''
        self.content = ''
        self.chmod = ''
        
    def isFileRef(self):
        return self.type == FT_FILEREF

class ExampleFolder:
    def __init__(self):
        self.name = ''
        self.chmod = ''

class ExampleCommand:
    def __init__(self):
        self.edit = ''
        self.environment = ''
        self.output = ''
        self.cmd = ''
        self.suffix = ''

class ExampleOutput:
    def __init__(self):
        self.name = ''
        self.tools = ''
        self.os = ''
        self.commands = []
        
class ExampleInfo:
    def __init__(self):
        self.name = ''
        self.files = []
        self.folders = []
        self.outputs = []
        
    def getFileContents(self, fname):
        for f in self.files:
            if fname == f.name and not f.isFileRef():
                return f.content
            
        return ''

def readExampleInfos(fpath, examples):
    """ Add the example infos for the file fpath to the
        global dictionary examples.
    """

    # Create doctree    
    t = SConsDoc.SConsDocTree()
    t.parseXmlFile(fpath)
    
    # Parse scons_examples
    for e in stf.findAll(t.root, "scons_example", SConsDoc.dbxid, 
                         t.xpath_context, t.nsmap):
        n = ''
        if stf.hasAttribute(e, 'name'):
            n = stf.getAttribute(e, 'name')
        if n and n not in examples:
            i = ExampleInfo()
            i.name = n
            examples[n] = i
            
        # Parse file and directory entries
        for f in stf.findAll(e, "file", SConsDoc.dbxid, 
                             t.xpath_context, t.nsmap):
            fi = ExampleFile()
            if stf.hasAttribute(f, 'name'):
                fi.name = stf.getAttribute(f, 'name')
            if stf.hasAttribute(f, 'chmod'):
                fi.chmod = stf.getAttribute(f, 'chmod')
            fi.content = stf.getText(f)
            examples[n].files.append(fi)
        for d in stf.findAll(e, "directory", SConsDoc.dbxid, 
                             t.xpath_context, t.nsmap):
            di = ExampleFolder()
            if stf.hasAttribute(d, 'name'):
                di.name = stf.getAttribute(d, 'name')
            if stf.hasAttribute(d, 'chmod'):
                di.chmod = stf.getAttribute(d, 'chmod')
            examples[n].folders.append(di)


    # Parse scons_example_files
    for f in stf.findAll(t.root, "scons_example_file", SConsDoc.dbxid, 
                         t.xpath_context, t.nsmap):
        if stf.hasAttribute(f, 'example'):
            e = stf.getAttribute(f, 'example')
        else:
            continue
        fi = ExampleFile(FT_FILEREF)
        if stf.hasAttribute(f, 'name'):
            fi.name = stf.getAttribute(f, 'name')
        if stf.hasAttribute(f, 'chmod'):
            fi.chmod = stf.getAttribute(f, 'chmod')
        fi.content = stf.getText(f)
        examples[e].files.append(fi)
        
    
    # Parse scons_output
    for o in stf.findAll(t.root, "scons_output", SConsDoc.dbxid, 
                         t.xpath_context, t.nsmap):
        if stf.hasAttribute(o, 'example'):
            n = stf.getAttribute(o, 'example')
        else:
            continue

        eout = ExampleOutput()
        if stf.hasAttribute(o, 'name'):
            eout.name = stf.getAttribute(o, 'name')
        if stf.hasAttribute(o, 'tools'):
            eout.tools = stf.getAttribute(o, 'tools')
        if stf.hasAttribute(o, 'os'):
            eout.os = stf.getAttribute(o, 'os')

        for c in stf.findAll(o, "scons_output_command", SConsDoc.dbxid, 
                         t.xpath_context, t.nsmap):
            if stf.hasAttribute(c, 'suffix'):
                s = stf.getAttribute(c, 'suffix')
            else:
                continue

            oc = ExampleCommand()
            oc.suffix = s
            if stf.hasAttribute(c, 'edit'):
                oc.edit = stf.getAttribute(c, 'edit')
            if stf.hasAttribute(c, 'environment'):
                oc.environment = stf.getAttribute(c, 'environment')
            if stf.hasAttribute(c, 'output'):
                oc.output = stf.getAttribute(c, 'output')
            if stf.hasAttribute(c, 'cmd'):
                oc.cmd = stf.getAttribute(c, 'cmd')

            eout.commands.append(oc)

        examples[n].outputs.append(eout)

def readAllExampleInfos(dpath):
    """ Scan for XML files in the given directory and 
        collect together all relevant infos (files/folders,
        output commands) in a map, which gets returned.
    """
    examples = {}
    for path, dirs, files in os.walk(dpath):
        for f in files:
            if f.endswith('.xml'):
                fpath = os.path.join(path, f)
                if SConsDoc.isSConsXml(fpath):
                    readExampleInfos(fpath, examples)
                   
    return examples

generated_examples = os.path.join('doc','generated','examples')

def ensureExampleOutputsExist(dpath):
    """ Scan for XML files in the given directory and 
        ensure that for every example output we have a
        corresponding output file in the 'generated/examples'
        folder.
    """
    # Ensure that the output folder exists
    if not os.path.isdir(generated_examples):
        os.mkdir(generated_examples)
        
    examples = readAllExampleInfos(dpath)
    for key, value in examples.iteritems():
        # Process all scons_output tags
        for o in value.outputs:
            for c in o.commands:
                cpath = os.path.join(generated_examples, 
                                     key+'_'+c.suffix+'.out')
                if not os.path.isfile(cpath):
                    content = c.output
                    if not content:
                        content = "NO OUTPUT YET! Run the script to generate/update all examples."

                    f = open(cpath, 'w')
                    f.write("%s\n" % content)
                    f.close()
        # Process all scons_example_file tags
        for r in value.files:
            if r.isFileRef():
                # Get file's content
                content = value.getFileContents(r.name)
                fpath = os.path.join(generated_examples, 
                                     key+'_'+r.name.replace("/","_"))
                # Write file
                f = open(fpath, 'w')
                f.write("%s\n" % content)
                f.close()

def collectSConsExampleNames(fpath):
    """ Return a set() of example names, used in the given file fpath.
    """
    names = set()
    suffixes = {}
    failed_suffixes = False

    # Create doctree    
    t = SConsDoc.SConsDocTree()
    t.parseXmlFile(fpath)
    
    # Parse it
    for e in stf.findAll(t.root, "scons_example", SConsDoc.dbxid, 
                         t.xpath_context, t.nsmap):
        n = ''
        if stf.hasAttribute(e, 'name'):
            n = stf.getAttribute(e, 'name')
        if n:
            names.add(n)
            if n not in suffixes:
                suffixes[n] = []
        else:
            print "Error: Example in file '%s' is missing a name!" % fpath
            failed_suffixes = True
    
    for o in stf.findAll(t.root, "scons_output", SConsDoc.dbxid, 
                         t.xpath_context, t.nsmap):
        n = ''
        if stf.hasAttribute(o, 'example'):
            n = stf.getAttribute(o, 'example')
        else:
            print "Error: scons_output in file '%s' is missing an example name!" % fpath
            failed_suffixes = True
            
        if n not in suffixes:
            print "Error: scons_output in file '%s' is referencing non-existent example '%s'!" % (fpath, n)
            failed_suffixes = True
            continue
            
        for c in stf.findAll(o, "scons_output_command", SConsDoc.dbxid, 
                         t.xpath_context, t.nsmap):
            s = ''
            if stf.hasAttribute(c, 'suffix'):
                s = stf.getAttribute(c, 'suffix')
            else:
                print "Error: scons_output_command in file '%s' (example '%s') is missing a suffix!" % (fpath, n)
                failed_suffixes = True
            
            if s not in suffixes[n]:
                suffixes[n].append(s)
            else:
                print "Error: scons_output_command in file '%s' (example '%s') is using a duplicate suffix '%s'!" % (fpath, n, s)
                failed_suffixes = True
    
    return names, failed_suffixes

def exampleNamesAreUnique(dpath):
    """ Scan for XML files in the given directory and 
        check whether the scons_example names are unique.
    """
    unique = True
    allnames = set()
    for path, dirs, files in os.walk(dpath):
        for f in files:
            if f.endswith('.xml'):
                fpath = os.path.join(path, f)
                if SConsDoc.isSConsXml(fpath):
                    names, failed_suffixes = collectSConsExampleNames(fpath)
                    if failed_suffixes:
                        unique = False
                    i = allnames.intersection(names)
                    if i:
                        print "Not unique in %s are: %s" % (fpath, ', '.join(i))
                        unique = False
                    
                    allnames |= names
                   
    return unique

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: