<commit_before>#!/usr/bin/python
# Copyright 2017 Northern.tech AS
#
#    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
#
#        https://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.

import time
import json
import os
import logging
import requests
import pytest

from . import logger
from . import api_version
from .requests_helpers import requests_retry
from ..common_docker import get_mender_gateway

class Deployments:
    # track the last statistic for a deployment id
    last_statistic = {}

    def __init__(self, auth, auth_v2):
        self.reset()
        self.auth = auth
        self.auth_v2 = auth_v2

    def reset(self):
        # Reset all temporary values.
        self.last_statistic = Deployments.last_statistic

    def get_deployments_base_path(self):
        return "https://%s/api/management/%s/deployments/" % (get_mender_gateway(), api_version)

    def upload_image(self, filename, description="abc"):
        image_path_url = self.get_deployments_base_path() + "artifacts"

        r = requests_retry().post(image_path_url,
                                  verify=False,
                                  headers=self.auth.get_auth_token(),
                                  files=(
                                      ("description", (None, description)),
                                      ("size", (None, str(os.path.getsize(filename)))),
                                      ("artifact", (filename, open(filename), "application/octet-stream"))
                                  ))

        logger.info("Received image upload status code: " + str(r.status_code) + " with payload: " + str(r.text))
        assert r.status_code == requests.status_codes.codes.created
        return r.headers["location"]

    def trigger_deployment(self, name, artifact_name, devices):
        deployments_path_url = self.get_deployments_base_path() + "deployments"

        trigger_data = {"name": name,
                        "artifact_name": artifact_name,
                        "devices": devices}

        headers = {'Content-Type': 'application/json'}
        headers.update(self.auth.get_auth_token())

        r = requests_retry().post(deployments_path_url, headers=headers,
                                  data=json.dumps(trigger_data), verify=False)

        logger.debug("triggering deployment with: " + json.dumps(trigger_data))
        logging.info("deployment returned: " + r.text)
        assert r.status_code == requests.status_codes.codes.created

        deployment_id = str(r.headers['Location'].split("/")[-1])<commit_msg>Open file for read in binary mode on requests.post<commit_after>#!/usr/bin/python
# Copyright 2017 Northern.tech AS
#
#    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
#
#        https://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.

import time
import json
import os
import logging
import requests
import pytest

from . import logger
from . import api_version
from .requests_helpers import requests_retry
from ..common_docker import get_mender_gateway

class Deployments:
    # track the last statistic for a deployment id
    last_statistic = {}

    def __init__(self, auth, auth_v2):
        self.reset()
        self.auth = auth
        self.auth_v2 = auth_v2

    def reset(self):
        # Reset all temporary values.
        self.last_statistic = Deployments.last_statistic

    def get_deployments_base_path(self):
        return "https://%s/api/management/%s/deployments/" % (get_mender_gateway(), api_version)

    def upload_image(self, filename, description="abc"):
        image_path_url = self.get_deployments_base_path() + "artifacts"

        r = requests_retry().post(image_path_url,
                                  verify=False,
                                  headers=self.auth.get_auth_token(),
                                  files=(
                                      ("description", (None, description)),
                                      ("size", (None, str(os.path.getsize(filename)))),
                                      ("artifact", (filename, open(filename, 'rb'), "application/octet-stream"))
                                  ))

        logger.info("Received image upload status code: " + str(r.status_code) + " with payload: " + str(r.text))
        assert r.status_code == requests.status_codes.codes.created
        return r.headers["location"]

    def trigger_deployment(self, name, artifact_name, devices):
        deployments_path_url = self.get_deployments_base_path() + "deployments"

        trigger_data = {"name": name,
                        "artifact_name": artifact_name,
                        "devices": devices}

        headers = {'Content-Type': 'application/json'}
        headers.update(self.auth.get_auth_token())

        r = requests_retry().post(deployments_path_url, headers=headers,
                                  data=json.dumps(trigger_data), verify=False)

        logger.debug("triggering deployment with: " + json.dumps(trigger_data))
        logging.info("deployment returned: " + r.text)
        assert r.status_code == requests.status_codes.codes.created

        deployment_id = str(r.headers['Location'].split("/")[-1])
{'fraction_non_alphanumeric': 0.09850599244787392, 'fraction_numerical': 0.003940239697914957, 'mean_word_length': 2.382565241532482, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '4488286', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1597, 'n_words': 438}