def post_stats(cls, stats_url, stats, timeout=2, auth_provider=None):
    """POST stats to the given url.

    :return: True if upload was successful, False otherwise.
    """
    def error(msg):
      # Report aleady closed, so just print error.
      print('WARNING: Failed to upload stats to {}. due to {}'.format(stats_url, msg),
            file=sys.stderr)
      return False

    # TODO(benjy): The upload protocol currently requires separate top-level params, with JSON
    # values.  Probably better for there to be one top-level JSON value, namely json.dumps(stats).
    # But this will first require changing the upload receiver at every shop that uses this.
    params = {k: cls._json_dump_options(v) for (k, v) in stats.items()}
    cookies = Cookies.global_instance()
    auth_provider = auth_provider or '<provider>'

    # We can't simply let requests handle redirects, as we only allow them for specific codes:
    # 307 and 308 indicate that the redirected request must use the same method, POST in this case.
    # So they indicate a true redirect of the POST itself, and we allow them.
    # The other redirect codes either must, or in practice do, cause the user agent to switch the
    # method to GET. So when they are encountered on a POST, it indicates an auth problem (a
    # redirection to a login page).
    def do_post(url, num_redirects_allowed):
      if num_redirects_allowed < 0:
        return error('too many redirects.')
      r = requests.post(url, data=params, timeout=timeout,
                        cookies=cookies.get_cookie_jar(), allow_redirects=False)
      if r.status_code in {307, 308}:
        return do_post(r.headers['location'], num_redirects_allowed - 1)
      elif r.status_code != 200:
        error('HTTP error code: {}. Reason: {}.'.format(r.status_code, r.reason))
        if 300 <= r.status_code < 400 or r.status_code == 401:
          print('Use `path/to/pants login --to={}` to authenticate against the stats '
                'upload service.'.format(auth_provider), file=sys.stderr)
        return False
      return True

    try:
      return do_post(stats_url, num_redirects_allowed=6)
    except Exception as e:  # Broad catch - we don't want to fail the build over upload errors.
      return error('Error: {}'.format(e))
def post_stats ( cls , stats_url , stats , timeout = 2 , auth_provider = None ) : def error ( msg ) : print ( 'WARNING: Failed to upload stats to {}. due to {}' . format ( stats_url , msg ) , file = sys . stderr ) return False params = { k : cls . _json_dump_options ( v ) for ( k , v ) in stats . items ( ) } cookies = Cookies . global_instance ( ) auth_provider = auth_provider or '<provider>' def do_post ( url , num_redirects_allowed ) : if num_redirects_allowed < 0 : return error ( 'too many redirects.' ) r = requests . post ( url , data = params , timeout = timeout , cookies = cookies . get_cookie_jar ( ) , allow_redirects = False ) if r . status_code in { 307 , 308 } : return do_post ( r . headers [ 'location' ] , num_redirects_allowed - 1 ) elif r . status_code != 200 : error ( 'HTTP error code: {}. Reason: {}.' . format ( r . status_code , r . reason ) ) if 300 <= r . status_code < 400 or r . status_code == 401 : print ( 'Use `path/to/pants login --to={}` to authenticate against the stats ' 'upload service.' . format ( auth_provider ) , file = sys . stderr ) return False return True try : return do_post ( stats_url , num_redirects_allowed = 6 ) except Exception as e : return error ( 'Error: {}' . format ( e ) )
POST stats to the given url.

    :return: True if upload was successful, False otherwise.