[Kimchi-devel] [PATCH v3 1/3] Reorganize the kimchi's paths gereneration code
Mark Wu
wudxw at linux.vnet.ibm.com
Mon Jan 27 08:00:13 UTC 2014
This patch reorganizes kimchi's paths generation code to make the user
of path vars can work with both kimchi root app and plugin app.
Signed-off-by: Mark Wu <wudxw at linux.vnet.ibm.com>
---
src/kimchi/cachebust.py | 4 +-
src/kimchi/config.py.in | 155 +++++++++++++++++++-----------------------------
src/kimchi/root.py | 7 ++-
src/kimchi/server.py | 10 ++--
src/kimchi/template.py | 14 +++--
src/kimchi/utils.py | 6 +-
src/kimchid.in | 7 ++-
7 files changed, 89 insertions(+), 114 deletions(-)
diff --git a/src/kimchi/cachebust.py b/src/kimchi/cachebust.py
index 9a71f4f..7844695 100644
--- a/src/kimchi/cachebust.py
+++ b/src/kimchi/cachebust.py
@@ -23,11 +23,11 @@
import os
-from kimchi.config import get_prefix
+from kimchi.config import paths
def href(url):
# for error.html, url is absolute path
- f = os.path.join(get_prefix(), 'ui', url.lstrip("/"))
+ f = os.path.join(paths.ui_dir, url.lstrip("/"))
mtime = os.path.getmtime(f)
return "%s?cacheBust=%s" % (url, mtime)
diff --git a/src/kimchi/config.py.in b/src/kimchi/config.py.in
index 22fe614..f32c008 100644
--- a/src/kimchi/config.py.in
+++ b/src/kimchi/config.py.in
@@ -28,7 +28,6 @@ import platform
from ConfigParser import SafeConfigParser
-from glob import iglob
from kimchi.xmlutils import xpath_get_text
@@ -37,86 +36,24 @@ from kimchi.xmlutils import xpath_get_text
DEFAULT_LOG_LEVEL = "debug"
-def get_prefix():
- if __file__[0] == '/':
- base = os.path.dirname(__file__)
- else:
- base = os.path.dirname('./%s' % __file__)
-
- if os.access('%s/../../src/kimchi/config.py' % base, os.F_OK):
- return '%s/../..' % base
- else:
- return '@pkgdatadir@'
-
-
-def without_installation():
- return get_prefix() != '@pkgdatadir@'
-
-
-def get_config_dir():
- prefix = get_prefix()
-
- if prefix == '@pkgdatadir@':
- return '@sysconfdir@/kimchi'
- else:
- return os.path.join(prefix, 'src')
-
-
-def _get_kimchi_src_dir():
- prefix = get_prefix()
- return ('@kimchidir@' if prefix == '@pkgdatadir@'
- else os.path.join(prefix, 'src/kimchi'))
-
-
-def get_api_schema_file():
- return os.path.join(_get_kimchi_src_dir(), 'API.json')
-
-
-def get_default_log_dir():
- prefix = get_prefix()
-
- if prefix == '@pkgdatadir@':
- return "@localstatedir@/log/kimchi"
-
- return os.path.join(prefix, 'log')
-
-
-def get_state_path():
- prefix = get_prefix()
-
- if prefix == '@pkgdatadir@':
- return "@localstatedir@/lib/kimchi"
-
- return os.path.join(prefix, 'data')
-
-
def get_session_path():
- return os.path.join(get_state_path(), 'sessions')
+ return os.path.join(paths.state_dir, 'sessions')
def get_object_store():
- return os.path.join(get_prefix(), 'data', 'objectstore')
+ return os.path.join(paths.state_dir, 'objectstore')
def get_distros_store():
- return os.path.join(get_config_dir(), 'distros.d')
-
-
-def get_template_path(resource):
- return '%s/ui/pages/%s.tmpl' % (get_prefix(), resource)
+ return os.path.join(paths.conf_dir, 'distros.d')
def get_screenshot_path():
- return "%s/data/screenshots" % get_prefix()
-
-
-def get_mo_path():
- return '%s/mo' % get_prefix()
+ return os.path.join(paths.state_dir, 'screenshots')
-def get_support_language():
- mopath = "%s/*" % get_mo_path()
- return [path.rsplit('/', 1)[1] for path in iglob(mopath)]
+def get_debugreports_path():
+ return os.path.join(paths.state_dir, 'debugreports')
def find_qemu_binary():
@@ -137,37 +74,65 @@ def find_qemu_binary():
return location
-def get_debugreports_path():
- return os.path.join(get_prefix(), 'data', 'debugreports')
+class Paths(object):
+
+ def __init__(self):
+ self.prefix = self.get_prefix()
+ self.installed = (self.prefix == '@pkgdatadir@')
+
+ if self.installed:
+ self.state_dir = '@localstatedir@/lib/kimchi'
+ self.log_dir = '@localstatedir@/log/kimchi'
+ self.conf_dir = '@sysconfdir@/kimchi'
+ self.src_dir = '@kimchidir@'
+ self.plugins_dir = '@kimchidir@'
+ else:
+ self.state_dir = self.add_prefix('data')
+ self.log_dir = self.add_prefix('log')
+ self.conf_dir = self.add_prefix('src')
+ self.src_dir = self.add_prefix('src/kimchi')
+ self.plugins_dir = self.add_prefix('plugins')
+
+ self.ui_dir = self.add_prefix('ui')
+ self.mo_dir = self.add_prefix('mo')
+
+ def get_prefix(self):
+ if __file__[0] == '/':
+ base = os.path.dirname(__file__)
+ else:
+ base = os.path.dirname('./%s' % __file__)
+
+ if os.access('%s/../../src/kimchi/config.py' % base, os.F_OK):
+ return os.path.abspath('%s/../..' % base)
+ else:
+ return '@pkgdatadir@'
+
+ def add_prefix(self, subdir):
+ return os.path.join(self.prefix, subdir)
+ def get_template_path(self, resource):
+ return os.path.join(self.ui_dir, 'pages/%s.tmpl' % resource)
-def get_plugins_dir():
- prefix = get_prefix()
- if prefix == '@pkgdatadir@':
- prefix = '@kimchidir@'
- return os.path.join(prefix, 'plugins')
+paths = Paths()
-def get_plugin_config(name):
- prefix = get_prefix()
- if prefix == '@pkgdatadir@':
- base_dir = '@sysconfdir@/kimchi/plugins.d/'
- else:
- base_dir = os.path.join(prefix, 'plugins')
- return os.path.join(base_dir, '%s/%s.conf' % (name, name))
+class PluginPaths(Paths):
-def _get_plugin_ui_dir(name):
- prefix = get_prefix()
- if prefix == '@pkgdatadir@':
- base_dir = '@pkgdatadir@/plugins/'
- else:
- base_dir = os.path.join(prefix, 'plugins')
- return os.path.join(base_dir, '%s/ui' % name)
+ def __init__(self, name):
+ super(PluginPaths, self).__init__()
+ self.plugin_dir = os.path.join('plugins', name)
+ if self.installed:
+ self.conf_dir = '@sysconfdir@/kimchi/plugins.d'
+ self.src_dir = os.path.join('@kimchidir@', self.plugin_dir)
+ else:
+ self.conf_dir = self.add_prefix(self.plugin_dir)
+ self.src_dir = self.add_prefix(self.plugin_dir)
-def get_plugin_tab_xml(name):
- return os.path.join(_get_plugin_ui_dir(name), 'config/tab-ext.xml')
+ self.ui_dir = self.add_prefix(os.path.join(self.plugin_dir, 'ui'))
+ self.mo_dir = self.add_prefix(os.path.join(self.plugin_dir, 'mo'))
+ self.conf_file = os.path.join(self.conf_dir, '%s.conf' % name)
def _get_config():
@@ -180,12 +145,12 @@ def _get_config():
config.set("server", "ssl_key", "")
config.set("server", "environment", "development")
config.add_section("logging")
- config.set("logging", "log_dir", get_default_log_dir())
+ config.set("logging", "log_dir", paths.log_dir)
config.set("logging", "log_level", DEFAULT_LOG_LEVEL)
config.add_section("display")
config.set("display", "display_proxy_port", "64667")
- config_file = os.path.join(get_config_dir(), 'kimchi.conf')
+ config_file = os.path.join(paths.conf_dir, 'kimchi.conf')
if os.path.exists(config_file):
config.read(config_file)
return config
@@ -195,4 +160,4 @@ config = _get_config()
if __name__ == '__main__':
- print get_prefix()
+ print paths.prefix
diff --git a/src/kimchi/root.py b/src/kimchi/root.py
index 83a777c..6e107bc 100644
--- a/src/kimchi/root.py
+++ b/src/kimchi/root.py
@@ -23,11 +23,12 @@
import cherrypy
import json
+import os
from kimchi import auth
from kimchi import template
-from kimchi.config import get_api_schema_file
+from kimchi.config import paths
from kimchi.control import sub_nodes
from kimchi.control.base import Resource
from kimchi.control.utils import parse_request
@@ -50,8 +51,8 @@ class Root(Resource):
for ident, node in sub_nodes.items():
setattr(self, ident, node(model))
-
- self.api_schema = json.load(open(get_api_schema_file()))
+ self.api_schema = json.load(open(os.path.join(paths.src_dir,
+ 'API.json')))
def error_production_handler(self, status, message, traceback, version):
data = {'code': status, 'reason': message}
diff --git a/src/kimchi/server.py b/src/kimchi/server.py
index 2a9f53f..9884249 100644
--- a/src/kimchi/server.py
+++ b/src/kimchi/server.py
@@ -33,6 +33,7 @@ from kimchi import config
from kimchi import model
from kimchi import mockmodel
from kimchi import vnc
+from kimchi.config import paths, PluginPaths
from kimchi.control import sub_nodes
from kimchi.root import Root
from kimchi.utils import get_enabled_plugins, import_class
@@ -64,8 +65,8 @@ class Server(object):
CACHEEXPIRES = 31536000
configObj = {
'/': {'tools.trailing_slash.on': False,
- 'tools.staticdir.root': config.get_prefix(),
- 'tools.staticfile.root': config.get_prefix(),
+ 'tools.staticdir.root': paths.prefix,
+ 'tools.staticfile.root': paths.prefix,
'request.methods_with_bodies': ('POST', 'PUT'),
'tools.nocache.on': True,
'tools.sessions.on': True,
@@ -209,7 +210,8 @@ class Server(object):
plugin_config['/ui/config/tab-ext.xml'] = {
'tools.staticfile.on': True,
'tools.staticfile.filename':
- config.get_plugin_tab_xml(plugin_name),
+ os.path.join(PluginPaths(plugin_name).ui_dir,
+ 'config/tab-ext.xml'),
'tools.nocache.on': True}
except KeyError:
continue
@@ -231,7 +233,7 @@ class Server(object):
cert = options.ssl_cert
key = options.ssl_key
if not cert or not key:
- config_dir = config.get_config_dir()
+ config_dir = paths.conf_dir
cert = '%s/kimchi-cert.pem' % config_dir
key = '%s/kimchi-key.pem' % config_dir
diff --git a/src/kimchi/template.py b/src/kimchi/template.py
index f854657..37676b6 100644
--- a/src/kimchi/template.py
+++ b/src/kimchi/template.py
@@ -28,9 +28,10 @@ import os
from Cheetah.Template import Template
+from glob import iglob
-from kimchi import config
+from kimchi.config import paths
def get_lang():
@@ -54,8 +55,13 @@ def get_lang():
return langs
+def get_support_languages():
+ mopath = "%s/*" % paths.mo_dir
+ return [path.rsplit('/', 1)[1] for path in iglob(mopath)]
+
+
def validate_language(langs):
- supportLangs = config.get_support_language()
+ supportLangs = get_support_languages()
for lang in langs:
if lang in supportLangs:
return lang
@@ -86,12 +92,12 @@ def render(resource, data):
cherrypy.response.headers['Content-Type'] = 'application/json;charset=utf-8'
return json.dumps(data, indent=2, separators=(',', ':'))
elif can_accept_html():
- filename = config.get_template_path(resource)
+ filename = paths.get_template_path(resource)
try:
params = {'data': data}
lang = validate_language(get_lang())
gettext_conf = {'domain': 'kimchi',
- 'localedir': config.get_mo_path(),
+ 'localedir': paths.mo_dir,
'lang': [lang]}
params['lang'] = gettext_conf
return Template(file=filename, searchList=params).respond()
diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
index 0e66214..be32195 100644
--- a/src/kimchi/utils.py
+++ b/src/kimchi/utils.py
@@ -28,10 +28,10 @@ import urllib2
from cherrypy.lib.reprconf import Parser
+from kimchi.config import paths, PluginPaths
from kimchi.exception import TimeoutExpired
-from kimchi import config
from threading import Timer
@@ -49,7 +49,7 @@ def is_digit(value):
def _load_plugin_conf(name):
- plugin_conf = config.get_plugin_config(name)
+ plugin_conf = PluginPaths(name).conf_file
if not os.path.exists(plugin_conf):
cherrypy.log.error_log.error("Plugin configuration file %s"
" doesn't exist." % plugin_conf)
@@ -63,7 +63,7 @@ def _load_plugin_conf(name):
def get_enabled_plugins():
- plugin_dir = config.get_plugins_dir()
+ plugin_dir = paths.plugins_dir
try:
dir_contents = os.listdir(plugin_dir)
except OSError:
diff --git a/src/kimchid.in b/src/kimchid.in
index 548fa52..eebc017 100644
--- a/src/kimchid.in
+++ b/src/kimchid.in
@@ -30,12 +30,13 @@ sys.path.insert(1, '@pythondir@')
import kimchi.server
import kimchi.config
-if kimchi.config.without_installation():
- sys.path.append(kimchi.config.get_prefix())
-from kimchi.config import config
+from kimchi.config import config, paths
from optparse import OptionParser
+if not paths.installed:
+ sys.path.append(paths.prefix)
+
ACCESS_LOG = "kimchi-access.log"
ERROR_LOG = "kimchi-error.log"
--
1.8.4.2
More information about the Kimchi-devel
mailing list