summaryrefslogtreecommitdiffstats
path: root/wsd/lint-discovery.py
blob: ab0ec4f27df168c8b0d3b09f2859dfbf54429cb3 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, you can obtain one at http://mozilla.org/MPL/2.0/.
#
# Makes sure that discovery.xml in online.git is in sync with
# filter/source/config/fragments/ in core.git.

from __future__ import print_function
import os
import sys
import xml.sax


class DiscoveryHandler(xml.sax.handler.ContentHandler):
    """Parses an online.git discovery.xml."""

    def __init__(self):
        # Dict of app -> {extension -> action}
        self.appActions = {}
        self.app = None
        self.allExtensions = set()

    def startElement(self, name, attrs):
        if name == "app":
            for k, v in list(attrs.items()):
                if k == "name":
                    self.app = v
                    self.appActions[self.app] = {}
        elif name == "action" and self.app:
            action = None
            ext = None
            for k, v in list(attrs.items()):
                if k == "name":
                    action = v
                elif k == "ext":
                    ext = v
            if action and ext:
                self.appActions[self.app][ext] = action
                if ext in self.allExtensions:
                    # Potential problem:
                    # see 2de5017e329ce09efbd8f4dc6066fdba3e2c080c
                    # discovery.xml with duplicating "ext" is valid, but
                    # can't be used directly in e.g. SharePoint, unless
                    # specific extensions are imported using
                    # New-SPWOPIBinding's parameters, avoiding
                    # the duplication.
                    print("warning: extension '" + ext +
                          "' exists for '" + self.app + "', " +
                          "but already used earlier in discovery.xml")
                self.allExtensions.add(ext)

    def endElement(self, name):
        if name == "app" and self.app:
            self.app = None


class FilterTypeHandler(xml.sax.handler.ContentHandler):
    """Parses core.git filter/source/config/fragments/types/*.xcu."""

    def __init__(self):
        self.name = None
        self.inExtensions = False
        self.content = []
        self.extensions = None
        self.extensionsSep = " "

    def startElement(self, name, attrs):
        if name == "node":
            for k, v in list(attrs.items()):
                if k == "oor:name":
                    self.name = v
        elif name == "prop":
            for k, v in list(attrs.items()):
                if k == "oor:name" and v == "Extensions":
                    self.inExtensions = True
        elif name == "value" and self.inExtensions:
            for k, v in list(attrs.items()):
                if k == "oor:separator":
                    self.extensionsSep = v

    def endElement(self, name):
        if name == "prop" and self.inExtensions:
            self.inExtensions = False
            self.extensions = "".join(self.content).strip()\
                                .encode("utf-8").split(self.extensionsSep)
            self.extensionsSep = " "
            self.content = []

    def characters(self, content):
        if self.inExtensions:
            self.content.append(content)


class FilterFragmentHandler(xml.sax.handler.ContentHandler):
    """Parses core.git filter/source/config/fragments/filters/*.xcu."""

    def __init__(self):
        self.inType = False
        self.typeName = None
        self.inFlags = False
        self.flags = None
        self.inDocumentService = False
        self.documentService = None
        self.content = []

    def startElement(self, name, attrs):
        if name == "prop":
            for k, v in list(attrs.items()):
                if k == "oor:name" and v == "Type":
                    self.inType = True
                elif k == "oor:name" and v == "Flags":
                    self.inFlags = True
                elif k == "oor:name" and v == "DocumentService":
                    self.inDocumentService = True

    def endElement(self, name):
        if name == "prop" and self.inType:
            self.inType = False
            self.typeName = "".join(self.content).strip()
            self.content = []
        elif name == "prop" and self.inFlags:
            self.inFlags = False
            encodedFlags = "".join(self.content).strip().encode("utf-8")
            self.flags = encodedFlags.split(" ")
            self.content = []
        elif name == "prop" and self.inDocumentService:
            self.inDocumentService = False
            self.documentService = "".join(self.content).strip()
            self.content = []

    def characters(self, content):
        if self.inType or self.inFlags or self.inDocumentService:
            self.content.append(content)


# Builds a 'document service' -> {'extension' -> 'filter flags'} dictionary.
def getExtensionProperties(filterDir):
    # Build a 'type name' -> 'extensions' dictionary.
    typeNameExtensions = {}
    typeFragments = os.path.join(filterDir, "types")
    for typeFragment in os.listdir(typeFragments):
        if not typeFragment.endswith(".xcu"):
            continue
        parser = xml.sax.make_parser()
        filterTypeHandler = FilterTypeHandler()
        parser.setContentHandler(filterTypeHandler)
        parser.parse(os.path.join(typeFragments, typeFragment))
        # Did we find some extensions?
        if filterTypeHandler.extensions:
            typeNameExtensions[filterTypeHandler.name] = \
                filterTypeHandler.extensions
    # Build a 'type name' -> ('filter flag list', 'document service')
    # dictionary.
    typeNameFlags = {}
    filterFragments = os.path.join(filterDir, "filters")
    for filterFragment in os.listdir(filterFragments):
        if not filterFragment.endswith(".xcu"):
            continue
        parser = xml.sax.make_parser()
        handler = FilterFragmentHandler()
        parser.setContentHandler(handler)
        parser.parse(os.path.join(filterFragments, filterFragment))
        if "IMPORT" in handler.flags:
            if handler.typeName in typeNameFlags:
                if "EXPORT" in typeNameFlags[handler.typeName][0]:
                    # don't modify a filetype with maximal capabilities
                    continue
            typeNameFlags[handler.typeName] = \
                (handler.flags, handler.documentService)
    # Now build the combined
    # 'document service' -> {'extension' -> 'filter flags'}.
    extensionProperties = {}
    for typeName in typeNameExtensions:
        if typeName not in typeNameFlags:
            continue
        flags, documentService = typeNameFlags[typeName]
        if documentService not in extensionProperties:
            extensionProperties[documentService] = {}
        for extension in typeNameExtensions[typeName]:
            extensionProperties[documentService][extension] = flags
    return extensionProperties


# Map app names to document service names
appDocumentServices = {
    'writer': 'com.sun.star.text.TextDocument',
    'writer-global': 'com.sun.star.text.GlobalDocument',
    'writer-web': 'com.sun.star.text.WebDocument',
    'calc': 'com.sun.star.sheet.SpreadsheetDocument',
    'impress': 'com.sun.star.presentation.PresentationDocument',
    'draw': 'com.sun.star.drawing.DrawingDocument',
}
documentServicesApp = {v: k for k, v in appDocumentServices.items()}

# We know about these extensions
extensionsSkipList = {
    'xls',  # we know that it can be edited
    'pdf',  # it exists for draw - its entry in core.git is missing document
            # service
    'zip',
    'htm',
    'html',
    'xhtml',
    '*',   # well, obvious ;-)
    '',    # and this :-D
}


def main():
    discoveryXml = "discovery.xml"
    repoGuess = os.path.join(os.environ["HOME"], "git/libreoffice/master")
    filterDir = os.path.join(repoGuess, "filter/source/config/fragments")
    if len(sys.argv) >= 3:
        discoveryXml = sys.argv[1]
        filterDir = sys.argv[2]

    # Parse discovery.xml, which describes what online.git exposes at the
    # moment.
    parser = xml.sax.make_parser()
    discoveryHandler = DiscoveryHandler()
    parser.setContentHandler(discoveryHandler)
    parser.parse(discoveryXml)

    # Parse core.git filter definitions to build a
    # 'document service' -> {'extension' -> 'filter flags'} dictionary.
    extensionProperties = getExtensionProperties(filterDir)

    proposed = {}

    # Now look up the filter flags in core.git for the extension.
    for app, actions in discoveryHandler.appActions.items():
        if app not in appDocumentServices:
            continue  # e.g., for "Capabilities"
        documentService = appDocumentServices[app]
        if documentService not in extensionProperties:
            # Inconsistency found.
            print("warning: actions for '" + app + "' " +
                  "exist, but not found in core.git")
            continue
        for extension, discoveryAction in actions.items():
            if extension in extensionsSkipList:
                continue
            if extension not in extensionProperties[documentService]:
                # Inconsistency found.
                print("warning: action for '" + app + ":" + extension + "' " +
                      "exists, but is not found in core.git")
                continue
            flags = extensionProperties[documentService][extension]
            if "IMPORT" in flags and "EXPORT" in flags:
                coreAction = "edit"
            else:
                coreAction = "view"

            if discoveryAction != coreAction:
                # Inconsistency found.
                print("warning: action for '" + app + ":" + extension + "' " +
                      "is '" + discoveryAction + "', " +
                      "but it should be '" + coreAction + "'")

        # Now see if there are any new extensions in the core.git filter config
        # which are missing.
        for extension, flags in extensionProperties[documentService].items():
            if extension not in actions:
                if "IMPORT" in flags and "EXPORT" in flags:
                    action = "edit"
                else:
                    action = "view"
                if app not in proposed:
                    proposed[app] = {}
                proposed[app][extension] = action

    # Now see if there are any new types in the core.git filter config which
    # are missing.
    for documentService, extensions in extensionProperties.items():
        missingName = None
        if documentService not in documentServicesApp:
            # Inconsistency found.
            print("warning: extensions for '" + documentService + "' " +
                  "found in core.git, without mapping to apps "
                  "in discovery.xml")
            missingName = documentService
        else:
            app = documentServicesApp[documentService]
            if app not in discoveryHandler.appActions:
                # Inconsistency found.
                print("warning: extensions for '" + app + "' " +
                      "found in core.git, all missing in discovery.xml")
                missingName = app

        if missingName:
            for extension, flags in extensions.items():
                if "IMPORT" in flags and "EXPORT" in flags:
                    action = "edit"
                else:
                    action = "view"
                if missingName not in proposed:
                    proposed[missingName] = {}
                proposed[missingName][extension] = action

    # Produce a copy&paste-able XML output for the proposed changes.
    for app, extensions in proposed.items():
        newExtensions = {}
        for extension, action in extensions.items():
            if extension in extensionsSkipList:
                continue
            if extension in discoveryHandler.allExtensions:
                continue  # see 2de5017e329ce09efbd8f4dc6066fdba3e2c080c
            newExtensions[extension] = action
        if not newExtensions:
            continue  # no extensions after filtering

        print('        <app name="' + app + '">')
        for extension, action in newExtensions.items():
            print('            <action name="' + action + '" default="true" ' +
                  'ext="' + extension + '"/>')
        print('        </app>')


if __name__ == "__main__":
    main()

# vim:set shiftwidth=4 softtabstop=4 expandtab: