Skip to content

Instantly share code, notes, and snippets.

@pudquick
Last active December 22, 2015 22:29
Show Gist options
  • Select an option

  • Save pudquick/6540866 to your computer and use it in GitHub Desktop.

Select an option

Save pudquick/6540866 to your computer and use it in GitHub Desktop.
import re
import urllib2
from autopkglib import Processor, ProcessorError
__all__ = ["ExampleURLProvider"]
EXAMPLE_BASE_URL = "http://example.com/downloads"
re_dmg = re.compile(r'a[^>]* href="(?P<filename>[^"]+\.dmg)"')
class ExampleURLProvider(Processor):
description = "Provides URL to the latest Example download."
input_variables = {
"base_url": {
"required": False,
"description": "Default is '%s." % EXAMPLE_BASE_URL,
},
}
output_variables = {
"url": {
"description": "URL to the latest Example product download.",
},
}
__doc__ = description
def get_example_dmg_url(self, base_url):
# Construct download directory URL.
index_url = base_url
# Read HTML index.
try:
f = urllib2.urlopen(index_url)
html = f.read()
f.close()
except BaseException as e:
raise ProcessorError("Can't download %s: %s" % (index_url, e))
# Search for HTML links that link to files ending in .dmg
m = re_dmg.search(html)
if not m:
raise ProcessorError(
"Couldn't find download URL in %s"
% index_url)
# Return URL.
return "/".join((base_url, m.group("filename")))
def main(self):
# Determine base_url.
base_url = self.env.get("base_url", EXAMPLE_BASE_URL)
self.env["url"] = self.get_example_dmg_url(base_url)
self.output("Found URL %s" % self.env["url"])
if __name__ == "__main__":
processor = ExampleURLProvider()
processor.execute_shell()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment