示例#1
0
    def _create_eggs_cache(self):
        log.info("Creating cache for plugins eggs")

        # $HOME/.stoq/plugins
        default_store = get_default_store()
        path = os.path.join(get_application_dir(), 'plugins')
        if not os.path.exists(path):
            os.makedirs(path)

        existing_eggs = {
            unicode(os.path.basename(f)[:-4]): md5sum_for_filename(f)
            for f in glob.iglob(os.path.join(path, '*.egg'))
        }

        # Now extract all eggs from the database and put it where stoq know
        # how to load them
        for plugin_name, egg_md5sum in default_store.using(PluginEgg).find(
            (PluginEgg.plugin_name, PluginEgg.egg_md5sum)):
            # A little optimization to avoid loading the egg in memory if we
            # already have a valid version cached.
            if existing_eggs.get(plugin_name, u'') == egg_md5sum:
                log.info("Plugin %r egg md5sum matches. Skipping it..." %
                         (plugin_name, ))
                continue

            log.info("Creating egg cache for plugin %r" % (plugin_name, ))
            egg_filename = '%s.egg' % (plugin_name, )
            plugin_egg = default_store.find(PluginEgg,
                                            plugin_name=plugin_name).one()

            with open(os.path.join(path, egg_filename), 'wb') as f:
                f.write(plugin_egg.egg_content)
示例#2
0
    def _create_eggs_cache(self):
        log.info("Creating cache for plugins eggs")

        # $HOME/.stoq/plugins
        default_store = get_default_store()
        path = os.path.join(get_application_dir(), 'plugins')
        if not os.path.exists(path):
            os.makedirs(path)

        existing_eggs = {
            unicode(os.path.basename(f)[:-4]): md5sum_for_filename(f) for f in
            glob.iglob(os.path.join(path, '*.egg'))}

        # Now extract all eggs from the database and put it where stoq know
        # how to load them
        for plugin_name, egg_md5sum in default_store.using(PluginEgg).find(
                (PluginEgg.plugin_name, PluginEgg.egg_md5sum)):
            # A little optimization to avoid loading the egg in memory if we
            # already have a valid version cached.
            if existing_eggs.get(plugin_name, u'') == egg_md5sum:
                log.info("Plugin %r egg md5sum matches. Skipping it..." % (
                    plugin_name, ))
                continue

            log.info("Creating egg cache for plugin %r" % (plugin_name, ))
            egg_filename = '%s.egg' % (plugin_name, )
            plugin_egg = default_store.find(
                PluginEgg, plugin_name=plugin_name).one()

            with open(os.path.join(path, egg_filename), 'wb') as f:
                f.write(plugin_egg.egg_content)
示例#3
0
    def _setup_twisted(self):
        root = resource.Resource()
        checker = _PasswordChecker()
        cf = BasicCredentialFactory(SERVER_NAME)

        # eggs
        eggs_path = pkg_resources.resource_filename(
            'stoqserver', 'data/eggs')
        root.putChild(
            'eggs',
            self._get_resource_wrapper(static.File(eggs_path), checker, cf))

        # conf
        root.putChild(
            'login',
            self._get_resource_wrapper(static.File(APP_CONF_FILE), checker, cf))

        # md5sum
        with tempfile.NamedTemporaryFile(delete=False) as f:
            for egg in SERVER_EGGS:
                egg_path = os.path.join(eggs_path, egg)
                if not os.path.exists(eggs_path):
                    continue

                f.write('%s:%s\n' % (egg, md5sum_for_filename(egg_path)))

        root.putChild('md5sum', static.File(f.name))

        site = server.Site(root)
        site.protocol = HTTPChannel

        reactor.listenTCP(SERVER_AVAHI_PORT, site)
示例#4
0
    def test_md5sum_for_filename(self):
        # Test with a known md5sum
        with tempfile.NamedTemporaryFile() as f:
            f.write('foobar')
            f.flush()

            md5sum = subprocess.check_output(['md5sum', f.name]).split(' ')[0]
            # Make sure the md5sum tool is really working
            self.assertEqual(md5sum, '3858f62230ac3c915f300c664312c63f')
            self.assertEquals(md5sum_for_filename(f.name), md5sum)

        # Test with a random md5sum in a large file
        with tempfile.NamedTemporaryFile() as f:
            for x in xrange(1000000):
                f.write(random.choice(string.printable))
            f.flush()

            md5sum = subprocess.check_output(['md5sum', f.name]).split(' ')[0]
            self.assertEquals(md5sum_for_filename(f.name), md5sum)
示例#5
0
    def test_md5sum_for_filename(self):
        # Test with a known md5sum
        with tempfile.NamedTemporaryFile() as f:
            f.write('foobar')
            f.flush()

            md5sum = subprocess.check_output(['md5sum', f.name]).split(' ')[0]
            # Make sure the md5sum tool is really working
            self.assertEqual(md5sum, '3858f62230ac3c915f300c664312c63f')
            self.assertEquals(md5sum_for_filename(f.name), md5sum)

        # Test with a random md5sum in a large file
        with tempfile.NamedTemporaryFile() as f:
            for x in range(1000000):
                f.write(random.choice(string.printable))
            f.flush()

            md5sum = subprocess.check_output(['md5sum', f.name]).split(' ')[0]
            self.assertEquals(md5sum_for_filename(f.name), md5sum)
示例#6
0
 def callback(filename):
     md5sum = unicode(md5sum_for_filename(filename))
     with open(filename) as f:
         with new_store() as store:
             PluginEgg(
                 store=store,
                 plugin_name=plugin_name,
                 egg_md5sum=md5sum,
                 egg_content=f.read(),
             )
     self._reload()
示例#7
0
    def _insert_egg(self, plugin_name, filename):
        from stoqlib.database.runtime import new_store
        from stoqlib.domain.plugin import PluginEgg
        from stoqlib.lib.fileutils import md5sum_for_filename

        print('Inserting plugin egg %s %s' % (plugin_name, filename))
        md5sum = str(md5sum_for_filename(filename))
        with open(filename, 'rb') as f:
            with new_store() as store:
                plugin_egg = store.find(PluginEgg, plugin_name=plugin_name).one()
                if plugin_egg is None:
                    plugin_egg = PluginEgg(
                        store=store,
                        plugin_name=plugin_name,
                    )
                plugin_egg.egg_content = f.read()
                plugin_egg.egg_md5sum = md5sum
示例#8
0
    def _insert_egg(self, plugin_name, filename):
        from stoqlib.database.runtime import new_store
        from stoqlib.domain.plugin import PluginEgg
        from stoqlib.lib.fileutils import md5sum_for_filename

        print('Inserting plugin egg %s %s' % (plugin_name, filename))
        md5sum = str(md5sum_for_filename(filename))
        with open(filename, 'rb') as f:
            with new_store() as store:
                plugin_egg = store.find(PluginEgg, plugin_name=plugin_name).one()
                if plugin_egg is None:
                    plugin_egg = PluginEgg(
                        store=store,
                        plugin_name=plugin_name,
                    )
                plugin_egg.egg_content = f.read()
                plugin_egg.egg_md5sum = md5sum
示例#9
0
        def callback(filename):
            md5sum = unicode(md5sum_for_filename(filename))
            with open(filename) as f:
                with new_store() as store:
                    existing_egg = store.find(PluginEgg,
                                              plugin_name=plugin_name).one()
                    if existing_egg is not None:
                        existing_egg.egg_content = f.read()
                        existing_egg.md5sum = md5sum
                    else:
                        PluginEgg(
                            store=store,
                            plugin_name=plugin_name,
                            egg_md5sum=md5sum,
                            egg_content=f.read(),
                        )

            self._reload()
示例#10
0
    def run(self):
        try:
            self._setup_avahi()
        except dbus.exceptions.DBusException as e:
            logger.warning("Failed to setup avahi: %s", str(e))

        # md5sum
        with tempfile.NamedTemporaryFile(delete=False) as f:
            for egg in SERVER_EGGS:
                egg_path = os.path.join(_eggs_path, egg)
                if not os.path.exists(_eggs_path):
                    continue

                f.write('%s:%s\n' % (egg, md5sum_for_filename(egg_path)))

        global _md5sum_path
        _md5sum_path = f.name
        server = BaseHTTPServer.HTTPServer(('localhost', self._port),
                                           _RequestHandler)
        server.serve_forever()
示例#11
0
    def run(self):
        if dbus is not None:
            try:
                self._setup_avahi()
            except dbus.exceptions.DBusException as e:
                logger.warning("Failed to setup avahi: %s", str(e))

        # md5sum
        with tempfile.NamedTemporaryFile(delete=False) as f:
            for egg in SERVER_EGGS:
                egg_path = os.path.join(_eggs_path, egg)
                if not os.path.exists(_eggs_path):
                    continue

                f.write('%s:%s\n' % (egg, md5sum_for_filename(egg_path)))

        global _md5sum_path
        _md5sum_path = f.name
        server = http.server.HTTPServer(('localhost', self._port), _RequestHandler)
        server.serve_forever()