Пример #1
0
    def _install_plugin(zip_file_data, plugin, p_dir):
        import os
        import shutil
        import zipfile
        import datetime
        import hashlib
        from ospy.helpers import mkdir_p
        from ospy.helpers import del_rw
        from ospy.options import options

        # First stop it if it is running:
        enabled = plugin in options.enabled_plugins
        if enabled:
            options.enabled_plugins.remove(plugin)
            start_enabled_plugins()

        # Clean the target directory and create it if needed:
        target_dir = plugin_dir(plugin)
        if os.path.exists(target_dir):
            old_files = os.listdir(target_dir)
            for old_file in old_files:
                if old_file != 'data':
                    shutil.rmtree(os.path.join(target_dir, old_file), onerror=del_rw)
        else:
            mkdir_p(target_dir)

        # Load the zip file:
        zip_file = zipfile.ZipFile(zip_file_data)
        infos = zip_file.infolist()

        # Version information:
        plugin_hash = ''
        plugin_date = datetime.datetime(1970, 1, 1)

        # Extract all files:
        for zip_info in infos:
            zip_name = zip_info.filename
            if zip_name.startswith(p_dir):
                relative_name = zip_name[len(p_dir):].lstrip('/')
                target_name = os.path.join(target_dir, relative_name)
                if relative_name:
                    if relative_name.endswith('/'):
                        mkdir_p(target_name)
                    else:
                        plugin_date = max(plugin_date, datetime.datetime(*zip_info.date_time))
                        plugin_hash += hex(zip_info.CRC)
                        contents = zip_file.read(zip_name)
                        with open(target_name, 'wb') as fh:
                            fh.write(contents)

        options.plugin_status[plugin] = {
            'hash': hashlib.md5(plugin_hash).hexdigest(),
            'date': plugin_date
        }
        options.plugin_status = options.plugin_status

        # Start again if needed:
        if enabled:
            options.enabled_plugins.append(plugin)
            start_enabled_plugins()
Пример #2
0
    def _install_plugin(zip_file_data, plugin, p_dir):
        import os
        import shutil
        import zipfile
        import datetime
        import hashlib
        from ospy.helpers import mkdir_p
        from ospy.helpers import del_rw
        from ospy.options import options

        # First stop it if it is running:
        enabled = plugin in options.enabled_plugins
        if enabled:
            options.enabled_plugins.remove(plugin)
            start_enabled_plugins()

        # Clean the target directory and create it if needed:
        target_dir = plugin_dir(plugin)
        if os.path.exists(target_dir):
            old_files = os.listdir(target_dir)
            for old_file in old_files:
                if old_file != 'data':
                    shutil.rmtree(os.path.join(target_dir, old_file), onerror=del_rw)
        else:
            mkdir_p(target_dir)

        # Load the zip file:
        zip_file = zipfile.ZipFile(zip_file_data)
        infos = zip_file.infolist()

        # Version information:
        plugin_hash = ''
        plugin_date = datetime.datetime(1970, 1, 1)

        # Extract all files:
        for zip_info in infos:
            zip_name = zip_info.filename
            if zip_name.startswith(p_dir):
                relative_name = zip_name[len(p_dir):].lstrip('/')
                target_name = os.path.join(target_dir, relative_name)
                if relative_name:
                    if relative_name.endswith('/'):
                        mkdir_p(target_name)
                    else:
                        plugin_date = max(plugin_date, datetime.datetime(*zip_info.date_time))
                        plugin_hash += hex(zip_info.CRC)
                        contents = zip_file.read(zip_name)
                        with open(target_name, 'wb') as fh:
                            fh.write(contents)

        options.plugin_status[plugin] = {
            'hash': hashlib.md5(plugin_hash).hexdigest(),
            'date': plugin_date
        }
        options.plugin_status = options.plugin_status

        # Start again if needed:
        if enabled:
            options.enabled_plugins.append(plugin)
            start_enabled_plugins()
Пример #3
0
    def _get_wunderground_data(self, query, name=None, force=False):
        with self._lock:
            query, path = self._get_query_path(query, name)
            mkdir_p(os.path.dirname(path))

            try_nr = 1
            data = {}
            while try_nr <= 2:
                try:
                    if not os.path.exists(path) or (
                            force and
                            time.time() - os.path.getmtime(path) > 2 * 3600):
                        # Max 5 calls per minute:
                        self._requests.append(time.time())
                        if len(self._requests) > 5:
                            del self._requests[0]
                            if self._requests[-1] - self._requests[0] < 60:
                                logging.info(
                                    'Waiting for weather information.')
                                time.sleep(60 - (self._requests[-1] -
                                                 self._requests[0]))

                        with open(path, 'wb') as fh:
                            print query
                            req = urllib2.urlopen(
                                "http://api.wunderground.com/api/" +
                                self._wunderground_key + "/" + query)
                            while True:
                                chunk = req.read(20480)
                                if not chunk:
                                    break
                                fh.write(chunk)

                    try:
                        with file(path, 'r') as fh:
                            data = json.load(fh)
                    except ValueError:
                        raise Exception('Failed to read ' + path + '.')

                    if data is not None:
                        if 'error' in data['response']:
                            raise Exception(path + ': ' +
                                            str(data['response']['error']))
                    else:
                        raise Exception('JSON decoding failed.')

                except Exception as err:
                    if try_nr < 2:
                        log.debug(str(err), 'Retrying.')
                        os.remove(path)
                    else:
                        raise
                try_nr += 1

            return data
Пример #4
0
def start_enabled_plugins():
    from ospy.helpers import mkdir_p
    from ospy.options import options
    import logging
    import time
    
    for module in available():
        if module in options.enabled_plugins and module not in __running:
            plugin_n = module
            import_name = __name__ + '.' + module
            try:
                time.sleep(0.1)
                plugin = getattr(__import__(import_name), module)
                plugin = reload(plugin)
                plugin_n = plugin.NAME
                mkdir_p(plugin_data_dir(module))
                mkdir_p(plugin_docs_dir(module))

                plugin.start()
                __running[module] = plugin
                logging.info('Started the {} plug-in.'.format(plugin_n))

                if plugin.LINK is not None and not (plugin.LINK.startswith(module) or plugin.LINK.startswith(__name__)):
                    plugin.LINK = module + '.' + plugin.LINK

            except Exception:
                logging.error('Failed to load the {} plug-in:'.format(plugin_n) + '\n' + traceback.format_exc())
                options.enabled_plugins.remove(module)

    for module, plugin in __running.copy().iteritems():
        if module not in options.enabled_plugins:
            plugin_n = plugin.NAME
            try:
                plugin.stop()
                del __running[module]
                logging.info('Stopped the {} plug-in.'.format(plugin_n))
            except Exception:
                logging.error('Failed to stop the {} plug-in:'.format(plugin_n) + '\n' + traceback.format_exc())
Пример #5
0
def start_enabled_plugins():
    from ospy.helpers import mkdir_p
    from ospy.options import options
    import logging

    for module in available():
        if module in options.enabled_plugins and module not in __running:
            plugin_n = module
            import_name = __name__ + '.' + module
            try:
                plugin = getattr(__import__(import_name), module)
                plugin = reload(plugin)
                plugin_n = plugin.NAME
                mkdir_p(plugin_data_dir(module))
                mkdir_p(plugin_docs_dir(module))

                plugin.start()
                __running[module] = plugin
                logging.info('Started the {} plug-in.'.format(plugin_n))

                if plugin.LINK is not None and not (plugin.LINK.startswith(module) or plugin.LINK.startswith(__name__)):
                    plugin.LINK = module + '.' + plugin.LINK

            except Exception:
                logging.error('Failed to load the {} plug-in:'.format(plugin_n) + '\n' + traceback.format_exc())
                options.enabled_plugins.remove(module)

    for module, plugin in __running.copy().iteritems():
        if module not in options.enabled_plugins:
            plugin_n = plugin.NAME
            try:
                plugin.stop()
                del __running[module]
                logging.info('Stopped the {} plug-in.'.format(plugin_n))
            except Exception:
                logging.error('Failed to stop the {} plug-in:'.format(plugin_n) + '\n' + traceback.format_exc())