Ejemplo n.º 1
0
    def get(self, *args):
        self.response.headers['Content-Type'] = "application/rss+xml"

        items = self.getItems(*args)

        itemElems = []
        for item in items:
            itemElem = Element("item")
            self._appendElementWithTextNode(itemElem, "title", item.title)
            self._appendElementWithTextNode(itemElem, "link", item.link)
            self._appendElementWithTextNode(itemElem, "description", item.description)
            self._appendElementWithTextNode(itemElem, "guid", item.link)
            if item.subPlaceFeedLink:
                self._appendElementWithTextNode(itemElem, "sharkattackdata:subPlaceFeedLink", item.subPlaceFeedLink)
            if item.attackFeedLink:
                self._appendElementWithTextNode(itemElem, "sharkattackdata:attackFeedLink", item.attackFeedLink)

            itemElems.append(itemElem)

        # Need to make channel element after the generator returned by getItems has been iterated.
        channelElem = Element("channel")
        self._appendElementWithTextNode(channelElem, "title", self._feedTitle)
        self._appendElementWithTextNode(channelElem, "link", self._feedLink)
        self._appendElementWithTextNode(channelElem, "description", self._feedDescription)

        for itemElem in itemElems:
            channelElem.appendChild(itemElem)

        responseText = """<?xml version="1.0"?>
<rss version="2.0" xmlns:sharkattackdata="http://sharkattackdata.com/rss/modules/1.0/">
%s
</rss>
""" % (channelElem.toprettyxml())
        self.response.out.write(responseText)
Ejemplo n.º 2
0
    def getXML(self):
        """
        Converts TFC implementation (dict) into a XML string representation.
        The method reflects this class implementation - dictionary containing
        list of mappings while each mapping (i.e. entry, see addMapping
        method) is a dictionary of key, value pairs.

        """
        def _getElementForMappingEntry(entry, mappingStyle):
            e = Element(mappingStyle)
            for k, v in entry.items():
                # ignore empty, None or compiled regexp items into output
                if not v or (k == "path-match-expr"):
                    continue
                e.setAttribute(k, str(v))
            return e

        root = Element("storage-mapping")  # root element name
        for mappingStyle, mappings in self.items():
            for mapping in mappings:
                mapElem = _getElementForMappingEntry(mapping, mappingStyle)
                root.appendChild(mapElem)
        return root.toprettyxml()
Ejemplo n.º 3
0
    def getXML(self):
        """
        Converts TFC implementation (dict) into a XML string representation.
        The method reflects this class implementation - dictionary containing
        list of mappings while each mapping (i.e. entry, see addMapping
        method) is a dictionary of key, value pairs.

        """

        def _getElementForMappingEntry(entry, mappingStyle):
            e = Element(mappingStyle)
            for k, v in entry.items():
                # ignore empty, None or compiled regexp items into output
                if not v or (k == "path-match-expr"):
                    continue
                e.setAttribute(k, str(v))
            return e

        root = Element("storage-mapping")  # root element name
        for mappingStyle, mappings in self.items():
            for mapping in mappings:
                mapElem = _getElementForMappingEntry(mapping, mappingStyle)
                root.appendChild(mapElem)
        return root.toprettyxml()
Ejemplo n.º 4
0
def daemonize(stdout= '/dev/null', stderr = None, stdin= '/dev/null', \
              workdir= None, startmsg = 'started with pid %s', \
              keepParent = False ):
    '''
        This forks the current process into a daemon.
        The stdin, stdout, and stderr arguments are file names that
        will be opened and be used to replace the standard file descriptors
        in sys.stdin, sys.stdout, and sys.stderr.
        These arguments are optional and default to /dev/null.
        Note that stderr is opened unbuffered, so
        if it shares a file with stdout then interleaved output
        may not appear in the order that you expect.
    '''
    # Do first fork.
    try:
        pid = os.fork()
        if pid > 0:
            if not keepParent:
                os._exit(0) # Exit first parent.
            return pid
    except OSError as e:
        sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
        print("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)
    # Decouple from parent environment.
    os.chdir("/")
    os.umask(UMASK)
    os.setsid()

    # Do second fork.
    try:
        pid = os.fork()
        if pid > 0: os._exit(0) # Exit second parent.
    except OSError as e:
        sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
        print("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)

    # Open file descriptors and print start message
    if not stderr: stderr = stdout
    si = file(stdin, 'r')
    so = file(stdout, 'a+')
    se = file(stderr, 'a+', 0)
    pid = str(os.getpid())
    sys.stderr.write("\n%s\n" % startmsg % pid)
    sys.stderr.flush()
    if workdir:
        #file(pidfile,'w+').write("%s\n" % pid)
        #Since the current working directory may be a mounted filesystem, we
        #avoid the issue of not being able to unmount the filesystem at
        #shutdown time by changing it to the root directory.
        os.chdir(workdir)
        #We probably don't want the file mode creation mask inherited from
        #the parent, so we give the child complete control over permissions.
        os.umask(UMASK)

        daemon = Element("Daemon")
        processId = Element("ProcessID")
        processId.setAttribute("Value", str(os.getpid()))
        daemon.appendChild(processId)

        parentProcessId = Element("ParentProcessID")
        parentProcessId.setAttribute("Value", str(os.getppid()))
        daemon.appendChild(parentProcessId)

        processGroupId = Element("ProcessGroupID")
        processGroupId.setAttribute("Value", str(os.getpgrp()))
        daemon.appendChild(processGroupId)

        userId = Element("UserID")
        userId.setAttribute("Value", str(os.getuid()))
        daemon.appendChild(userId)

        effectiveUserId = Element("EffectiveUserID")
        effectiveUserId.setAttribute("Value", str(os.geteuid()))
        daemon.appendChild(effectiveUserId)

        groupId = Element("GroupID")
        groupId.setAttribute("Value", str(os.getgid()))
        daemon.appendChild(groupId)

        effectiveGroupId = Element("EffectiveGroupID")
        effectiveGroupId.setAttribute("Value", str(os.getegid()))
        daemon.appendChild(effectiveGroupId)

        dom = Document()
        dom.appendChild(daemon)
        props = open("Daemon.xml", "w")
        props.write(daemon.toprettyxml())
        props.close()

# Redirect standard file descriptors.
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())
    return 0
Ejemplo n.º 5
0
def daemonize(stdout= '/dev/null', stderr = None, stdin= '/dev/null', \
              workdir= None, startmsg = 'started with pid %s', \
              keepParent = False ):
    '''
        This forks the current process into a daemon.
        The stdin, stdout, and stderr arguments are file names that
        will be opened and be used to replace the standard file descriptors
        in sys.stdin, sys.stdout, and sys.stderr.
        These arguments are optional and default to /dev/null.
        Note that stderr is opened unbuffered, so
        if it shares a file with stdout then interleaved output
        may not appear in the order that you expect.
    '''
    # Do first fork.
    try:
        pid = os.fork()
        if pid > 0:
            if not keepParent:
                os._exit(0) # Exit first parent.
            return pid
    except OSError as e:
        sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
        print("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)
    # Decouple from parent environment.
    os.chdir("/")
    os.umask(UMASK)
    os.setsid()

    # Do second fork.
    try:
        pid = os.fork()
        if pid > 0: os._exit(0) # Exit second parent.
    except OSError as e:
        sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
        print("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)

    # Open file descriptors and print start message
    if not stderr: stderr = stdout
    si = file(stdin, 'r')
    so = file(stdout, 'a+')
    se = file(stderr, 'a+', 0)
    pid = str(os.getpid())
    sys.stderr.write("\n%s\n" % startmsg % pid)
    sys.stderr.flush()
    if workdir:
        #file(pidfile,'w+').write("%s\n" % pid)
        #Since the current working directory may be a mounted filesystem, we
        #avoid the issue of not being able to unmount the filesystem at
        #shutdown time by changing it to the root directory.
        os.chdir(workdir)
        #We probably don't want the file mode creation mask inherited from
        #the parent, so we give the child complete control over permissions.
        os.umask(UMASK)

        daemon = Element("Daemon")
        processId = Element("ProcessID")
        processId.setAttribute("Value", str(os.getpid()))
        daemon.appendChild(processId)

        parentProcessId = Element("ParentProcessID")
        parentProcessId.setAttribute("Value", str(os.getppid()))
        daemon.appendChild(parentProcessId)

        processGroupId = Element("ProcessGroupID")
        processGroupId.setAttribute("Value", str(os.getpgrp()))
        daemon.appendChild(processGroupId)

        userId = Element("UserID")
        userId.setAttribute("Value", str(os.getuid()))
        daemon.appendChild(userId)

        effectiveUserId = Element("EffectiveUserID")
        effectiveUserId.setAttribute("Value", str(os.geteuid()))
        daemon.appendChild(effectiveUserId)

        groupId = Element("GroupID")
        groupId.setAttribute("Value", str(os.getgid()))
        daemon.appendChild(groupId)

        effectiveGroupId = Element("EffectiveGroupID")
        effectiveGroupId.setAttribute("Value", str(os.getegid()))
        daemon.appendChild(effectiveGroupId)

        dom = Document()
        dom.appendChild(daemon)
        props = open("Daemon.xml", "w")
        props.write(daemon.toprettyxml())
        props.close()

# Redirect standard file descriptors.
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())
    return 0
Ejemplo n.º 6
0
        effectiveUserId = Element("EffectiveUserID")
        effectiveUserId.setAttribute("Value", str(os.geteuid()))
        daemon.appendChild(effectiveUserId)

        groupId = Element("GroupID")
        groupId.setAttribute("Value", str(os.getgid()))
        daemon.appendChild(groupId)

        effectiveGroupId = Element("EffectiveGroupID")
        effectiveGroupId.setAttribute("Value", str(os.getegid()))
        daemon.appendChild(effectiveGroupId)

        dom = Document()
        dom.appendChild(daemon)
        props = open("Daemon.xml", "w")
        props.write(daemon.toprettyxml())
        props.close()

    # Redirect standard file descriptors.
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())
    return 0


def test():
    """
        This is an example main function run by the daemon.
        This prints a count and timestamp once per second.
    """
    logFile = os.path.join("/tmp/daemon.logging")
Ejemplo n.º 7
0
        effectiveUserId = Element("EffectiveUserID")
        effectiveUserId.setAttribute("Value", str(os.geteuid()))
        daemon.appendChild(effectiveUserId)

        groupId = Element("GroupID")
        groupId.setAttribute("Value", str(os.getgid()))
        daemon.appendChild(groupId)

        effectiveGroupId = Element("EffectiveGroupID")
        effectiveGroupId.setAttribute("Value", str(os.getegid()))
        daemon.appendChild(effectiveGroupId)

        dom = Document()
        dom.appendChild(daemon)
        props = open("Daemon.xml", "w")
        props.write(daemon.toprettyxml())
        props.close()

# Redirect standard file descriptors.
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())
    return 0


def test():
    '''
        This is an example main function run by the daemon.
        This prints a count and timestamp once per second.
    '''
    logFile = os.path.join("/tmp/daemon.logging")