远程代码的执行和更新
# rcode.py
# -*- coding: utf-8 -*-
import logging
log = logging.getLogger(__name__)
class Code(object):
def __init__(self, had_changed=False, obj=None):
self.obj = obj
self.had_changed = had_changed
class CodeManager(object):
def __init__(self, redis=None, prefix="__code__"):
self.redis_client = redis
self.prefix = prefix
self.__code_obj = {}
def update(self, name, had_changed=True):
if name in self.__code_obj:
self.__code_obj[name].had_changed = had_changed
def package(self, name):
return self.__package(name)
def remove(self, name):
if '#' in name:
try:
del self.__code_obj[name]
except Exception, e:
log.error(e.message)
else:
for key in self.__code_obj.keys():
if key.startswith(name):
try:
del self.__code_obj[key]
except Exception, e:
log.error(e.message)
def __package(self, name):
c = self.__code_obj.get(name)
if c and not c.had_changed:
return c.obj
p, n = name.split("#")
__s = self.redis_client.conn().hget("{}.{}".format(self.prefix, p), n)
if not __s:
log.error(u"{} 不存在".format(name))
return
_obj = self.__e(name.split(".").pop(), __s)
if not _obj:
return
self.__code_obj[name] = Code(obj=_obj)
return _obj
def __e(self, name, source):
try:
exec source
__c = eval(name)
setattr(__c, "package", self.__package)
return __c
except Exception, e:
log.error(e.message)
作者:百里求一
出处:http://www.cnblogs.com/bergus/
我的语雀: https://www.yuque.com/barry.bai
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。