Ejemplo n.º 1
0
                               connection.error.code(),
                               connection.error.localizedDescription())
        if connection.SSLerror:
            display_detail('SSL error detail: %s', str(connection.SSLerror))
        display_detail('Headers: %s', connection.headers)
        raise CurlError(connection.error.code(),
                        connection.error.localizedDescription())

    if connection.response != None and connection.status != 200:
        display.display_detail('Status: %s', connection.status)
        display.display_detail('Headers: %s', connection.headers)
    if connection.redirection != []:
        display.display_detail('Redirection: %s', connection.redirection)

    connection.headers['http_result_code'] = str(connection.status)
    description = NSHTTPURLResponse.localizedStringForStatusCode_(
        connection.status)
    connection.headers['http_result_description'] = description

    if str(connection.status).startswith('2'):
        return connection.get_response_data()
    else:
        # there was an HTTP error of some sort.
        raise CurlError(
            connection.status, '%s failed, HTTP returncode %s (%s)' %
            (url, connection.status,
             connection.headers.get('http_result_description', 'Failed')))


def get_hardware_info():
    '''Uses system profiler to get hardware info for this machine'''
    cmd = ['/usr/sbin/system_profiler', 'SPHardwareDataType', '-xml']
Ejemplo n.º 2
0
            keychain.debug_output()
        display.display_detail('Headers: %s', connection.headers)
        if os.path.exists(tempdownloadpath) and not resume:
            os.remove(tempdownloadpath)
        raise ConnectionError(connection.error.code(),
                              connection.error.localizedDescription())

    if connection.response is not None:
        display.display_debug1('Status: %s', connection.status)
        display.display_debug1('Headers: %s', connection.headers)
    if connection.redirection != []:
        display.display_debug1('Redirection: %s', connection.redirection)

    temp_download_exists = os.path.isfile(tempdownloadpath)
    connection.headers['http_result_code'] = str(connection.status)
    description = NSHTTPURLResponse.localizedStringForStatusCode_(
        connection.status)
    connection.headers['http_result_description'] = description

    if str(connection.status).startswith('2') and temp_download_exists:
        os.rename(tempdownloadpath, destinationpath)
        return connection.headers
    elif connection.status == 304:
        # unchanged on server
        display.display_debug1('Item is unchanged on the server.')
        return connection.headers
    else:
        # there was an HTTP error of some sort; remove our temp download.
        if os.path.exists(tempdownloadpath):
            try:
                os.unlink(tempdownloadpath)
            except OSError:
Ejemplo n.º 3
0
def get_url(url, destinationpath,
            custom_headers=None, message=None, onlyifnewer=False,
            resume=False, follow_redirects=False, pkginfo=None):
    """Gets an HTTP or HTTPS URL and stores it in
    destination path. Returns a dictionary of headers, which includes
    http_result_code and http_result_description.
    Will raise ConnectionError if Gurl has a connection error.
    Will raise HTTPError if HTTP Result code is not 2xx or 304.
    Will raise GurlError if Gurl has some other error.
    If destinationpath already exists, you can set 'onlyifnewer' to true to
    indicate you only want to download the file only if it's newer on the
    server.
    If you set resume to True, Gurl will attempt to resume an
    interrupted download."""

    tempdownloadpath = destinationpath + '.download'
    if os.path.exists(tempdownloadpath) and not resume:
        os.remove(tempdownloadpath)

    cache_data = None
    if onlyifnewer and os.path.exists(destinationpath):
        # create a temporary Gurl object so we can extract the
        # stored caching data so we can download only if the
        # file has changed on the server
        gurl_obj = Gurl.alloc().initWithOptions_({'file': destinationpath})
        cache_data = gurl_obj.getStoredHeaders()
        del gurl_obj

    # only works with NSURLSession (10.9 and newer)
    ignore_system_proxy = prefs.pref('IgnoreSystemProxies')

    options = {'url': url,
               'file': tempdownloadpath,
               'follow_redirects': follow_redirects,
               'ignore_system_proxy': ignore_system_proxy,
               'can_resume': resume,
               'additional_headers': header_dict_from_list(custom_headers),
               'download_only_if_changed': onlyifnewer,
               'cache_data': cache_data,
               'logging_function': display.display_debug2,
               'pkginfo': pkginfo}
    display.display_debug2('Options: %s' % options)

    # Allow middleware to modify options
    if middleware:
        display.display_debug2('Processing options through middleware')
        # middleware module must have process_request_options function
        # and must return usable options
        options = middleware.process_request_options(options)
        display.display_debug2('Options: %s' % options)

    connection = Gurl.alloc().initWithOptions_(options)
    stored_percent_complete = -1
    stored_bytes_received = 0
    connection.start()
    try:
        while True:
            # if we did `while not connection.isDone()` we'd miss printing
            # messages and displaying percentages if we exit the loop first
            connection_done = connection.isDone()
            if message and connection.status and connection.status != 304:
                # log always, display if verbose is 1 or more
                # also display in MunkiStatus detail field
                display.display_status_minor(message)
                # now clear message so we don't display it again
                message = None
            if (str(connection.status).startswith('2')
                    and connection.percentComplete != -1):
                if connection.percentComplete != stored_percent_complete:
                    # display percent done if it has changed
                    stored_percent_complete = connection.percentComplete
                    display.display_percent_done(
                        stored_percent_complete, 100)
            elif connection.bytesReceived != stored_bytes_received:
                # if we don't have percent done info, log bytes received
                stored_bytes_received = connection.bytesReceived
                display.display_detail(
                    'Bytes received: %s', stored_bytes_received)
            if connection_done:
                break

    except (KeyboardInterrupt, SystemExit):
        # safely kill the connection then re-raise
        connection.cancel()
        raise
    except Exception as err:  # too general, I know
        # Let us out! ... Safely! Unexpectedly quit dialogs are annoying...
        connection.cancel()
        # Re-raise the error as a GurlError
        raise GurlError(-1, str(err))

    if connection.error is not None:
        # gurl returned an error
        display.display_detail(
            'Download error %s: %s', connection.error.code(),
            connection.error.localizedDescription())
        if connection.SSLerror:
            display.display_detail(
                'SSL error detail: %s', str(connection.SSLerror))
            keychain.debug_output()
        display.display_detail('Headers: %s', connection.headers)
        if os.path.exists(tempdownloadpath) and not resume:
            os.remove(tempdownloadpath)
        raise ConnectionError(connection.error.code(),
                              connection.error.localizedDescription())

    if connection.response is not None:
        display.display_debug1('Status: %s', connection.status)
        display.display_debug1('Headers: %s', connection.headers)
    if connection.redirection != []:
        display.display_debug1('Redirection: %s', connection.redirection)

    temp_download_exists = os.path.isfile(tempdownloadpath)
    connection.headers['http_result_code'] = str(connection.status)
    description = NSHTTPURLResponse.localizedStringForStatusCode_(
        connection.status)
    connection.headers['http_result_description'] = description

    if str(connection.status).startswith('2') and temp_download_exists:
        try:
            os.rename(tempdownloadpath, destinationpath)
        except OSError as err:
            # Re-raise the error as a GurlError
            raise GurlError(-1, str(err))
        return connection.headers
    elif connection.status == 304:
        # unchanged on server
        display.display_debug1('Item is unchanged on the server.')
        return connection.headers
    else:
        # there was an HTTP error of some sort; remove our temp download.
        if os.path.exists(tempdownloadpath):
            try:
                os.unlink(tempdownloadpath)
            except OSError:
                pass
        raise HTTPError(connection.status,
                        connection.headers.get('http_result_description', ''))