diff --git a/lib/evasion/agency/agent.py b/lib/evasion/agency/agent.py index b3a2528..355b7c1 100644 --- a/lib/evasion/agency/agent.py +++ b/lib/evasion/agency/agent.py @@ -23,7 +23,8 @@ class Base(object): """Base class agent entry. """ - _log = logging.getLogger("evasion.agency.agent.Base") + def __init__(self): + self.log = logging.getLogger("evasion.agency.agent.Base") def setUp(self, config): diff --git a/lib/evasion/agency/agents/testing/fake.py b/lib/evasion/agency/agents/testing/fake.py index 26f8f30..b06c142 100644 --- a/lib/evasion/agency/agents/testing/fake.py +++ b/lib/evasion/agency/agents/testing/fake.py @@ -14,6 +14,8 @@ """ +import logging + from evasion.agency import agent @@ -34,6 +36,7 @@ class Agent(agent.Base): """ def __init__(self): + self.log = logging.getLogger("evasion.agency.agents.testing.fake.Agent") self.config = None self._parent = None diff --git a/lib/evasion/agency/docs/__init__.py b/lib/evasion/agency/docs/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/lib/evasion/agency/docs/agency.stx b/lib/evasion/agency/docs/agency.stx deleted file mode 100644 index 5dbd6c4..0000000 --- a/lib/evasion/agency/docs/agency.stx +++ /dev/null @@ -1,29 +0,0 @@ -============================= -Evasion Project Device Access -============================= - -.. contents: - - -Introduction -============ - -This module is the beginnings of the device abstract need so that device access code isn't tightly -bound into applications. - - -Drivers -======= - -This is a collection of packages used to control specific hardware and third party software -applications. - - -Installing -========== - -This module is marked as being unsafe to zip. Easy install should automatically -decompress it. If this doesn't work the you can do:: - - easy_install -Z - diff --git a/lib/evasion/agency/manager.py b/lib/evasion/agency/manager.py index c37ae2a..931b96c 100644 --- a/lib/evasion/agency/manager.py +++ b/lib/evasion/agency/manager.py @@ -171,6 +171,8 @@ def setUp(self): continue try: a.mod.setUp(a.config) + except (SystemExit, KeyboardInterrupt): + raise except: self.log.exception("%s setUp error: " % a) sys.stderr.write("%s setUp error: %s" % (a, self.formatError())) @@ -196,6 +198,8 @@ def tearDown(self): continue try: a.mod.tearDown() + except (SystemExit, KeyboardInterrupt): + raise except: self.log.exception("%s tearDown error: " % a) sys.stderr.write("%s tearDown error: %s" % (a, self.formatError())) @@ -218,6 +222,8 @@ def start(self): continue try: a.mod.start() + except (SystemExit, KeyboardInterrupt): + raise except: self.log.exception("%s start error: " % a) sys.stderr.write("%s start error: %s" % (a, self.formatError())) @@ -240,6 +246,8 @@ def stop(self): continue try: a.mod.stop() + except (SystemExit, KeyboardInterrupt): + raise except: self.log.exception("%s stop error: " % a) sys.stderr.write("%s stop error: %s" % (a, self.formatError())) diff --git a/lib/evasion/agency/scripts/__init__.py b/lib/evasion/agency/scripts/__init__.py deleted file mode 100644 index b727192..0000000 --- a/lib/evasion/agency/scripts/__init__.py +++ /dev/null @@ -1 +0,0 @@ -import manager \ No newline at end of file diff --git a/lib/evasion/agency/scripts/manager.py b/lib/evasion/agency/scripts/manager.py deleted file mode 100644 index 2f1718e..0000000 --- a/lib/evasion/agency/scripts/manager.py +++ /dev/null @@ -1,225 +0,0 @@ -# -*- coding: utf-8 -*- -""" -:mod:`agency.scripts.manager` -- This supervises then agents under its control. -================================================================================= - -.. module:: 'agency.scripts.manager' - :platform: Unix, MacOSX, Windows - :synopsis: This supervises then agents under its control. -.. moduleauthor:: Oisin Mulvihill - -This is where the main function to run the agency lives. This will generally be run and managed by -the director. - -.. autofunction:: agency.scripts.manager.appmain(isExit) -.. autofunction:: agency.scripts.manager.setup(logconfig, deviceconfig, managerconfig) -.. autofunction:: agency.scripts.manager.exit() -.. autofunction:: agency.scripts.manager.run(app=appmain) -.. autofunction:: agency.scripts.manager.create_config(cfg_data) -.. autofunction:: agency.scripts.manager.main() - - -""" -import os -import sys -import time -import pprint -import os.path -import logging -import logging.config -from configobj import ConfigObj -from optparse import OptionParser -from pydispatch import dispatcher - - -def get_log(): - return logging.getLogger("agency.manager") - - -def appmain(isExit): - """The application main loop. - - isExit: - This is a function that will return true - when its time to exit. - - """ - def event_watch(signal, sender, **data): - """Show the event we've received so far.""" - get_log().debug("Event watch - signal:%s, sender:%s" % (signal, sender)) - - # pickup all events from anybody: - dispatcher.connect( - event_watch, - signal = dispatcher.Any, - sender = dispatcher.Any, - ) - - print "Appmain running." - while not isExit(): - time.sleep(0.5) - print "Appmain exit." - - -def setup(logconfig, managerconfig): - """Setup the various pieces of information based on the given details. - - logconfig: - path and file to the log configuration. If it doesn't exit - a default stdout configuration will be used. - - managerconfig (required): - The device configuration to load, setup and start running. - - The configuration for stomp, locale, etc setup. - - """ - # Set up python logging if a config file is given: - if os.path.isfile(logconfig): - logging.config.fileConfig(logconfig) - - else: - # No log configuration file given or it has been overidden - # by the user, just print out to console instead: - log = logging.getLogger() - hdlr = logging.StreamHandler() - formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - log.addHandler(hdlr) - log.setLevel(logging.DEBUG) - logging.getLogger("agency") - - # Check the requried config files exits: - check_exits = [ - managerconfig, - ] - for f in check_exits: - if not os.path.isfile(f): - raise SystemError("The file '%s' was not found!" % f) - - # Set up manager details from its config. Stomp, etc... - import configobj - dm_cfg = configobj.ConfigObj(infile=managerconfig) - - # Set up the messenger protocols where using: - import messenger - - stomp_cfg = dict( - host = dm_cfg['Messenger'].get("host"), - port = dm_cfg['Messenger'].get("port"), - username = dm_cfg['Messenger'].get("username"), - password = dm_cfg['Messenger'].get("password"), - channel = dm_cfg['Messenger'].get("channel"), - ) - stomp_cfg['port'] = int(stomp_cfg['port']) - messenger.stompprotocol.setup(stomp_cfg) - - # Load the device/hardware configuration file, set it up - # and start the devices running ready for use: - from evasion.agency import manager - - manager.load(dm_cfg) - manager.setUp() - manager.start() - - -def exit(): - """Called by the service to stop the device manager and exit. - """ - from evasion import messenger - messenger.quit() - time.sleep(1) - - -def run(app=appmain): - """Called to run a set up manager running twisted in the main loop - and the app in its own thread. - """ - import messenger - import agency - - # Messaging system needs to run as the main loop the program will run - # inside appmain(), which is run in a different thread. - try: - messenger.run(app) - - except KeyboardInterrupt: - agency.shutdown() - exit() - - except: - agency.shutdown() - -# -DEFAULT_CONFIG_NAME = "devices.cfg" -DEFAULT_MANAGER_CONFIG_NAME = "manager.cfg" -DEFAULT_LOGCONFIG_NAME = "manager-log.cfg" - -def create_config(cfg_data): - """Create the default director.ini in the current directory based - on a the template stored in director.templatecfg - """ - import agency - import agency.templatecfg - from mako.template import Template - from pkg_resources import resource_string - - print("Creating initial configuration: '%s', '%s' and '%s'." % (DEFAULT_CONFIG_NAME, DEFAULT_MANAGER_CONFIG_NAME, DEFAULT_LOGCONFIG_NAME)) - - # Recover the template file we will fill out with the path information: - dcfg_data = resource_string(agency.templatecfg.__name__, 'devices.cfg.in') - mcfg_data = resource_string(agency.templatecfg.__name__, 'manager.cfg.in') - logcfg_data = resource_string(agency.templatecfg.__name__, 'log.cfg.in') - - # Ok, write the result out to disk: - fd = open(DEFAULT_CONFIG_NAME, 'w') - fd.write(dcfg_data) - fd.close() - - fd = open(DEFAULT_MANAGER_CONFIG_NAME, 'w') - fd.write(mcfg_data) - fd.close() - - fd = open(DEFAULT_LOGCONFIG_NAME, 'w') - fd.write(Template(logcfg_data).render(**cfg_data)) - fd.close() - - print("Success, '%s', '%s' and '%s' created ok." % (DEFAULT_CONFIG_NAME, DEFAULT_MANAGER_CONFIG_NAME, DEFAULT_LOGCONFIG_NAME)) - - -def main(): - """Main program for commandline non-service manager. - """ - parser = OptionParser() - parser.add_option("--logdir", action="store", dest="logdir", default=".", - help="Where log file goes in template log.") - parser.add_option("--logconfig", action="store", dest="logconfig_filename", default="log.cfg", - help="This is the logging config file.") - parser.add_option("--config", action="store", dest="config_filename", default="devices.cfg", - help="This the hardware configuration file.") - parser.add_option("--create", action="store_true", dest="create_config", default=False, - help="Create the default manager.cfg and device.cfg.") - (options, args) = parser.parse_args() - - if options.create_config: - create_config(dict( - log_dir=options.logdir, - logconfig=options.logconfig_filename, - config=options.config_filename, - dmconfig=options.manager_config, - )) - else: - setup( - logconfig=options.logconfig_filename, - managerconfig=options.manager_config, - ) - run(appmain) - - -if __name__ == "__main__": - main() - - - - - diff --git a/lib/evasion/agency/templatecfg/__init__.py b/lib/evasion/agency/templatecfg/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/lib/evasion/agency/templatecfg/devices.cfg.in b/lib/evasion/agency/templatecfg/devices.cfg.in deleted file mode 100644 index fd6f581..0000000 --- a/lib/evasion/agency/templatecfg/devices.cfg.in +++ /dev/null @@ -1,7 +0,0 @@ -# -# A minial example of an agent configuration. -# - -[test] -cat = service -agent = agency.agents.testing.fake diff --git a/lib/evasion/agency/templatecfg/log.cfg.in b/lib/evasion/agency/templatecfg/log.cfg.in deleted file mode 100644 index 2dcc512..0000000 --- a/lib/evasion/agency/templatecfg/log.cfg.in +++ /dev/null @@ -1,28 +0,0 @@ -[loggers] -keys=root - -[handlers] -keys=default - -[formatters] -keys=default - -[logger_root] -level=NOTSET -handlers=default -qualname=(root) -propagate=1 -channel= -parent= - -[handler_default] -class=handlers.RotatingFileHandler -args=("${log_dir}/deviceaccess.log", "au", 10 * 1024 * 1024, 2) -level=DEBUG -formatter=default - -[formatter_default] -format=%(asctime)s %(name)s %(levelname)s %(message)s -datefmt= - - diff --git a/lib/evasion/agency/templatecfg/manager.cfg.in b/lib/evasion/agency/templatecfg/manager.cfg.in deleted file mode 100644 index 4f80f6b..0000000 --- a/lib/evasion/agency/templatecfg/manager.cfg.in +++ /dev/null @@ -1,14 +0,0 @@ -# -# Device Manager Configuration -# -# Oisin Mulvihill -# 2008-12-23 -# -[Messenger] -# STOMP broker server: -host = localhost -port = 61613 -username = '' -password = '' -channel = 'evasion' - diff --git a/lib/evasion/agency/tests/testagency.py b/lib/evasion/agency/tests/testagency.py index cc7bdc6..0314585 100644 --- a/lib/evasion/agency/tests/testagency.py +++ b/lib/evasion/agency/tests/testagency.py @@ -9,31 +9,6 @@ from evasion.director.testing import director_setup -class TestDevice(object): - """Used to check the agent manager is calling the correct methods. - """ - def __init__(self): - self.setUpCalled = False - self.tearDownCalled = False - self.startCalled = False - self.stopCalled = False - self.queryCalled = False - - def setUp(self, config): - self.setUpCalled = True - - def tearDown(self): - self.tearDownCalled = True - - def start(self): - self.startCalled = True - - def stop(self): - self.stopCalled = True - - def query(self): - self.queryCalled = True - class AgencyTC(unittest.TestCase): @@ -41,186 +16,6 @@ def setUp(self): # unittesting reset: agency.node._reset() - # unittesting reset: - agency.manager.shutdown() - - - def testAgentConfigFilter(self): - """Test the extraction of devices from a config file which may contain other things. - """ - test_config = """ - [director] - messaging = no - - [My section] - this = 'should be ignored' - yes = True - - [and this too] - notanagent = 1 - - [testswipe] - # first card swipe - disable = 'no' - cat = 'swipe' - alias = 1 - agent = 'evasion.agency.agents.testing.fake' - interface = 127.0.0.1 - port = 8810 - - [magtekusbhid] - # second card swipe - disable = 'no' - cat = 'swipe' - alias = 2 - agent = 'evasion.agency.agents.testing.fake' - interface = 127.0.0.1 - port = 8810 - - [tsp700] - # first printer: load but don't use it. - disabled = 'yes' - cat = 'printer' - alias = 1 - agent = 'evasion.agency.agents.testing.fake' - interface = 127.0.0.1 - port = 8810 - - """ - director_setup(test_config) - - td1 = TestDevice() - - # These should run without problems. - agents = agency.manager.load() - self.assertEquals(len(agents), 3) - - agent1 = agency.manager.agent('swipe/1') - self.assertEquals(agent1.node, '/agent/swipe/testswipe/1') - agent1.agent.setParent(td1) - - agent1 = agency.manager.agent('/agent/swipe/1', absolute=True) - self.assertEquals(agent1.node, '/agent/swipe/testswipe/1') - agent1.agent.setParent(td1) - - # unittesting reset: - agency.node._reset() - - # unittesting reset: - agency.manager.shutdown() - - test_config = "" - agents = agency.manager.load(test_config) - self.assertEquals(len(agents), 0) - - # unittesting reset: - agency.node._reset() - - # unittesting reset: - agency.manager.shutdown() - - test_config = """ - [messenger] - host = '127.0.0.1' - """ - agents = agency.manager.load(test_config) - self.assertEquals(len(agents), 0) - - - def testmanager(self): - """Test the Manager class. - """ - self.assertEquals(agency.manager.agents, 0) - - # Make sure you can't call the following without calling load: - self.assertRaises(agency.ManagerError, agency.manager.tearDown) - self.assertRaises(agency.ManagerError, agency.manager.setUp) - self.assertRaises(agency.ManagerError, agency.manager.start) - self.assertRaises(agency.ManagerError, agency.manager.stop) - - # shutdown should be ok though: - agency.manager.shutdown() - - - td1 = TestDevice() - td2 = TestDevice() - td3 = TestDevice() - - test_config = """ - [director] - messaging = no - - [testswipe] - # first card swipe - disable = 'no' - cat = 'swipe' - alias = 1 - agent = 'evasion.agency.agents.testing.fake' - interface = 127.0.0.1 - port = 8810 - - [magtekusbhid] - # second card swipe - disable = 'no' - cat = 'swipe' - alias = 2 - agent = 'evasion.agency.agents.testing.fake' - interface = 127.0.0.1 - port = 8810 - - [tsp700] - # first printer: load but don't use it. - disabled = 'yes' - cat = 'printer' - alias = 1 - agent = 'evasion.agency.agents.testing.fake' - interface = 127.0.0.1 - port = 8810 - - """ - director_setup(test_config) - - agents = agency.manager.load() - self.assertEquals(len(agents), 3) - - agent1 = agency.manager.agent('swipe/1') - self.assertEquals(agent1.node, '/agent/swipe/testswipe/1') - agent1.agent.setParent(td1) - - agent1 = agency.manager.agent('/agent/swipe/1', absolute=True) - self.assertEquals(agent1.node, '/agent/swipe/testswipe/1') - agent1.agent.setParent(td1) - - agent2 = agency.manager.agent('swipe/2') - self.assertEquals(agent2.node, '/agent/swipe/magtekusbhid/2') - agent2.agent.setParent(td2) - - agent3 = agency.manager.agent('printer/1') - self.assertEquals(agent3.node, '/agent/printer/tsp700/1') - agent3.agent.setParent(td3) - - # Call all the methods and check that the individual - # agent methods have also been called: - agency.manager.setUp() - self.assertEquals(td1.setUpCalled, True) - self.assertEquals(td2.setUpCalled, True) - self.assertEquals(td3.setUpCalled, False) - - agency.manager.start() - self.assertEquals(td1.startCalled, True) - self.assertEquals(td2.startCalled, True) - self.assertEquals(td3.startCalled, False) - - agency.manager.stop() - self.assertEquals(td1.stopCalled, True) - self.assertEquals(td2.stopCalled, True) - self.assertEquals(td3.stopCalled, False) - - agency.manager.tearDown() - self.assertEquals(td1.tearDownCalled, True) - self.assertEquals(td2.tearDownCalled, True) - self.assertEquals(td3.tearDownCalled, False) - def testagencyNodes(self): """Test the agent node id generation. @@ -243,107 +38,4 @@ def testagencyNodes(self): self.assertEquals(alias_id, '/agent/swipe/23') - def testConfigContainer(self): - """Verify the behaviour of the test container. - """ - c = config.Container() - - # Check it catches that I haven't provided the required fields: - self.assertRaises(config.ConfigError, c.check) - - c.node = '/agent/swipe/testing/1' - c.alias = '/agent/swipe/1' - c.cat = 'swipe' - c.agent = 'agency.agents.testing.swipe' - c.name = 'testingswipe' - - # This should not now raise ConfigError. - c.check() - - - def testConfiguration(self): - """Test the configuration catches the required fields. - """ - test_config = """ - [director] - messaging = no - - [testswipe] - alias = 1 - cat = 'swipe' - agent = 'evasion.agency.agents.testing.fake' - interface = 127.0.0.1 - port = 8810 - - """ - def check(node, alias): - pass - - agents = agency.config.load(test_config, check) - agent1 = agents[0] - - self.assertEquals(agent1.alias, '/agent/swipe/1') - self.assertEquals(agent1.node, '/agent/swipe/testswipe/1') - self.assertEquals(agent1.name, 'testswipe') - - self.assertEquals(agent1.interface, '127.0.0.1') - self.assertEquals(agent1.port, '8810') - - - def testBadConfigurationCatching(self): - """Test that bad configurations are caught. - """ - test_config = """ - [director] - messaging = no - - [testswipe] - alias = 1 - cat = 'swipe12' # unknow cat - agent = 'agency.agents.testing.fake' - interface = 127.0.0.1 - port = 8810 - - """ - - self.assertRaises(agency.ConfigError, agency.config.load, test_config) - - - test_config = """ - [director] - messaging = no - - [testswipe] - alias = 1 - cat = 'swipe' - agent = 'evasion.agency.agents.testing.doesnotexits' # unknown agent module - interface = 127.0.0.1 - port = 8810 - - """ - self.assertRaises(ImportError, agency.config.load, test_config) - - # test duplicated aliases i.e. the two same cat entries have been - # given the same alias - test_config = """ - [director] - messaging = no - - [testswipe] - alias = 1 # first alias: OK - cat = 'fake' - agent = 'evasion.agency.agents.testing.swipe' - interface = 127.0.0.1 - port = 8810 - - [magtek] - alias = 1 # Duplicate alias! - cat = 'swipe' - agent = 'evasion.agency.agents.testing.fake' - - """ - - self.assertRaises(agency.ConfigError, agency.config.load, test_config) - - diff --git a/setup.py b/setup.py index 1b28370..da6173f 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ Name='evasion-agency' -ProjecUrl="http://github.com/oisinmulvihill/evasion-agency/tarball/master#egg=evasion_agency" +ProjectUrl="http://github.com/oisinmulvihill/evasion-agency/tarball/master#egg=evasion_agency" Version='1.1.4' Author='Oisin Mulvihill' AuthorEmail='oisinmulvihill at gmail dot com' @@ -26,7 +26,7 @@ ShortDescription=Summary Description=Summary -TestSuite = 'evasion.agency.tests' +TestSuite = 'nose.collector' needed = [ 'Mako', @@ -34,6 +34,14 @@ 'pydispatcher', ] +SETUP_REQUIRES = [ + 'nose>=1.0', +] + +TEST_REQUIRES = [ + 'nose>=1.0', +# 'evasion-messenger', +] # Include everything under agency. I needed to add a __init__.py # to each directory inside agency I did this using the following @@ -57,15 +65,11 @@ } # Make exe versions of the scripts: -EntryPoints = { - 'console_scripts': [ - 'manager = evasion.agency.scripts.manager:main', - ] -} +EntryPoints = {} setup( - url=ProjecUrl, + url=ProjectUrl, zip_safe=False, name=Name, version=Version, @@ -76,6 +80,8 @@ license=License, test_suite=TestSuite, scripts=ProjectScripts, + setup_requires=SETUP_REQUIRES, + tests_require=TEST_REQUIRES, install_requires=needed, packages=find_packages('lib'), package_data=PackageData,