summaryrefslogtreecommitdiff
path: root/scripts/content_tools/arche_tool.py
blob: f99d7be39ad7be65dfb47bce1223bcfee26e425f (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
#!runpy.sh

"""\

This module contains tools for comparing files output by LLVOAvatar::dumpArchetypeXML

$LicenseInfo:firstyear=2016&license=viewerlgpl$
Second Life Viewer Source Code
Copyright (C) 2016, Linden Research, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
version 2.1 of the License only.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
$/LicenseInfo$
"""

import argparse
from lxml import etree
from itertools import chain

def node_key(e):
    if e.tag == "param":
        return e.tag + " " + e.get("id")
    if e.tag == "texture":
        return e.tag + " " + e.get("te")
    if e.get("name"):
        return e.tag + " " + e.get("name")
    return None

def compare_matched_nodes(key,items,summary):
    tags = list(set([e.tag for e in items]))
    if len(tags) != 1:
        print "different tag types for key",key
        summary.setdefault("tag_mismatch",0)
        summary["tag_mismatch"] += 1
        return
    all_attrib = list(set(chain.from_iterable([e.attrib.keys() for e in items])))
    #print key,"all_attrib",all_attrib
    for attr in all_attrib:
        vals = [e.get(attr) for e in items]
        #print "key",key,"attr",attr,"vals",vals
        if len(set(vals)) != 1:
            print key,"- attr",attr,"multiple values",vals
            summary.setdefault("attr",{})
            summary["attr"].setdefault(attr,0)
            summary["attr"][attr] += 1

def compare_trees(file_trees):
    print "compare_trees"
    summary = {}
    all_keys = list(set([node_key(e) for tree in file_trees for e in tree.getroot().iter() if node_key(e)]))
    #print "keys",all_keys
    tree_nodes = []
    for i,tree in enumerate(file_trees):
        nodes = dict((node_key(e),e) for e in tree.getroot().iter() if node_key(e))
        tree_nodes.append(nodes)
    for key in sorted(all_keys):
        items = []
        for nodes in tree_nodes:
            if not key in nodes:
                print "file",i,"missing item for key",key
                summary.setdefault("missing",0)
                summary["missing"] += 1
            else:
                items.append(nodes[key])
        compare_matched_nodes(key,items,summary)
    print "Summary:"
    print summary
                
def dump_appearance_params(tree):
    vals = []
    for e in tree.getroot().iter():
        if e.tag == "param":
            g = int(e.get("group"))
            if g in [0,3]:
                vals.append("{" + e.get("id") + "," +e.get("u8") + "}")
                #print e.get("id"), e.get("name"), e.get("group"), e.get("u8")
    if len(vals)==253:
        print ", ".join(vals)
        
    
if __name__ == "__main__":

    parser = argparse.ArgumentParser(description="compare avatar XML archetype files")
    parser.add_argument("--verbose", help="verbose flag", action="store_true")
    parser.add_argument("--compare", help="compare flag", action="store_true")
    parser.add_argument("--appearance_params", help="compare flag", action="store_true")
    parser.add_argument("files", nargs="+", help="name of one or more archtype files")
    args = parser.parse_args()


    print "files",args.files
    file_trees = [etree.parse(filename) for filename in args.files]
    print args
    if args.compare:
        compare_trees(file_trees)
    if args.appearance_params:
        dump_appearance_params(file_trees[0])