<commit_before>#!/usr/bin/python
#
# Copyright 2015 <NAME>, w/ insignificant contributions by <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""See docstring for PuppetlabsProductsURLProvider class"""

from __future__ import absolute_import
import urllib2
import re
from distutils.version import LooseVersion

from autopkglib import Processor, ProcessorError

__all__ = ["PuppetAgentProductsURLProvider"]

DL_INDEX = "https://downloads.puppetlabs.com/mac"
DEFAULT_VERSION = "latest"
DEFAULT_PRODUCT_VERSION = "5"
OS_VERSION = "10.12"

class PuppetAgentProductsURLProvider(Processor):
    """Extracts a URL for a Puppet Labs item."""
    description = __doc__
    input_variables = {
        "product_version": {
            "required": False,
            "description":
                "Major version of the AIO installer. Either 5 or 6 at present. Defaults to %s".format(DEFAULT_PRODUCT_VERSION),
        },
        "get_version": {
            "required": False,
            "description":
                ("Specific version to request. Defaults to '%s', which "
                 "automatically finds the highest available release version."
                 % (DEFAULT_VERSION)),
        },
        "get_os_version": {
            "required": False,
            "description":
                ("When fetching the puppet-agent, collection-style pkg, "
                 "designates OS. Defaults to '%s'. Currently only 10.9 "
                 "or 10.10 packages are available."
                 % (OS_VERSION)),
        },
    }
    output_variables = {
        "version": {
            "description": "Version of the product.",
        },
        "url": {
            "description": "Download URL.",
        },
    }

    def main(self):
        """Return a download URL for a PuppetLabs item"""
        download_url = DL_INDEX
        os_version = self.env.get("get_os_version", OS_VERSION)
        product_version = self.env.get("product_version", DEFAULT_PRODUCT_VERSION)
        version_re = r"\d+\.\d+\.\d+" # e.g.: 10.10/PC1/x86_64/puppet-agent-1.2.5-1.osx10.10.dmg
        download_url += str("/puppet" + product_version + "/" + os_version + "/x86_64")
        re_download = ("href=\"(puppet-agent-(%s)-1.osx(%s).dmg)\"" % (version_re, os_version))

        try:
            data = urllib2.urlopen(download_url).read()
        except BaseException as err:
            raise ProcessorError(
                "Unexpected error retrieving download index: '%s'" % err)

        # (dmg, version)<commit_msg>Make basic urlopen() calls Python 3 friendly<commit_after>#!/usr/bin/python
#
# Copyright 2015 <NAME>, w/ insignificant contributions by <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""See docstring for PuppetlabsProductsURLProvider class"""

from __future__ import absolute_import
import re
from distutils.version import LooseVersion

from autopkglib import Processor, ProcessorError

try:
    from urllib.request import urlopen  # For Python 3
except ImportError:
    from urllib2 import urlopen  # For Python 2

__all__ = ["PuppetAgentProductsURLProvider"]

DL_INDEX = "https://downloads.puppetlabs.com/mac"
DEFAULT_VERSION = "latest"
DEFAULT_PRODUCT_VERSION = "5"
OS_VERSION = "10.12"

class PuppetAgentProductsURLProvider(Processor):
    """Extracts a URL for a Puppet Labs item."""
    description = __doc__
    input_variables = {
        "product_version": {
            "required": False,
            "description":
                "Major version of the AIO installer. Either 5 or 6 at present. Defaults to %s".format(DEFAULT_PRODUCT_VERSION),
        },
        "get_version": {
            "required": False,
            "description":
                ("Specific version to request. Defaults to '%s', which "
                 "automatically finds the highest available release version."
                 % (DEFAULT_VERSION)),
        },
        "get_os_version": {
            "required": False,
            "description":
                ("When fetching the puppet-agent, collection-style pkg, "
                 "designates OS. Defaults to '%s'. Currently only 10.9 "
                 "or 10.10 packages are available."
                 % (OS_VERSION)),
        },
    }
    output_variables = {
        "version": {
            "description": "Version of the product.",
        },
        "url": {
            "description": "Download URL.",
        },
    }

    def main(self):
        """Return a download URL for a PuppetLabs item"""
        download_url = DL_INDEX
        os_version = self.env.get("get_os_version", OS_VERSION)
        product_version = self.env.get("product_version", DEFAULT_PRODUCT_VERSION)
        version_re = r"\d+\.\d+\.\d+" # e.g.: 10.10/PC1/x86_64/puppet-agent-1.2.5-1.osx10.10.dmg
        download_url += str("/puppet" + product_version + "/" + os_version + "/x86_64")
        re_download = ("href=\"(puppet-agent-(%s)-1.osx(%s).dmg)\"" % (version_re, os_version))

        try:
            data = urlopen(download_url).read()
        except BaseException as err:
            raise ProcessorError(
                "Unexpected error retrieving download index: '%s'" % err)

        # (dmg, version)
{'fraction_non_alphanumeric': 0.10994000324306795, 'fraction_numerical': 0.015242419328684935, 'mean_word_length': 2.534670487106017, 'pattern_counts': {'":': 26, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5206796', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1718, 'n_words': 530}