def convertpickle(src, target): import gozerbot.compat.persist p = gozerbot.compat.persist.Persist(src) if p and p.data: pers = Persist(target) if not pers.data: pers.data = {} pers.data.update(p.data) try: pers.save() except TypeError: pers2 = Persist(target) if not pers2.data: pers2.data = {} for item, value in p.data.iteritems(): pers2.data[jsonstring(item)] = value pers2.save()
def save(self, attributes=[]): target = {} if attributes: for key in attributes: target[key] = self[key] else: target = cpy(self) targetfile = getdatadir() + os.sep + "containers" + os.sep + str(self.createtime) + "_" + stripname(self.origin) p = Persist(targetfile) p.data = getjson().dumps(target) p.save()
def getaliases(ddir=None, force=True): """ return global aliases. """ global aliases if not aliases or force: from jsb.lib.persist import Persist from jsb.utils.lazydict import LazyDict d = ddir or getdatadir() p = Persist(d + os.sep + "run" + os.sep + "aliases") if not p.data: p.data = LazyDict() aliases = p.data return aliases
def savealiases(ddir=None): """ return global aliases. """ global aliases if aliases: logging.warn("saving aliases") from jsb.lib.persist import Persist from jsb.utils.lazydict import LazyDict d = ddir or getdatadir() p = Persist(d + os.sep + "run" + os.sep + "aliases") p.data = aliases p.save() return aliases
def save(self, attributes=[]): target = {} if attributes: for key in attributes: target[key] = self[key] else: target = cpy(self) targetfile = getdatadir() + os.sep + "containers" + os.sep + str( self.createtime) + "_" + stripname(self.origin) p = Persist(targetfile) p.data = getjson().dumps(target) p.save()
def boot(ddir=None, force=False, encoding="utf-8", umask=None, saveperms=True, fast=False, clear=False, loadall=False): """ initialize the bot. """ global plugin_packages try: if os.getuid() == 0: print "don't run the bot as root" os._exit(1) except AttributeError: pass from jsb.lib.datadir import getdatadir, setdatadir if ddir: setdatadir(ddir) origdir = ddir ddir = ddir or getdatadir() if not ddir: logging.error("can't determine datadir to boot from") raise Exception("can't determine datadir") if not ddir in sys.path: sys.path.append(ddir) makedirs(ddir) if os.path.isdir("/var/run/jsb") and botuser() == "jsb": rundir = "/var/run/jsb" else: rundir = ddir + os.sep + "run" try: k = open(rundir + os.sep + 'jsb.pid', 'w') k.write(str(os.getpid())) k.close() except IOError: pass try: reload(sys) sys.setdefaultencoding(encoding) except (AttributeError, IOError): handle_exception() try: if not umask: checkpermissions(getdatadir(), 0700) else: checkpermissions(getdatadir(), umask) except: handle_exception() from jsb.lib.plugins import plugs global loaded global cmndtable global retable global pluginlist global callbacktable global shorttable global cmndperms global timestamps global plugwhitelist global plugblacklist if not retable: retable = Persist(rundir + os.sep + 'retable') if clear: retable.data = {} if not cmndtable: cmndtable = Persist(rundir + os.sep + 'cmndtable') if clear: cmndtable.data = {} if not pluginlist: pluginlist = Persist(rundir + os.sep + 'pluginlist') if clear: pluginlist.data = [] if not callbacktable: callbacktable = Persist(rundir + os.sep + 'callbacktable') if clear: callbacktable.data = {} if not shorttable: shorttable = Persist(rundir + os.sep + 'shorttable') if clear: shorttable.data = {} if not timestamps: timestamps = Persist(rundir + os.sep + 'timestamps') if not plugwhitelist: plugwhitelist = Persist(rundir + os.sep + 'plugwhitelist') if not plugwhitelist.data: plugwhitelist.data = [] if not plugblacklist: plugblacklist = Persist(rundir + os.sep + 'plugblacklist') if not plugblacklist.data: plugblacklist.data = [] if not cmndperms: cmndperms = Config('cmndperms', ddir=ddir) changed = [] gotlocal = False dosave = clear or False maincfg = getmainconfig(ddir=ddir) logging.warn("mainconfig used is %s" % maincfg.cfile) if os.path.isdir('jsb'): gotlocal = True packages = find_packages("jsb" + os.sep + "plugs") pluglist = [x for x in packages if not 'db' in x] for p in pluglist: if p not in plugin_packages: plugin_packages.append(p) for plug in default_plugins: plugs.reload(plug, showerror=True, force=True) changed = scandir(getdatadir() + os.sep + 'myplugs', dbenable=maincfg.dbenable) if changed: logging.debug("myplugs has changed -=- %s" % str(changed)) for plugfile in changed: if "gae" in plugfile: continue try: plugs.reloadfile(plugfile, force=True) except Exception, ex: handle_exception() dosave = True
def getaliases(): from jsb.lib.persist import Persist p = Persist(getdatadir() + os.sep + "aliases") if not p.data: p.data = {} return p
def boot(ddir=None, force=False, encoding="utf-8", umask=None, saveperms=True, fast=False, clear=False, loadall=False): """ initialize the bot. """ global plugin_packages try: if os.getuid() == 0: print "don't run the bot as root" os._exit(1) except AttributeError: pass logging.warn("starting!") from jsb.lib.datadir import getdatadir, setdatadir if ddir: setdatadir(ddir) origdir = ddir ddir = ddir or getdatadir() if not ddir: logging.error("can't determine datadir to boot from") ; raise Exception("can't determine datadir") if not ddir in sys.path: sys.path.append(ddir) makedirs(ddir) if os.path.isdir("/var/run/jsb") and botuser() == "jsb": rundir = "/var/run/jsb" else: rundir = ddir + os.sep + "run" try: k = open(rundir + os.sep + 'jsb.pid','w') k.write(str(os.getpid())) k.close() except IOError: pass try: reload(sys) sys.setdefaultencoding(encoding) except (AttributeError, IOError): handle_exception() try: if not umask: checkpermissions(getdatadir(), 0700) else: checkpermissions(getdatadir(), umask) except: handle_exception() from jsb.lib.plugins import plugs global loaded global cmndtable global retable global pluginlist global callbacktable global shorttable global cmndperms global timestamps global plugwhitelist global plugblacklist if not retable: retable = Persist(rundir + os.sep + 'retable') if clear: retable.data = {} if not cmndtable: cmndtable = Persist(rundir + os.sep + 'cmndtable') if clear: cmndtable.data = {} if not pluginlist: pluginlist = Persist(rundir + os.sep + 'pluginlist') if clear: pluginlist.data = [] if not callbacktable: callbacktable = Persist(rundir + os.sep + 'callbacktable') if clear: callbacktable.data = {} if not shorttable: shorttable = Persist(rundir + os.sep + 'shorttable') if clear: shorttable.data = {} if not timestamps: timestamps = Persist(rundir + os.sep + 'timestamps') #if clear: timestamps.data = {} if not plugwhitelist: plugwhitelist = Persist(rundir + os.sep + 'plugwhitelist') if not plugwhitelist.data: plugwhitelist.data = [] if not plugblacklist: plugblacklist = Persist(rundir + os.sep + 'plugblacklist') if not plugblacklist.data: plugblacklist.data = [] if not cmndperms: cmndperms = Config('cmndperms', ddir=ddir) changed = [] gotlocal = False dosave = clear or False maincfg = getmainconfig(ddir=ddir) logging.warn("mainconfig used is %s" % maincfg.cfile) if os.path.isdir('jsb'): gotlocal = True packages = find_packages("jsb" + os.sep + "plugs") pluglist = [x for x in packages if not 'db' in x] for p in pluglist: if p not in plugin_packages: plugin_packages.append(p) for plug in default_plugins: plugs.reload(plug, showerror=True, force=True) changed = scandir(getdatadir() + os.sep + 'myplugs', dbenable=maincfg.dbenable) if changed: logging.debug("myplugs has changed -=- %s" % str(changed)) for plugfile in changed: if "gae" in plugfile: continue try: plugs.reloadfile(plugfile, force=True) except Exception, ex: handle_exception() dosave = True configchanges = checkconfig() if configchanges: logging.info("there are configuration changes: %s" % str(configchanges)) for f in configchanges: if 'mainconfig' in f: force = True ; dosave = True if os.path.isdir('jsb'): corechanges = scandir("jsb" + os.sep + "plugs", dbenable=maincfg.dbenable) if corechanges: logging.debug("core changed -=- %s" % str(corechanges)) for plugfile in corechanges: if not maincfg.dbenable and "db" in plugfile: continue if "gae" in plugfile: continue try: plugs.reloadfile(plugfile, force=True) except Exception, ex: handle_exception() dosave = True if maincfg.dbenable: if not 'jsb.plugs.db' in plugin_packages: plugin_packages.append("jsb.plugs.db") try: from jsb.db import getmaindb from jsb.db.tables import tablestxt db = getmaindb() if db: db.define(tablestxt) except Exception, ex: logging.warn("could not initialize database %s" % str(ex)) else: logging.warn("db not enabled, set dbenable = 1 in %s to enable" % getmainconfig().cfile) try: plugin_packages.remove("jsb.plugs.db") except ValueError: pass if force or dosave or not cmndtable.data or len(cmndtable.data) < 100: logging.debug("using target %s" % str(plugin_packages)) plugs.loadall(plugin_packages, force=True) savecmndtable(saveperms=saveperms) savepluginlist() savecallbacktable() savealiases() logging.warn("ready")
def boot(ddir=None, force=False, encoding="utf-8", umask=None, saveperms=True, fast=False, clear=False): """ initialize the bot. """ try: if os.getuid() == 0: print "don't run the bot as root" os._exit(1) except AttributeError: pass logging.info("booting ..") from jsb.lib.datadir import getdatadir, setdatadir if ddir: setdatadir(ddir) origdir = ddir ddir = ddir or getdatadir() if not ddir: logging.error("can't determine datadir to boot from") os._exit(1) if not ddir in sys.path: sys.path.append(ddir) makedirs(ddir) if os.path.isdir("/var/run/jsb") and botuser() == "jsb": rundir = "/var/run/jsb" else: rundir = ddir + os.sep + "run" try: k = open(rundir + os.sep + "jsb.pid", "w") k.write(str(os.getpid())) k.close() except IOError: pass try: if not ongae: reload(sys) sys.setdefaultencoding(encoding) except (AttributeError, IOError): pass try: if not umask: checkpermissions(getdatadir(), 0700) else: checkpermissions(getdatadir(), umask) except: handle_exception() global loaded global cmndtable global pluginlist global callbacktable global cmndperms global plugcommands global timestamps global plugwhitelist global plugblacklist if not cmndtable: cmndtable = Persist(rundir + os.sep + "cmndtable") if clear: cmndtable.data = {} if not pluginlist: pluginlist = Persist(rundir + os.sep + "pluginlist") if clear: pluginlist.data = [] if not callbacktable: callbacktable = Persist(rundir + os.sep + "callbacktable") if clear: callbacktable.data = {} if not timestamps: timestamps = Persist(rundir + os.sep + "timestamps") if clear: timestamps.data = {} if not plugwhitelist: plugwhitelist = Persist(rundir + os.sep + "plugwhitelist") if not plugwhitelist.data or clear: plugwhitelist.data = [] if not plugblacklist: plugblacklist = Persist(rundir + os.sep + "plugblacklist") if not plugblacklist.data or clear: plugblacklist.data = [] if not cmndperms: cmndperms = Config("cmndperms", ddir=ddir) from jsb.lib.plugins import plugs if not cmndtable.data or force: plugs.loadall(plugin_packages, force=True) loaded = True savecmndtable(saveperms=saveperms) if not pluginlist.data or force: if not loaded: plugs.loadall(plugin_packages, force=True) loaded = True savepluginlist() if not callbacktable.data or force: if not loaded: plugs.loadall(plugin_packages, force=True) loaded = True savecallbacktable() if not loaded: logging.info("boot - plugins not loaded .. loading defaults") for plug in default_plugins: plugs.reload(plug, showerror=True, force=True) if not fast: if ongae: plugs.loadall( ["myplugs.common", "myplugs.gae", "jsb.plugs.myplugs.gae", "jsb.plugs.myplugs.common"], force=True ) else: plugs.loadall( ["myplugs.common", "myplugs.socket", "jsb.plugs.myplugs.socket", "jsb.plugs.myplugs.common"], force=True, ) else: logging.error("skipped loading of myplugs") try: changed = checktimestamps() if changed: logging.warn("boot - files changed %s" % str(changed)) for plugfile in changed: plugs.reloadfile(plugfile, force=True) except Exception, ex: logging.error("boot - can't read myplugs dir.")