commit bd2c285a1d15dffbd42f8702200fd9d550726491 Author: bjschuma Date: Thu Dec 17 22:30:27 2009 -0500 Took newer version of ocarina2 and renamed to scion diff --git a/src/base/__init__.py b/src/base/__init__.py new file mode 100644 index 00000000..1c5f26fb --- /dev/null +++ b/src/base/__init__.py @@ -0,0 +1,2 @@ + +#__all__ = ["bt"] \ No newline at end of file diff --git a/src/base/bt/__init__.py b/src/base/bt/__init__.py new file mode 100644 index 00000000..fbe1ed4c --- /dev/null +++ b/src/base/bt/__init__.py @@ -0,0 +1,4 @@ +# This is the base tools package +# It contains various tools needed by the base layer of ocarina2 + +__all__ = ["proc", "message"] \ No newline at end of file diff --git a/src/base/bt/__init__.pyc b/src/base/bt/__init__.pyc new file mode 100644 index 00000000..e0e0ee6e Binary files /dev/null and b/src/base/bt/__init__.pyc differ diff --git a/src/base/bt/file.py b/src/base/bt/file.py new file mode 100644 index 00000000..8ea7c3fe --- /dev/null +++ b/src/base/bt/file.py @@ -0,0 +1,44 @@ +import os.path +__author__="bjschuma" +__date__ ="$Dec 6, 2009 11:35:56 PM$" + +import os +from message import write + + +# Check if path is a directory +def checkDir(path): + path = os.path.expanduser(path) + if checkPath(path) == True: + return os.path.isdir(path) + return False + + +# Check if path exists +def checkPath(path): + path = os.path.expanduser(path) + write("Checking if "+path+" exists", True) + return os.path.exists(path) + + +def expandPath(path): + return os.path.expanduser(path) + + +# Open a file +def fopen(path,flags='r'): + path = os.path.expanduser(path) + # If we are reading a file, check that it exists + if ('r' in flags) == True: + exists = checkPath(path) + if exists == False: + write(path+" does not exist", True) + return None + return open(path, flags) + + +# Return a listing of directory contents +def ls(path): + if checkDir(path) == False: + return [] + return os.listdir(path) diff --git a/src/base/bt/file.pyc b/src/base/bt/file.pyc new file mode 100644 index 00000000..47ead998 Binary files /dev/null and b/src/base/bt/file.pyc differ diff --git a/src/base/bt/message.py b/src/base/bt/message.py new file mode 100644 index 00000000..5f5577be --- /dev/null +++ b/src/base/bt/message.py @@ -0,0 +1,22 @@ + + +__author__="bjschuma" +__date__ ="$Dec 5, 2009 6:46:13 PM$" + +import os +import inspect +import settings + + +# Print an error message +def error(text): + lineno = str(inspect.currentframe().f_back.f_lineno) + filename = inspect.currentframe().f_back.f_code.co_filename + filename = filename.rsplit(os.sep,1)[1] + print filename,"("+lineno+"):",text + +# Print general text to the screen +def write(text,verbose=False): + if (verbose==False) or (settings.get("verbose")==True): + print text + diff --git a/src/base/bt/message.pyc b/src/base/bt/message.pyc new file mode 100644 index 00000000..a7e43fdc Binary files /dev/null and b/src/base/bt/message.pyc differ diff --git a/src/base/bt/proc.py b/src/base/bt/proc.py new file mode 100644 index 00000000..b423f024 --- /dev/null +++ b/src/base/bt/proc.py @@ -0,0 +1,30 @@ +# Manages process-related tasks + +__author__="bjschuma" +__date__ ="$Dec 5, 2009 6:33:50 PM$" + +import settings +from message import write + +# Set our process name to name +def setname(name): + # Set the process name (thank you exaile.py) + # This only works on linux2 machines + message = "Attempting to set process name to ocarina2..." + if settings.settings["ARCH"] == 'linux2': + try: + import ctypes + libc = ctypes.CDLL('libc.so.6') + libc.prctl(15,name, 0, 0, 0) + message += "success!" + except: + try: + import dl + libc = dl.open('/lib/libc.so.6') + name+="\0" + libc.call('prctl',15, name, 0, 0, 0) # 15 is PR_SET_NAME + message += "success!" + except: + message += "failed." + + write(message,True) \ No newline at end of file diff --git a/src/base/bt/proc.pyc b/src/base/bt/proc.pyc new file mode 100644 index 00000000..ead5cf0d Binary files /dev/null and b/src/base/bt/proc.pyc differ diff --git a/src/base/loader.py b/src/base/loader.py new file mode 100644 index 00000000..3e7a7604 --- /dev/null +++ b/src/base/loader.py @@ -0,0 +1,54 @@ +# This class is used to load plugins + +import sys +from bt.message import write +from bt.file import * + + +class PluginLoader: + def __init__(self): + # Plugins are added to this array upon loading + self.plugins = [] + + + def clearPlugins(self): + self.plugins = [] + + + # Load plugins from a directory + def loaddir(self, dir): + exists = checkDir(dir) + # Exit now if the directory doesn't exist + if exists == False: + return + write("Loading plugins from " + dir, True) + + # Add the directory to our import path + sys.path.append(dir) + modlist = ls(dir) + for mod in modlist: + split = mod.rsplit('.',1) + # Check for things we should not import + if split[0]=="__init__": + continue + elif split[1]=="pyc": + continue + # Load the module into our module array + write("Attempting to load "+mod, True) + self.loadmod(split[0]) + + + # Call with a module name to import + # Adds the imported module to a list + def loadmod(self,mod): + __import__(mod) + plugin = sys.modules[mod] + plugin.__name__ = mod + self.plugins += [plugin] + + + # Load specific plugin + def loadpath(self, path): + write("Loading " + path) + + diff --git a/src/base/loader.pyc b/src/base/loader.pyc new file mode 100644 index 00000000..e38036b7 Binary files /dev/null and b/src/base/loader.pyc differ diff --git a/src/base/manager.py b/src/base/manager.py new file mode 100644 index 00000000..110145c8 --- /dev/null +++ b/src/base/manager.py @@ -0,0 +1,90 @@ + + +__author__="bjschuma" +__date__ ="$Dec 8, 2009 8:40:36 AM$" + +import sys +from bt.message import write + +class Manager: + def __init__(self): + write("Creating plugin manager", True) + self.enabled = [] + # Map plugin name to dictionary + self.disabled = [] + + + def addPlugins(self, plugins): + # Add each plugin to the disabled list + for plugin in plugins: + write("Adding plugin: " + str(plugin), True) + self.disabled += [plugin] + # If we are adding a core plugin, activate it right away + if(plugin.__type__=="core"): + self.enablePlugin(plugin.__name__) + + + # Move plugin from old[index] to new + # Return the plugin + def movePlugin(self, index, old, new): + plugin = old.pop(index) + index = len(new) + new += [plugin] + return plugin,index + + + # Disable a plugin, return index of disabled plugin + def disablePlugin(self,name): + index = self.findPlugin(name, self.enabled) + if index > -1: + write("Disabling plugin: "+name) + plugin,index = self.movePlugin(index, self.enabled, self.disabled) + plugin.close() + return index + + + # Move a plugin to the enabled list, return number of plugins enabled + def enablePlugin(self,name): + index = self.findPlugin(name, self.disabled) + if index > -1: + write("Enabling plugin: "+name,True) + plugin,index = self.movePlugin(index, self.disabled, self.enabled) + plugin.open() + return True + + + # Return first location where plugin.__name__ == name + def findPlugin(self,name,array): + write("Finding plugin: "+name,True) + for i in range(len(array)): + if array[i].__name__ == name: + return i + return -1 + + + # Disable the plugin, reload it, reenable it + def reloadPlugin(self,name): + # Check if plugin has been loaded + if (name in sys.modules) == False: + write("Plugin not loaded: "+name, True) + return + reenable = self.disablePlugin(name) + + #import sys + #write(sys.modules["example"]) + #return + #reenable = self.disablePlugin(name) + #index = self.findPlugin(name, self.disabled) + #write("Reloading plugin: "+name) + #plugin = self.disabled[index] + #print plugin + #reload(plugin) + # Only reenable if a plugin was disabled + #if reenable == True: + # self.enablePlugin(name) + + + + +global manager +manager = Manager() \ No newline at end of file diff --git a/src/base/manager.pyc b/src/base/manager.pyc new file mode 100644 index 00000000..5f128a65 Binary files /dev/null and b/src/base/manager.pyc differ diff --git a/src/base/ocarina.py b/src/base/ocarina.py new file mode 100644 index 00000000..97ecdc5a --- /dev/null +++ b/src/base/ocarina.py @@ -0,0 +1,44 @@ +# To change this template, choose Tools | Templates +# and open the template in the editor. + +__author__="bjschuma" +__date__ ="$Dec 4, 2009 3:37:21 PM$" + +global name +name = "ocarina2" + +# We need to import settings before we can use disp() +import settings + +# The first thing we do is import write() so we can occasionally print messages +from bt.message import write +write("Welcome to Ocarina2") + +# Next, we set the process name to ocarina2 +from bt.proc import setname +setname(name) + +# Import the plugin loader class! +import loader +import manager + + +def loadPluginPath(path): + # Load a plugin directory, add give to the plugin manager + load = loader.PluginLoader() + load.loaddir("../core") + manager.manager.addPlugins(load.plugins) + + +# Begin our main loop +def main(): + load = loader.PluginLoader() + for path in settings.get("PLUGPATH"): + loadPluginPath(path) + + raw_input("Input something:") + manager.manager.reloadPlugin("example") + + + +if __name__ == "__main__":main() diff --git a/src/base/settings.py b/src/base/settings.py new file mode 100644 index 00000000..716697cc --- /dev/null +++ b/src/base/settings.py @@ -0,0 +1,65 @@ +import os.path +__author__="bjschuma" +__date__ ="$Dec 4, 2009 4:04:24 PM$" + +from bt.message import write +from bt.message import error +from bt.file import fopen +#import bt.message.disp + +import os +import sys +import getopt + + +# Create a dictionary to hold the settings +global settings +settings = dict() + +# Return the value at key +def get(key): + return settings[key.upper()] + + +# Read settings from file +def readfile(file): + write("Reading file: "+file,True) + file = fopen(file) + + +# Parse the user input +def parseInput(): + write("Parsing user input",True) + input = sys.argv[1:] + write(input,True) + optc = "v" + long = ["verbose"] + + # Attempt to parse user input + try: + opts, args = getopt.getopt(input, optc, long) + except getopt.GetoptError, e: + error(e.msg) + return + + + + + +# Set default values +# Set verbose first so we can use write() +settings["VERBOSE"] = ('-v' in sys.argv) or ("--verbose" in sys.argv) +write("Setting default values...", True) +# Find who is running the program +user = os.path.expanduser("~") +user = os.path.join(user,".ocarina2") +settings["USER"] = user +settings["PLUGPATH"] = ["../core"] + +# Find out what platform we are running on +settings["ARCH"] = sys.platform + +if settings["ARCH"] == 'linux2': + readfile("/etc/ocarina.conf") + +parseInput() \ No newline at end of file diff --git a/src/base/settings.pyc b/src/base/settings.pyc new file mode 100644 index 00000000..1de073f1 Binary files /dev/null and b/src/base/settings.pyc differ diff --git a/src/core/example.py b/src/core/example.py new file mode 100644 index 00000000..cef74541 --- /dev/null +++ b/src/core/example.py @@ -0,0 +1,24 @@ +# This is a simple test plugin, to make sure everything is working + +__author__="bjschuma" +__date__ ="$Dec 7, 2009 9:12:00 AM$" + +__name__="test" +__type__="core" +__opt__=[] + +from bt.message import write + + +# Called every time the plugin is enabled +def open(): + write("Example plugin has been started",True) + + +def close(): + write("Example plugin has been stopped") + pass + + +def run(): + pass diff --git a/src/core/example.pyc b/src/core/example.pyc new file mode 100644 index 00000000..4d6ebcfc Binary files /dev/null and b/src/core/example.pyc differ diff --git a/src/ocarina b/src/ocarina new file mode 100644 index 00000000..538c2c48 --- /dev/null +++ b/src/ocarina @@ -0,0 +1,4 @@ +#!/bin/bash + +# This is a simple shell script for properly starting ocarina +cd base && `which python` ocarina.py $* \ No newline at end of file