def zip_folder(folder, filename):
    try:
        filename = filename + '.zip'
        try:
            zip_file = zipfile.ZipFile(filename,
                                       'w',
                                       compression=zipfile.ZIP_DEFLATED,
                                       allowZip64=True)
        except RuntimeError:  # if ZIP_DEFLATED not support, fallback to default
            zip_file = zipfile.ZipFile(filename, 'w', allowZip64=True)
        LOGGER.info('Create zip file: {0}'.format(filename))
        add_folder(zip_file, folder)
        LOGGER.info("folder {0} has been properly zipped as {1}".format(
            folder, filename))
        zip_file.close()
        status = Global.SUCCESS
        out_file = os.path.abspath(filename)
    except IOError as error:
        LOGGER.error('Cannot create zip file: {0} - {1}'.format(
            filename, error))
        status = Global.FAILURE
        out_file = ""

    if status == Global.SUCCESS:
        LOGGER.info("ZIP FILE: {0}".format(out_file))
    return status, out_file
Esempio n. 2
0
    def create_url_shortcut(self, campaign_url):
        """
        Create a shortcut to open given url

        :rtype: tuple
        :return: Status and output log
        """
        try:
            if os.path.exists(Folders.REPORTS):
                output_path = os.path.join(Folders.REPORTS, self._report_name + ".html")

                if not os.path.isfile(output_path):
                    LOGGER_FWK.info("CREATE_URL_SHORTCUT: Creating url shortcut to campaign result")

                    html_content = "<html>\n"
                    html_content += "<head>\n"
                    html_content += "<meta http-equiv=\"refresh\" content=\"0; URL=%s\">\n" % campaign_url
                    html_content += "</head>\n"
                    html_content += "<body></body>\n"
                    html_content += "</html>"

                    with open(output_path, "w") as fd:
                        fd.write(html_content)

        except Exception as ex:  # pylint: disable=W0703
            error_msg = "CREATE_URL_SHORTCUT: Fail, " + str(ex)
            LOGGER_FWK.error(error_msg)
            return Global.FAILURE, error_msg

        msg = "CREATE_URL_SHORTCUT: Created link to %s" % str(campaign_url)
        return Global.SUCCESS, msg
Esempio n. 3
0
    def _parse_bench_node(self, node):
        """
        Parses XML `Phone` node(s) from Bench Catalog file and maps it into a python structure (dict)

        :return: A dict mapping XML configuration
        :rtype: AttributeDict

        :raise AcsConfigException.INVALID_BENCH_CONFIG: If a (or more) deprecated parameter(s)
            is/are found in a Phone node

        """
        LOGGER_FWK.info(
            'Loading optional device model parameters from CLI and/or BenchConfig '
            'for {0} ({1})...'.format(self._device_name,
                                      self._device_model_name))
        buf_params = AttributeDict()
        # Storing value to avoid recomputing each call
        device_model_name = self.device_model_name

        if device_model_name:
            buf_params["Name"] = device_model_name

        if self.device_conf_filename:
            buf_params["DeviceConfigPath"] = self.device_conf_filename

        # Get phone properties
        for attrs in node.xpath(".//Parameter"):
            name, value, description = attrs.get("name"), attrs.get(
                "value"), attrs.get("description")

            if name in self.PROHIBITIVE_KEYS:
                # Do not allow to override internal keys
                # as it would lead to nasty & unexpected behavior !!
                continue

            # Not supported anymore, raise AcsConfigException.INVALID_BENCH_CONFIG !!
            if name and value:
                buf_params[name] = value
                self._bench_conf_deprecated[name] = (value, description)
            else:
                buf_params.update(attrs.attrib)

        # Report Errors if so
        if self.bench_contains_errors:
            LOGGER_FWK.error(self._report_errors())
            _error(
                'Invalid Bench Parameters format found! {0}'.format(', '.join(
                    self._bench_conf_deprecated.keys())),
                AcsConfigException.INVALID_BENCH_CONFIG)

        # Extracting device modules if so
        buf_params.device_modules = self.extract_device_modules(node)

        return buf_params
Esempio n. 4
0
def check_keys(dictionary, keys):
    """
    Check if keys are in given dictionary, raise an error if not.

    :type dictionary: dict
    :param dictionary: dict to test

    :type keys: string
    :param keys: keys to check

    :rtype: list
    :return: list of missing keys
    """
    key_list = []
    for element in keys:
        if element not in dictionary:
            LOGGER_FWK.error("KEY %s missing on your dictionary" % element)
            key_list.append(element)

    return key_list