Exemplo n.º 1
0
def pngtoppm(hashkey):
    """
    Decode PNG file and return temporary PPM filename.
    """
    pngname = png_filename(hashkey)
    ppmhandle, ppmname = tempfile.mkstemp()
    os.close(ppmhandle)
    error = os.system('pngtopnm "%s" > "%s"' % (pngname, ppmname))
    if error:
        makedirs(png_path(hashkey, 'error'))
        errorname = png_filename(hashkey, 'error')
        os.system('mv "%s" "%s"' % (pngname, errorname))
        raise Fault(
            415, "Could not decode uploaded PNG file (hashkey %s)." % hashkey)
    if not os.path.exists(ppmname):
        raise Fault(500, "Decoded screenshot file not found.")
    if os.path.getsize(ppmname) == 0:
        raise Fault(500, "Decoded screenshot file is empty.")
    return ppmname
Exemplo n.º 2
0
 def browsers_q(self):
     """Get SQL query to match requested browsers."""
     q = models.Q()
     browsers = self.browser_set.filter(active=True)
     if not browsers.count():
         raise Fault(
             404,
             u"No active browsers registered for factory %s." % self.name)
     browsers = browsers.filter(
         models.Q(last_error__isnull=True)
         | models.Q(last_error__lt=last_error_timeout()))
     if not len(browsers):
         raise Fault(
             204,
             u"All active browsers on %s are temporarily blocked because of errors."
             % self.name)
     for browser in browsers:
         q |= browser.features_q()
     return q
Exemplo n.º 3
0
def scale(ppmname, width, hashkey):
    """
    Make small preview image from uploaded screenshot.
    """
    makedirs(png_path(hashkey, size=width))
    pngname = png_filename(hashkey, size=width)
    error = os.system('pnmscale -width=%d "%s" | pnmtopng > %s' %
                      (width, ppmname, pngname))
    if error:
        raise Fault(500, "Could not create scaled preview image.")
Exemplo n.º 4
0
    def system_methodsignature(self, method_name):
        '''
        Returns the signature for a specified method
        '''

        for method in self.rpcmethods:
            if method.name == method_name:
                return method.signature
        raise Fault(APPLICATION_ERROR,
                    'No method found with name: ' + str(method_name))
Exemplo n.º 5
0
 def poll(self):
     """
     Get the next screenshot request from the queue.
     """
     while True:
         oldest = self.get_oldest_filename()
         if oldest is None:
             raise Fault(204, 'No matching request.')
         filename = oldest
         pos = filename.find('-locked-')
         if pos > -1:
             filename = filename[:pos]
         self.request_filename = '-'.join((filename, 'locked', self.factory,
                                           time.strftime(LOCKTIME_FORMAT)))
         fullpath = os.path.join(self.queue, self.request_filename)
         try:
             os.rename(os.path.join(self.queue, oldest), fullpath)
         except OSError:
             continue  # Somebody else locked this request already.
         config = {
             'filename': filename,
             'browser': 'Firefox',
             'width': 1024,
             'bpp': 24,
             'command': '',
         }
         for line in open(fullpath).readlines():
             line = line.strip()
             if not len(line):
                 continue
             match = config_line_match(line)
             if match is None:
                 raise Fault(
                     500, 'Bad request line "%s" in %s.' %
                     (line, self.request_filename))
             key, value = match.groups()
             if key in INTEGER_KEYS:
                 value = int(value)
             config[key] = value
         if 'request' not in config:
             config['request'] = config['filename']
         return config
Exemplo n.º 6
0
 def perms_error(fail):
     if isinstance(fail.value, Fault):
         ## If this is a fault, it's probably an "expected" result from
         ## zapi_perms.  Just render it.
         self._cbRender(fail.value, request)
     else:
         ## Something went wrong inside our own code
         ## TODO: Add some logic to report these somewhere
         self.log.warning(fail)
         f = Fault(5090, "Internal server error")
         self._cbRender(f, request)
Exemplo n.º 7
0
def entry_title_struct(entry):
    if not entry:
        raise Fault(404, "Post does not exist")
    struct = {
        'postid': str(entry.key().id()),
        'title': entry.title,
        'userid': '1',
    }
    if entry.date:
        struct['dateCreated'] = format_date(entry.date)
    return struct
Exemplo n.º 8
0
 def marshal(self, result, error, _id=None):
     if error:
         self.debug('response: %s: %s', error.__class__.__name__,
                    str(error))
         response = Fault(error.errno, error.strerror)
     else:
         if isinstance(result, dict):
             self.debug('response: entries returned %d',
                        result.get('count', 1))
         response = (result, )
     return xml_dumps(response, methodresponse=True)
Exemplo n.º 9
0
def offline_reply(req, msg=None):
    """Send a ServerOffline reply"""
    faultCode = koji.ServerOffline.faultCode
    if msg is None:
        faultString = "server is offline"
    else:
        faultString = msg
    response = dumps(Fault(faultCode, faultString))
    req.content_type = "text/xml"
    req.set_content_length(len(response))
    req.write(response)
Exemplo n.º 10
0
def pause_mailing(mailing_id):
    mailing = Mailing.grab(mailing_id)
    if not mailing:
        log_api.warn("Mailing [%d] not found!", mailing_id)
        raise Fault(http.NOT_FOUND, 'Mailing not found!')

    if mailing.status in (MAILING_STATUS.READY, MAILING_STATUS.RUNNING,
                          MAILING_STATUS.PAUSED):
        mailing.status = MAILING_STATUS.PAUSED
        mailing.save()
        manager = MailingManager.getInstance()
        assert (isinstance(manager, MailingManager))
        deferToThread(manager.pause_mailing, mailing)
    else:
        log_api.error("Bad mailing status: '%s' for [%d]. Can't pause it!",
                      mailing.status, mailing_id)
        raise Fault(
            http.NOT_ACCEPTABLE,
            "Bad mailing status: '%s'. Can't pause it!" % mailing.status)
    return mailing
Exemplo n.º 11
0
 def call_error(fail):
     if fail.check(errors.AsyncStack):
         # if we coded it right, we have an AsyncStack trace,
         # so let's try to find out what happened
         self.log.debug("handling a stacked exception")
         self.log.debug("%s %s" %
                        (fail.value.exception, fail.value.message))
         self.log.debug(fail.value.trace())
         return Fault(
             5091,
             "Weird server error: %s" % fail.getErrorMessage())
     elif fail.check(Fault):
         return fail
     else:
         self.log.debug("handling a non-stacked exception")
         self.log.debug(traceback.format_exc())
         self.log.debug(fail.getErrorMessage())
         return Fault(
             5092,
             "Weird server error: %s" % fail.getErrorMessage())
Exemplo n.º 12
0
def blogger_getUsersBlogs(appkey, username, password):
    if not auth_user(username, password):
        raise Fault(-1, "Authentication Failure")
    site = Site.objects.get_current()
    return [
        {
            'url': 'http://%s/' % site.domain,
            'blogid': BLOG_ID,
            'blogName': site.name
        },
    ]
Exemplo n.º 13
0
    def values(self, key, channels, start_sec, start_nano, end_sec, end_nano,
               count, interpolation):
        check_type(key, int, 'INT')
        check_type(channels, (list, tuple), 'ARRAY')
        for value in [
                start_sec, start_nano, end_sec, end_nano, count, interpolation
        ]:
            if not isinstance(value, int):
                raise ProtocolError('cr01arc01/cgi-bin/ArchiveDataServer.cgi',
                                    codes.xmlrpc.INTERNAL,
                                    'Internal Server Error', None)
        if not 0 <= interpolation <= 4:
            raise Fault(codes.archiver.ARGUMENT_ERROR,
                        'Invalid how={0}'.format(interpolation))
        if interpolation != 0:
            raise Exception('Only raw interpolation is supported by'
                            'MockArchiver.')
        key = str(key)
        self._check_key(key)
        archive_data = self._archives[key]['data']
        return_data = []

        start = start_sec + 1e-9 * start_nano
        end = end_sec + 1e-9 * end_nano

        for channel in channels:
            try:
                channel_data = archive_data[channel].copy()
                channel_values = channel_data['values']
                for index, value in enumerate(channel_values):
                    time = value['secs'] + 1e-9 * value['nano']
                    if not start <= time <= end:
                        channel_values.pop(index)
                del channel_values[count:]
            except KeyError:
                channel_data = {
                    'count': 1,
                    'meta': {
                        'alarm_high': 0.0,
                        'alarm_low': 0.0,
                        'disp_high': 10.0,
                        'disp_low': 0.0,
                        'prec': 1,
                        'type': 1,
                        'units': '<NO DATA>',
                        'warn_high': 0.0,
                        'warn_low': 0.0
                    },
                    'name': channel,
                    'type': 1,
                    'values': []
                }
            return_data.append(channel_data)
        return return_data
Exemplo n.º 14
0
def loads(data):
    d = ebdecode(data)
    if d['y'] == 'e':
        raise Fault(d['c'], d['s'])  # the server raised a fault
    elif d['y'] == 'r':
        # why is this return value so weird?
        # because it's the way that loads works in xmlrpclib
        return (d['r'], ), None
    elif d['y'] == 'q':
        return d['a'], d['q']
    raise ValueError
Exemplo n.º 15
0
    def _getFunc(self, method):
        func = self.funcs.get(method)
        if not func:
            try:
                func = resolve_dotted_attribute(self.instance, method, False)
            except AttributeError:
                pass

        if not func:
            raise Fault(2, 'method "%s" is not supported' % method)

        return func
Exemplo n.º 16
0
    def _popen(self, command, paths, fault):
        """Given a website ID, a command and a Fault template, run the command.
        """
        process = Popen4(command)
        result = process.wait()
        output = process.fromchild.read()

        if result != 0:
            message = fault[1] % (paths['repo'], result, output)
            return Fault(fault[0], message)

        return result
    def test_restart_fail_stop(self):
        """Test the restart method failing to stop the process."""
        self.rpc.supervisor.stopProcess.side_effect = Fault(
            42, "Failed to stop the process.")

        last = time.time()
        with self.assertRaises(Fault):
            self.listener.restart("foo", last)

        msg = "Failed to stop process %s (last heartbeat: %s), exiting: %s"
        args = ("foo", last, "<Fault 42: 'Failed to stop the process.'>")
        self.handler.assert_error(msg % args)
        self.rpc.supervisor.stopProcess.assert_called_once_with("foo")
Exemplo n.º 18
0
    def test_handle_rpc_fault_handles_all_faults(self):
        rpc_helper = RPCHelper()
        supervisor_fault_codes = self.attrDictWithoutUnders(
            SupervisorFaults).values()
        for item in supervisor_fault_codes:
            fault = Fault(item, getFaultDescription(item))

            if item == 2 or item == 10:
                with self.assertRaises(SystemExit):
                    rpc_helper.handle_rpc_fault(fault)
            else:
                with self.assertRaises(Fault):
                    rpc_helper.handle_rpc_fault(fault)
Exemplo n.º 19
0
 def stopProcessGroup(self, name):
     from supervisor import xmlrpc
     from xmlrpclib import Fault
     if name == 'BAD_NAME':
         raise Fault(xmlrpc.Faults.BAD_NAME, 'BAD_NAME')
     return [
         {'name':'foo_00', 'group':'foo',
          'status': xmlrpc.Faults.SUCCESS,
          'description': 'OK'},
         {'name':'foo_01', 'group':'foo',
          'status':xmlrpc.Faults.SUCCESS,
          'description': 'OK'},
         ]
Exemplo n.º 20
0
    def __init__(self, httprequest, args):
        self.request = httprequest
        self.args = args
        self.defer = None

        if len(args) != len(self.argumentTypes):
            httprequest.write(
                dumps(Fault(401, "Wrong number of args"), methodresponse=True))
            httprequest.finish()
            return

        for R, E in zip(args, self.argumentTypes):
            if not isinstance(R, E):
                httprequest.write(
                    dumps(Fault(402, "Invalid argument type"),
                          methodresponse=True))
                httprequest.finish()
                return

        self._complete = httprequest.notifyFinish()
        self._complete.addCallback(self.normalEnd)
        self._complete.addErrback(self.abortEnd)
Exemplo n.º 21
0
    def _get_story(story_id, user=None):
        """
        Gets a Story with the given ID.
        Performs permission checks for the user if provided.

        """

        try:
            story_id = int(story_id)
        except (TypeError, ValueError):
            raise Fault(400, 'Invalid post_id/story_id')

        try:
            story = Story.objects.get(id=story_id)
        except Story.DoesNotExist:
            raise Fault(404, 'Unknown post_id/story_id')

        if user and story.source:
            if not user.has_edit_permissions_to_source(story.source.pk):
                raise Fault(403, 'Not permitted to access this story')

        return story
Exemplo n.º 22
0
    def system_methodhelp(self, method_name):
        '''
        Returns documentation for a specified method
        '''

        if method_name in self.rpcmethods:
            return self.rpcmethods[method_name].help

        # this differs from what implementation in SimpleXMLRPCServer does
        # this will report via a fault or error while SimpleXMLRPCServer
        # just returns an empty string
        raise Fault(APPLICATION_ERROR,
                    'No method found with name: ' + str(method_name))
Exemplo n.º 23
0
    def render_POST(self, req):
        if req.content is None:
            req.setResponseCode(400)
            return 'Missing request body'
        # Twisted stores the request body in either StringIO or TempFile
        # so the following might result in file I/O, but won't
        # read from the request transport (circa Twisted 12.0)
        try:
            args, meth = loads(req.content.read())
            _log.info("Request: %s%s", meth, args)
        except Exception as e:
            _log.exception("Error decoding request: ")
            req.setResponseCode(400)
            return e.message

        req.setHeader('Content-Type', 'text/xml')

        try:
            if meth == 'archiver.info':
                return _info_rep
            elif meth == 'archiver.archives':
                return _archives_rep

            Dinfo = self.fetchInfo()

            if meth == 'archiver.names':
                _log.debug("%s: archiver.names %s", req.getClientIP(), args)

                @Dinfo.addCallback
                def startNames(info):
                    R = self.NamesRequest(req, args, applinfo=info)
                    return R.defer

            elif meth == 'archiver.values':
                _log.debug("%s: archiver.values %s", req.getClientIP(), args)

                @Dinfo.addCallback
                def startValues(info):
                    R = self.ValuesRequest(req, args, applinfo=info)
                    return R.defer
            else:
                _log.error("%s: Request for unknown method %s",
                           req.getClientIP(), meth)
                return dumps(Fault(400, "Unknown method"), methodresponse=True)
        except:
            _log.exception("Failure starting response: %s", req)
            cleanupRequest(None, req)
        else:
            Dinfo.addBoth(cleanupRequest, req)

        return NOT_DONE_YET
Exemplo n.º 24
0
def update(id, comment, attributes={}, notify=False, **kwargs):
    """
    array ticket.update(int id, string comment, struct attributes={}, boolean notify=False)
    Update a ticket, returning the new ticket in the same form as getTicket(). Requires a valid 'action' in attributes to support workflow.
    """
    request = kwargs['request']
    params = {}
    for key, value in attributes.iteritems():
        params[TracNameTofieldName(key)] = value

    ticket = get_ticket_model().objects.get(pk=id)
    if ticket.closed:
        return Fault(_('ticket already closed'))

    fakePOST = QueryDict('', mutable=True)
    fakePOST.update(params)

    form = TicketsForm(fakePOST,
                       instance=ticket,
                       is_stuff=request.user.is_staff,
                       user=request.user,
                       customer=request.organisation.id)
    form.cleaned_data = params
    form._changed_data = [name for name in params]

    for key, value in params.iteritems():
        setattr(ticket, key, value)
        if key == 'assigned':
            touch_ticket(value, ticket.pk)
    ticket.save(user=request.user)

    remember_changes(request, form, ticket)

    touch_ticket(request.user, ticket.pk)

    if comment:
        com = tickets_comments()
        com.comment = _('ticket changed by %(user)s\n\n%(comment)s') % {
            'user': request.user,
            'comment': comment
        }
        com.ticket = ticket
        com.action = 4
        com.save(user=request.user)

        check_references(request, com)

    if notify:
        mail_ticket(request, ticket.pk, form, is_api=True)

    return get(id, **kwargs)
Exemplo n.º 25
0
def pingback_post(response, source_uri, target_uri, slug=None, postid=None):
    """This is the pingback handler for posts."""
    if slug:
        entry = Entry.all().filter("published =", True).filter('link =',
                                                               slug).get()
    else:
        entry = Entry.all().filter("published =",
                                   True).filter('post_id =', postid).get()
    #use allow_trackback as allow_pingback
    if entry is None or not entry.allow_trackback:
        raise Fault(33, 'no such post')
    title, excerpt = get_excerpt(response, target_uri)
    if not title:
        raise Fault(17, 'no title provided')
    elif not excerpt:
        raise Fault(17, 'no useable link to target')

    comment = Comment.all().filter("entry =",
                                   entry).filter("weburl =", source_uri).get()
    if comment:
        raise Fault(48, 'pingback has already been registered')
        return

    comment = Comment(author=title[:30],
                      content="<strong>" + title[:250] + "...</strong><br/>" +
                      excerpt[:250] + '...',
                      weburl=source_uri,
                      entry=entry)
    comment.ctype = COMMENT_PINGBACK
    try:
        comment.save()
        g_blog.tigger_action("pingback_post", comment)
        logging.info("PingBack Successfully Added ! From  %s" % source_uri)
        memcache.delete("/" + entry.link)
        return True
    except:
        raise Fault(49, "Access denied.")
        return
Exemplo n.º 26
0
def redirect(http_request, factory_name, encrypted_password, request_id):
    """
    Redirect the browser to the requested URL for the screenshot, and
    save the browser in the database.
    """
    try:
        factory = get_object_or_404(Factory, name=factory_name)
        nonces.verify(http_request, factory, encrypted_password)
        request = get_object_or_404(Request, id=request_id)
        request.check_factory_lock(factory)
        user_agent = http_request.META['HTTP_USER_AGENT']
        try:
            browser = Browser.objects.get(
                factory=factory,
                user_agent=user_agent,
                active=True)
        except Browser.DoesNotExist:
            raise Fault(404, u"Unknown user agent: %s." % user_agent)
        # Check that the browser matches the request
        if (request.browser_group_id is not None and
            request.browser_group_id != browser.browser_group_id):
            raise Fault(409, u"Requested browser %s but got %s." %
                        (request.browser_group.name,
                         browser.browser_group.name))
        if ((request.major is not None and request.major != browser.major) or
            (request.minor is not None and request.minor != browser.minor)):
            raise Fault(409,
                u"Requested browser version %s.%s but got %s.%s." %
                (request.major, request.minor,
                 browser.major, browser.minor))
        # Update request with browser and redirect timestamp
        request.update_fields(browser=browser,
                              redirected=datetime.now())
        return HttpResponseRedirect(request.request_group.website.url)
    except Fault, fault:
        FactoryError.objects.create(factory=factory,
            code=fault.faultCode, message=fault.faultString)
        return error_page(http_request, "redirect error", fault.faultString)
Exemplo n.º 27
0
def read_pnm_header(ppmname):
    """
    Try to read PNM header from decoded screenshot.
    """
    header = file(ppmname, 'rb').read(1024)
    match = HEADER_MATCH(header)
    if match is None:
        raise Fault(
            500, "Could not read PNM header after decoding uploaded PNG file.")
    return (
        match.group(1),
        int(match.group(2)),
        int(match.group(3)),
    )
Exemplo n.º 28
0
def offline_reply(start_response, msg=None):
    """Send a ServerOffline reply"""
    faultCode = koji.ServerOffline.faultCode
    if msg is None:
        faultString = "server is offline"
    else:
        faultString = msg
    response = dumps(Fault(faultCode, faultString))
    headers = [
        ('Content-Length', str(len(response))),
        ('Content-Type', "text/xml"),
    ]
    start_response('200 OK', headers)
    return [response]
Exemplo n.º 29
0
def blogger_getUserInfo(appkey, username, password):
    if not auth_user(username, password):
        raise Fault(-1, "Authentication Failure")

    site = Site.objects.get_current()
    user = User.objects.get(username=username)
    return {
        'url': 'http://%s/' % site.domain,
        'nickname': user.username,
        'userid': '1',
        'email': user.email,
        'lastname': user.last_name,
        'firstname': user.first_name,
    }
Exemplo n.º 30
0
    def updateProjectName(self, lp_project_id, new_name):
        """Set the project name for the given project id.

        Returns the number of vouchers that were updated.
        """
        num_updated = 0
        for voucher in self.vouchers:
            if voucher.project_id == lp_project_id:
                voucher.project_name = new_name
                num_updated += 1
        if num_updated == 0:
            raise Fault('NotFound',
                        'No vouchers matching product id %s' % lp_project_id)
        return [num_updated]