--- imagefactory/ApplicationConfiguration.py | 23 ++++++------------- imagefactory/Singleton.py | 35 ++++++++++++++++++++++++++++++ imagefactory/qmfagent/ImageFactory.py | 21 ++++++------------ imgfac.py | 34 +++++++++++------------------ 4 files changed, 62 insertions(+), 51 deletions(-) create mode 100644 imagefactory/Singleton.py
diff --git a/imagefactory/ApplicationConfiguration.py b/imagefactory/ApplicationConfiguration.py index 9de2b78..3b121b8 100644 --- a/imagefactory/ApplicationConfiguration.py +++ b/imagefactory/ApplicationConfiguration.py @@ -24,27 +24,18 @@ import argparse import json import logging import props +from Singleton import Singleton
-class ApplicationConfiguration(object): - instance = None - +class ApplicationConfiguration(Singleton): configuration = props.prop("_configuration", "The configuration property.")
- def __new__(cls, *p, **k): - if cls.instance is None: - i = super(ApplicationConfiguration, cls).__new__(cls, *p, **k) - #initialize here, not in __init__() - i.log = logging.getLogger('%s.%s' % (__name__, i.__class__.__name__)) - - i.configuration = i.__parse_arguments() - - cls.instance = i - elif(len(p) | len(k) > 0): - cls.instance.log.warn('Attempted re-initialize of singleton: %s' % (cls.instance, )) - return cls.instance + def _singleton_init(self): + super(ApplicationConfiguration, self)._singleton_init() + self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__)) + self.configuration = self.__parse_arguments()
def __init__(self): - pass + pass
def __new_argument_parser(self): main_description = """Image Factory is an application for creating system images to run virtual machines in various public and private \ diff --git a/imagefactory/Singleton.py b/imagefactory/Singleton.py new file mode 100644 index 0000000..161da6f --- /dev/null +++ b/imagefactory/Singleton.py @@ -0,0 +1,35 @@ +# Copyright (C) 2010-2011 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. A copy of the GNU General Public License is +# also available at http://www.gnu.org/copyleft/gpl.html. + +class Singleton(object): + _instance = None + + def __new__(cls, *args, **kwargs): + if cls._instance is None: + instance = super(Singleton, cls).__new__(cls, *args, **kwargs) + instance._singleton_init() + cls._instance = instance + elif args or kwargs: + cls._instance.log.warn('Attempted re-initialize of singleton: %s' % (cls._instance, )) + return cls._instance + + def __init__(self): + pass + + def _singleton_init(self): + """Initialize a singleton instance before it is registered.""" + pass diff --git a/imagefactory/qmfagent/ImageFactory.py b/imagefactory/qmfagent/ImageFactory.py index a0c2bcc..c5e64fa 100644 --- a/imagefactory/qmfagent/ImageFactory.py +++ b/imagefactory/qmfagent/ImageFactory.py @@ -24,13 +24,13 @@ from BuildAdaptor import BuildAdaptor from imagefactory import props from imagefactory.ApplicationConfiguration import ApplicationConfiguration from imagefactory.ImageWarehouse import ImageWarehouse +from imagefactory.Singleton import Singleton from imagefactory.Template import Template import logging
# Singleton representing the Factory itself
-class ImageFactory(object): - instance = None +class ImageFactory(Singleton):
# QMF schema for ImageFactory qmf_schema = Schema(SCHEMA_TYPE_DATA, "com.redhat.imagefactory", "ImageFactory") @@ -77,18 +77,11 @@ class ImageFactory(object): qmf_object = props.prop("_qmf_object", "The qmf_object property.") agent = props.prop("_agent", "The property agent")
- def __new__(cls, *p, **k): - if cls.instance is None: - i = super(ImageFactory, cls).__new__(cls, *p, **k) - # initialize here, not in __init__() - i.log = logging.getLogger('%s.%s' % (__name__, i.__class__.__name__)) - i.qmf_object = Data(ImageFactory.qmf_schema) - #i.agent = k.get('agent', p[0] if (len(p) > 0) else None) - i.warehouse = ImageWarehouse(ApplicationConfiguration().configuration["warehouse"]) - cls.instance = i - elif(len(p) | len(k) > 0): - cls.instance.log.warn('Attempted re-initialize of singleton: %s' % (cls.instance, )) - return cls.instance + def _singleton_init(self): + super(ImageFactory, self)._singleton_init() + self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__)) + self.qmf_object = Data(ImageFactory.qmf_schema) + self.warehouse = ImageWarehouse(ApplicationConfiguration().configuration["warehouse"])
def __init__(self): pass diff --git a/imgfac.py b/imgfac.py index 30e1fea..8b1ba11 100755 --- a/imgfac.py +++ b/imgfac.py @@ -27,9 +27,9 @@ from imagefactory.ApplicationConfiguration import ApplicationConfiguration from imagefactory.qmfagent.ImageFactoryAgent import * from imagefactory.BuildDispatcher import BuildDispatcher from imagefactory.ImageWarehouse import ImageWarehouse +from imagefactory.Singleton import Singleton
-class Application(object): - instance = None +class Application(Singleton):
def qmf_agent(): doc = "The qmf_agent property." @@ -42,28 +42,20 @@ class Application(object): return locals() qmf_agent = property(**qmf_agent())
- def __new__(cls, *p, **k): - if cls.instance is None: - i = super(Application, cls).__new__(cls, *p, **k) - # initialize here, not in __init__() - i.log = logging.getLogger('%s.%s' % (__name__, i.__class__.__name__)) - i.daemon = False - i.pid_file_path = "/var/run/imagefactory.pid" - signal.signal(signal.SIGTERM, i.signal_handler) - i.app_config = ApplicationConfiguration().configuration - - # by setting TMPDIR here we make sure that libguestfs - # (imagefactory -> oz -> libguestfs) uses the temporary directory of - # the user's choosing - os.putenv('TMPDIR', i.app_config['tmpdir']) - - cls.instance = i - elif(len(p) | len(k) > 0): - cls.instance.log.warn('Attempted re-initialize of singleton: %s' % (cls.instance, )) - return cls.instance + def _singleton_init(self): + super(Application, self)._singleton_init() + self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__)) + self.daemon = False + self.pid_file_path = "/var/run/imagefactory.pid" + signal.signal(signal.SIGTERM, self.signal_handler) + self.app_config = ApplicationConfiguration().configuration + + # by setting TMPDIR here we make sure that libguestfs + # (imagefactory -> oz -> libguestfs) uses the temporary directory of + # the user's choosing + os.putenv('TMPDIR', self.app_config['tmpdir'])
def __init__(self): - # logging.basicConfig(level=logging.NOTSET, format='%(asctime)s %(levelname)s %(name)s pid(%(process)d) Message: %(message)s') pass
def setup_logging(self):