Last active
October 15, 2015 20:30
-
-
Save triti/5974e3b03b108359c958 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<key>Processor</key> | |
<string>com.github.triti.SharedProcessors/XMLReader</string> | |
<key>Arguments</key> | |
<dict> | |
<key>xml_path</key> | |
<string>%destination_path%/com.eset.remoteadministrator.agent.pkg/PackageInfo</string> | |
<key>elements</key> | |
<array> | |
<dict> | |
<key>xpath</key> | |
<string>.</string> | |
<key>attributes</key> | |
<dict> | |
<key>identifier</key> | |
<string>bundleid</string> | |
</dict> | |
</dict> | |
<dict> | |
<key>xpath</key> | |
<string>./bundle</string> | |
<key>attributes</key> | |
<dict> | |
<key>CFBundleShortVersionString</key> | |
<string>version</string> | |
</dict> | |
</dict> | |
</array> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os.path | |
import xml.etree.cElementTree as ET | |
import FoundationPlist | |
from autopkglib import Processor, ProcessorError | |
__all__ = ['XMLReader'] | |
class XMLReader(Processor): | |
input_variables = { | |
"xml_path": { | |
"required": True, | |
"description": "Path to an XML file to be read.", | |
}, | |
"elements": { | |
"required": True, | |
"description": "Array of dictionaries that defines what elements to search for.", | |
}, | |
} | |
output_variables = { | |
"xml_reader_output_variables": { | |
"description": "Output variables per 'elements' supplied as input." | |
}, | |
} | |
def main(self): | |
elements = self.env['elements'] | |
path = os.path.normpath(self.env['xml_path']) | |
if not os.path.exists(path): | |
raise ProcessorError("Path '%s' does not exist!" % path) | |
self.output('Reading: %s' % path) | |
tree = ET.parse(path) | |
root = tree.getroot() | |
self.env["xml_reader_output_variables"] = {} | |
for query in elements: | |
if not 'xpath' in query: | |
raise ProcessorError("Query key 'xpath' is missing!") | |
xpath = query['xpath'] | |
try: | |
element = root.findall(xpath)[0] | |
except IndexError: | |
raise ProcessorError("XPath '%s' could not be found in XML file %s!" | |
% (xpath, path)) | |
if 'attributes' in query: | |
attributes = query['attributes'] | |
for key, val in attributes.items(): | |
self.env[val] = element.get(key) | |
if self.env[val] is None: | |
raise ProcessorError( | |
"Could not find attribute named '%s' at XPath '%s'!" | |
% (key, xpath)) | |
self.output("Assigning value of '%s' to output variable '%s'" | |
% (self.env[val], val)) | |
self.env["xml_reader_output_variables"][val] = (self.env[val]) | |
if 'text' in query: | |
var = query['text'] | |
self.env[var] = element.text | |
if self.env[var] is None: | |
raise ProcessorError("No text found at XPath '%s'!" | |
% xpath) | |
self.output("Assigning value of '%s' to output variable '%s'" | |
% (self.env[var], var)) | |
if __name__ == '__main__': | |
PROCESSOR = XMLReader() | |
PROCESSOR.execute_shell() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment