예제 #1
0
class BaseEngine(object):
    version = "0.0.--.0.0"
    description = "Unimplemented description field."
    __metaclass__ = RegisterEngine
    __depends__ = {}
    __exposes__ = {}

    def __init__(self, *args, **kw):
        kwargs = kw.get('kwargs', {})
        
        if "app_path" not in kwargs or kwargs["app_path"] is None:
            kwargs["app_path"] = os.path.join(GlobalSettings.installPath, self.__class__.__name__)    

        if(os.path.exists(kwargs["app_path"])): self.installed = True
        else: self.installed = False

        self.app_path = kwargs["app_path"]
        del kwargs["app_path"]

        for key in kwargs.keys(): self.__setattr__(key, kwargs[key])
        
        self.fileHelper = FileHelperObject(self.__class__.__name__)

    def resolveDepends(self):
        __newDepends = dict()
        for pack_name in self.__depends__.keys():
            package = EngineRegistrar.getPackage(pack_name)
            if(isinstance(self.__depends__[pack_name], dict)):
                pack = package(copy.deepcopy(self.__depends__[pack_name]))
                if not pack.installed: pack.install()
                __newDepends[pack_name] = pack.getResolutions(copy.deepcopy(self.__depends__[pack_name]))
                for key in __newDepends[pack_name].keys():
                    ret_val = pack.requestVariable(key)
                    if ret_val is not None: __newDepends[pack_name][key] = ret_val

            elif(isinstance(self.__depends__[pack_name], list)):
                __newDepends[pack_name] = list()
                for item in self.__depends__[pack_name]:
                    pack = package(copy.deepcopy(item))
                    if not pack.installed: pack.install()
                    __newDepends[pack_name].append(pack.getResolutions(copy.deepcopy(item)))
                    for key in __newDepends[pack_name][-1].keys():
                        ret_val = pack.requestVariable(key)
                        if ret_val is not None: __newDepends[pack_name][-1][key] = ret_val

        self.__depends__ = __newDepends

    def requestVariable(self, string): return None
    def getResolutions(self, requirements_as_dict):
        for key in requirements_as_dict.keys():
            requirements_as_dict[key] = self.__getattribute__(key)
        return requirements_as_dict


    def start(self): raise NotImplemented
    def default_start(self, func_callback):
        if(not self.installed):
            if(not self.install()): return False
        return func_callback()


    def stop(self): raise NotImplemented
    def default_stop(self, func_callback):
        if(not self.installed): return True
        return func_callback()


    def install(self, force = False): raise NotImplemented

    def default_install(self, func_callback, force = False):
        self.resolveDepends()
        if(force):
            if(not try_delete_path(self.app_path)): raise FilePermissionException
            self.installed = False
        if not os.path.exists(self.app_path):
            os.makedirs(self.app_path)
            self.installed = func_callback()
        return self.installed

    def uninstall(self): raise NotImplemented
    def default_uninstall(self, func_callback = None):
        if(func_callback != None):
            return func_callback()

        if(self.installed == True):
            if(not try_delete_path(self.app_path)):
                raise UninstallingItemDNEException
            self.installed = False

    def restart(self):
        raise NotImplemented

    def default_restart(self, func_callback = None):
        if(not self.installed):
            if(not self.install()): return False
        if(func_callback == None):
            stop_status = self.stop()
            start_status = self.start()
            return (stop_status and start_status)
        return func_callback()
    
    def download(self, *args, **kw):
        return self.fileHelper.download(*args, **kw)
    
    def unTar(self, *args, **kw):
        return self.fileHelper.unTar(*args, **kw)
    
    def runConfigAndMake(self, directory, flags={}):
        
        configPath = os.path.abspath(os.path.join(directory, 'configure'))
        assert os.path.exists(configPath)
        command('chmod u+x %s' % configPath)
        flagString = ''
        for flag, arg in flags.items():
            flagString += flag
            if arg:
                flagString += "=%s" % arg
            flagString += " "
        command('cd %s && bash %s -q %s && make && make install' % (directory, configPath, flagString))
    
    
    def cleanUp(self):
        return self.fileHelper.cleanUp()
예제 #2
0
class BaseEngine(object):
    version = "0.0.--.0.0"
    description = "Unimplemented description field."
    __metaclass__ = RegisterEngine
    depends = []
    optionalArgs = {"app_path" : None}

    def __init__(self, *args, **kw):
        if "settings" in kw: 
            self.settings = kw["settings"]
        else: self.settings = GlobalSettings()
        
        # Resolve the App Path Issue
        if not (self.__class__.__name__ in self.settings.packages):
            self.settings.packages[self.__class__.__name__] = dict()
        
        if not "app_path" in self.settings.packages[self.__class__.__name__]:
            self.settings.packages[self.__class__.__name__]["app_path"] = self.settings.default_install_path
        
        self.app_path = self.settings.packages[self.__class__.__name__]["app_path"] + "/" + self.__class__.__name__
        self.settings.packages[self.__class__.__name__]["app_path"] = self.app_path
        print self.app_path

        # Given the app path, let's check if it is installed or not
        if(os.path.exists(self.app_path)): self.installed = True
        else: self.installed = False
        
        self.fileHelper = FileHelperObject(self.__class__.__name__)

    def get_dependencies(self):        
        if(self.depends == []): return [self.__class__.__name__]
        
        new_depends = []
        for depend_name in self.depends:
             eng = EngineRegistrar.getPackage(depend_name)
             if eng is None:
                eng = generateDynamicClass(depend_name)
                if eng is None:
                    print "Could Not Identify Dependency %s as Package or Factory" % basePackage
                    return
                    
             new_depends = merge_depends(new_depends, eng.depends)
        return new_depends

    def default_start(self, func_callback):
        if(not self.installed):
            if(not self.install()): return False
        return func_callback()

    def stop(self): raise NotImplemented
    def default_stop(self, func_callback):
        if(not self.installed): return True
        return func_callback()

    def resolveDepends(self):
        for pack_name in self.depends:
            print pack_name
            package = EngineRegistrar.getPackage(pack_name)
            if(package is None):
                package = generateDynamicClass(pack_name)
                if(package is None):
                    print "Could Not Identify Dependency %s as Package or Factory" % basePackage
                    return

            pack_class = package(settings = self.settings)
            pack_class.install()
            self.settings = pack_class.settings


    def install(self, force = False): raise NotImplemented
    def default_install(self, func_callback, force = False):
        self.resolveDepends()
        if(force):
            if(not try_delete_path(self.app_path)): raise FilePermissionException
            self.installed = False
        if not (self.app_path is None):
            if not os.path.exists(self.app_path):
                os.makedirs(self.app_path)
                self.installed = func_callback()


    def uninstall(self): raise NotImplemented
    def default_uninstall(self, func_callback = None):
        if(func_callback != None):
            return func_callback()

        if(self.installed == True):
            if(not try_delete_path(self.app_path)):
                raise UninstallingItemDNEException
            self.installed = False


    def restart(self): raise NotImplemented
    def default_restart(self, func_callback = None):
        if(not self.installed):
            if(not self.install()): return False
        if(func_callback == None):
            stop_status = self.stop()
            start_status = self.start()
            return (stop_status and start_status)
        return func_callback()
    
    def download(self, *args, **kw):
        return self.fileHelper.download(*args, **kw)
    
    def unTar(self, *args, **kw):
        return self.fileHelper.unTar(*args, **kw)
    
    def runConfigAndMake(self, directory, flags={}):
        configPath = os.path.abspath(os.path.join(directory, 'configure'))
        assert os.path.exists(configPath)
        command('chmod u+x %s' % configPath)
        flagString = ''
        for flag, arg in flags.items():
            flagString += flag
            if arg:
                flagString += "=%s" % arg
            flagString += " "
        command('cd %s && bash %s -q %s && make && make install' % (directory, configPath, flagString))
    
    
    def cleanUp(self):
        return self.fileHelper.cleanUp()