Ejemplo n.º 1
0
def set_value(key, value, persist_across_reboots=False):
    """Set the value for a key. If |persist_across_restarts| is set, then the key
  won't be deleted even run.py is restarted. """
    value_path = get_value_file_path(key)

    try:
        value_str = json_utils.dumps(value)
    except Exception:
        logs.log_error('Non-serializable value stored to cache key %s: "%s"' %
                       (key, value))
        return

    try:
        with open(value_path, 'wb') as f:
            f.write(value_str)
    except IOError:
        logs.log_error('Failed to write %s to persistent cache.' % key)

    if not persist_across_reboots:
        return

    persist_value_path = value_path + PERSIST_FILE_EXTENSION
    if os.path.exists(persist_value_path):
        return

    try:
        open(persist_value_path, 'wb').close()
    except IOError:
        logs.log_error(
            'Failed to write presistent metadata file for cache key %s' % key)
Ejemplo n.º 2
0
def _http_request(url,
                  body=None,
                  method=_POST_METHOD,
                  force_reauthorization=False):
    """Make a POST request to the specified URL."""
    authorization = _get_authorization(force_reauthorization)
    headers = {
        'User-Agent': 'clusterfuzz-reproduce',
        'Authorization': authorization
    }

    http = httplib2.Http()
    request_body = json_utils.dumps(body) if body else ''
    response, content = http.request(url,
                                     method=method,
                                     headers=headers,
                                     body=request_body)

    # If the server returns 401 we may need to reauthenticate. Try the request
    # a second time if this happens.
    if response.status == 401 and not force_reauthorization:
        return _http_request(url,
                             body,
                             method=method,
                             force_reauthorization=True)

    if 'x-clusterfuzz-authorization' in response:
        shell.create_directory(os.path.dirname(AUTHORIZATION_CACHE_FILE),
                               create_intermediates=True)
        utils.write_data_to_file(response['x-clusterfuzz-authorization'],
                                 AUTHORIZATION_CACHE_FILE)

    return response, content
Ejemplo n.º 3
0
    def set_metadata(self, key, value, update_testcase=True):
        """Set metadata for a test case."""
        self._ensure_metadata_is_cached()
        self.metadata_cache[key] = value

        self.additional_metadata = json_utils.dumps(self.metadata_cache)
        if update_testcase:
            self.put()
Ejemplo n.º 4
0
    def delete_metadata(self, key, update_testcase=True):
        """Remove metadata key for a test case."""
        self._ensure_metadata_is_cached()

        # Make sure that the key exists in cache. If not, no work to do here.
        if key not in self.metadata_cache:
            return

        del self.metadata_cache[key]
        self.additional_metadata = json_utils.dumps(self.metadata_cache)
        if update_testcase:
            self.put()
Ejemplo n.º 5
0
    def test(self):
        """Tests json_utils.dumps with various primitive data types."""
        self.assertEqual(json_utils.dumps('string'), '"string"')

        self.assertEqual(json_utils.dumps(True), 'true')
        self.assertEqual(json_utils.dumps(False), 'false')

        self.assertEqual(json_utils.dumps(-5), '-5')
        self.assertEqual(json_utils.dumps(0), '0')
        self.assertEqual(json_utils.dumps(10), '10')
        self.assertEqual(json_utils.dumps(12.0), '12.0')

        self.assertEqual(
            json_utils.dumps(datetime.datetime(2018, 2, 4, 1, 2, 3, 9)),
            '{"__type__": "datetime", "day": 4, "hour": 1, "microsecond": 9, '
            '"minute": 2, "month": 2, "second": 3, "year": 2018}')
        self.assertEqual(
            json_utils.dumps(datetime.date(2018, 3, 20)),
            '{"__type__": "date", "day": 20, "month": 3, "year": 2018}')
Ejemplo n.º 6
0
def request(url,
            body=None,
            method=POST_METHOD,
            force_reauthorization=False,
            configuration=None):
    """Make an HTTP request to the specified URL."""
    if configuration:
        authorization = _get_authorization(force_reauthorization,
                                           configuration)
        headers = {
            "User-Agent": "clusterfuzz-reproduce",
            "Authorization": authorization,
        }
    else:
        headers = {}

    http = httplib2.Http()
    request_body = json_utils.dumps(body) if body is not None else ""
    response, content = http.request(url,
                                     method=method,
                                     headers=headers,
                                     body=request_body)

    # If the server returns 401 we may need to reauthenticate. Try the request
    # a second time if this happens.
    if response.status == 401 and not force_reauthorization:
        return request(
            url,
            body,
            method=method,
            force_reauthorization=True,
            configuration=configuration,
        )

    if AUTHORIZATION_HEADER in response:
        shell.create_directory(os.path.dirname(AUTHORIZATION_CACHE_FILE),
                               create_intermediates=True)
        utils.write_data_to_file(response[AUTHORIZATION_HEADER],
                                 AUTHORIZATION_CACHE_FILE)

    return response, content