Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

#!/usr/bin/python 

# 

# Copyright (C) Citrix Systems Inc. 

# 

# This program 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 only. 

# 

# This program 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 program; if not, write to the Free Software Foundation, Inc., 

# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 

# 

# Metadata VDI format 

# 

 

from xml.dom import minidom, Node 

import struct 

import sys, string 

import util 

 

HDR_STRING = "XSSM" 

STRUCT_FMT = "%dsIHH" % len(HDR_STRING) 

STRUCT_SIZE = struct.calcsize(STRUCT_FMT) 

MD_MAJOR = 1 

MD_MINOR = 2 

XML_TAG = "SRMetadata" 

 

def buildHeader(len): 

    output = struct.pack(STRUCT_FMT, \ 

                         HDR_STRING, \ 

                         len, \ 

                         MD_MAJOR, \ 

                         MD_MINOR) 

    return output 

 

def unpackHeader(input): 

    return struct.unpack(STRUCT_FMT,input) 

 

def _testHdr(hdr): 

    assert(hdr[0] == HDR_STRING) 

    assert(hdr[2] <= MD_MAJOR) 

    assert(hdr[3] <= MD_MINOR) 

 

def unpackBody(input, len): 

    return struct.unpack("%ds" % len,input) 

 

def _generateXMLloop(Dict, e, dom): 

    for key in Dict.keys(): 

        entry = dom.createElement(key) 

        e.appendChild(entry) 

        if not isinstance(Dict[key], dict): 

            textnode = dom.createTextNode(str(Dict[key])) 

            entry.appendChild(textnode) 

        else: 

            _generateXMLloop(Dict[key], entry, dom) 

 

def _generateXML(Dict): 

    dom = minidom.Document() 

    md = dom.createElement(XML_TAG) 

    dom.appendChild(md) 

 

    _generateXMLloop(Dict, md, dom) 

    return dom.toprettyxml() 

 

def _walkXML(parent): 

    Dict = {} 

 

    if not parent.hasChildNodes(): 

        if parent.nodeValue == None: 

            return '' 

 

    for node in parent.childNodes: 

        if node.nodeType == Node.ELEMENT_NODE: 

            # Print the element name 

            Dict[util.to_plain_string(node.nodeName)] = "" 

 

            # Walk over any text nodes in the current node 

            content = [] 

            for child in node.childNodes: 

                if child.nodeType == Node.TEXT_NODE and \ 

                       child.nodeValue.strip(): 

                    content.append(child.nodeValue.strip()) 

            if content: 

                strContent = string.join(content) 

                Dict[util.to_plain_string(node.nodeName)] = util.to_plain_string(strContent) 

            else: 

                # Walk the child nodes 

                Dict[util.to_plain_string(node.nodeName)] = _walkXML(node) 

 

    return Dict 

 

def _parseXML(str): 

    dom = minidom.parseString(str) 

    objectlist = dom.getElementsByTagName(XML_TAG)[0] 

    Dict = _walkXML(objectlist) 

    return Dict 

 

def buildOutput(Dict): 

    xmlstr = _generateXML(Dict) 

    XML = struct.pack("%ds" % len(xmlstr),xmlstr) 

    HDR = buildHeader(len(XML) + STRUCT_SIZE) 

    return HDR + XML 

 

def requiresUpgrade(path): 

    f = open(path, "rb") 

    s = f.read(STRUCT_SIZE) 

    assert(len(s) == STRUCT_SIZE) 

    hdr = unpackHeader(s) 

    mdmajor = hdr[2] 

    mdminor = hdr[3] 

 

    if mdmajor < MD_MAJOR: 

        return True 

 

    if mdmajor == MD_MAJOR and mdminor < MD_MINOR: 

        return True 

 

    return False 

 

def retrieveXMLfromFile(path): 

    try: 

        f = open(path, "rb") 

        s = f.read(STRUCT_SIZE) 

        assert(len(s) == STRUCT_SIZE) 

        hdr = unpackHeader(s) 

        _testHdr(hdr) 

        xmllen = hdr[1] - STRUCT_SIZE 

        s = f.read(xmllen) 

        assert(len(s) == xmllen) 

        XML = unpackBody(s, xmllen) 

        return XML[0] 

 

    finally: 

        f.close() 

 

def writeXMLtoFile(path, dict): 

    f = open(path, "wb") 

    f.write(buildOutput(dict)) 

    f.close() 

 

def main(): 

    path = sys.argv[1] 

    xml = retrieveXMLfromFile(path) 

    print xml 

    print _parseXML(xml) 

 

153if __name__ == '__main__': 

    main()