<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 01/24/2014 09:37 AM, Aline Manera
wrote:<br>
</div>
<blockquote cite="mid:52E1C3DF.5050309@linux.vnet.ibm.com"
type="cite">On 01/22/2014 03:48 AM, Mark Wu wrote:
<br>
<blockquote type="cite">This patch reorganizes kimchi's paths
generation code to make the user
<br>
of path vars can work with both kimchi root app and plugin app.
<br>
<br>
Signed-off-by: Mark Wu <a class="moz-txt-link-rfc2396E" href="mailto:wudxw@linux.vnet.ibm.com"><wudxw@linux.vnet.ibm.com></a>
<br>
---
<br>
src/kimchi/cachebust.py | 4 +-
<br>
src/kimchi/config.py.in | 164
++++++++++++++++++++----------------------------
<br>
src/kimchi/root.py | 6 +-
<br>
src/kimchi/server.py | 10 +--
<br>
src/kimchi/template.py | 14 +++--
<br>
src/kimchi/utils.py | 6 +-
<br>
src/kimchid.in | 7 ++-
<br>
tests/test_plugin.py | 4 +-
<br>
8 files changed, 100 insertions(+), 115 deletions(-)
<br>
<br>
diff --git a/src/kimchi/cachebust.py b/src/kimchi/cachebust.py
<br>
index 9a71f4f..7844695 100644
<br>
--- a/src/kimchi/cachebust.py
<br>
+++ b/src/kimchi/cachebust.py
<br>
@@ -23,11 +23,11 @@
<br>
import os
<br>
<br>
<br>
-from kimchi.config import get_prefix
<br>
+from kimchi.config import paths
<br>
<br>
<br>
def href(url):
<br>
# for error.html, url is absolute path
<br>
- f = os.path.join(get_prefix(), 'ui', url.lstrip("/"))
<br>
+ f = os.path.join(paths.ui_dir, url.lstrip("/"))
<br>
mtime = os.path.getmtime(f)
<br>
return "%s?cacheBust=%s" % (url, mtime)
<br>
diff --git a/src/kimchi/config.py.in b/src/kimchi/config.py.in
<br>
index 22fe614..53c8e42 100644
<br>
--- a/src/kimchi/config.py.in
<br>
+++ b/src/kimchi/config.py.in
<br>
@@ -28,7 +28,6 @@ import platform
<br>
<br>
<br>
from ConfigParser import SafeConfigParser
<br>
-from glob import iglob
<br>
<br>
<br>
from kimchi.xmlutils import xpath_get_text
<br>
@@ -37,86 +36,24 @@ from kimchi.xmlutils import xpath_get_text
<br>
DEFAULT_LOG_LEVEL = "debug"
<br>
<br>
<br>
-def get_prefix():
<br>
- if __file__[0] == '/':
<br>
- base = os.path.dirname(__file__)
<br>
- else:
<br>
- base = os.path.dirname('./%s' % __file__)
<br>
-
<br>
- if os.access('%s/../../src/kimchi/config.py' % base,
os.F_OK):
<br>
- return '%s/../..' % base
<br>
- else:
<br>
- return '@pkgdatadir@'
<br>
-
<br>
-
<br>
-def without_installation():
<br>
- return get_prefix() != '@pkgdatadir@'
<br>
-
<br>
-
<br>
-def get_config_dir():
<br>
- prefix = get_prefix()
<br>
-
<br>
- if prefix == '@pkgdatadir@':
<br>
- return '@sysconfdir@/kimchi'
<br>
- else:
<br>
- return os.path.join(prefix, 'src')
<br>
-
<br>
-
<br>
-def _get_kimchi_src_dir():
<br>
- prefix = get_prefix()
<br>
- return ('@kimchidir@' if prefix == '@pkgdatadir@'
<br>
- else os.path.join(prefix, 'src/kimchi'))
<br>
-
<br>
-
<br>
-def get_api_schema_file():
<br>
- return os.path.join(_get_kimchi_src_dir(), 'API.json')
<br>
-
<br>
-
<br>
-def get_default_log_dir():
<br>
- prefix = get_prefix()
<br>
-
<br>
- if prefix == '@pkgdatadir@':
<br>
- return "@localstatedir@/log/kimchi"
<br>
-
<br>
- return os.path.join(prefix, 'log')
<br>
-
<br>
-
<br>
-def get_state_path():
<br>
- prefix = get_prefix()
<br>
-
<br>
- if prefix == '@pkgdatadir@':
<br>
- return "@localstatedir@/lib/kimchi"
<br>
-
<br>
- return os.path.join(prefix, 'data')
<br>
-
<br>
-
<br>
def get_session_path():
<br>
- return os.path.join(get_state_path(), 'sessions')
<br>
+ return os.path.join(paths.state_dir, 'sessions')
<br>
<br>
<br>
def get_object_store():
<br>
- return os.path.join(get_prefix(), 'data', 'objectstore')
<br>
+ return os.path.join(paths.state_dir, 'objectstore')
<br>
<br>
<br>
def get_distros_store():
<br>
- return os.path.join(get_config_dir(), 'distros.d')
<br>
-
<br>
-
<br>
-def get_template_path(resource):
<br>
- return '%s/ui/pages/%s.tmpl' % (get_prefix(), resource)
<br>
+ return os.path.join(paths.conf_dir, 'distros.d')
<br>
<br>
<br>
def get_screenshot_path():
<br>
- return "%s/data/screenshots" % get_prefix()
<br>
-
<br>
+ return os.path.join(paths.state_dir, 'screenshots')
<br>
<br>
-def get_mo_path():
<br>
- return '%s/mo' % get_prefix()
<br>
<br>
-
<br>
-def get_support_language():
<br>
- mopath = "%s/*" % get_mo_path()
<br>
- return [path.rsplit('/', 1)[1] for path in iglob(mopath)]
<br>
+def get_debugreports_path():
<br>
+ return os.path.join(paths.state_dir, 'debugreports')
<br>
<br>
<br>
def find_qemu_binary():
<br>
@@ -137,37 +74,74 @@ def find_qemu_binary():
<br>
return location
<br>
<br>
<br>
-def get_debugreports_path():
<br>
- return os.path.join(get_prefix(), 'data', 'debugreports')
<br>
+class Paths(object):
<br>
+
<br>
+ def __init__(self):
<br>
+ self.prefix = self.get_prefix()
<br>
+ self.installed = (self.prefix == '@pkgdatadir@')
<br>
+
<br>
+ if self.installed:
<br>
+ self.state_dir = '@localstatedir@/lib/kimchi'
<br>
+ self.log_dir = '@localstatedir@/log/kimchi'
<br>
+ self.conf_dir = '@sysconfdir@/kimchi'
<br>
+ self.src_dir = '@kimchidir@'
<br>
+ self.plugins_dir = '@kimchidir@'
<br>
+ else:
<br>
+ self.state_dir = self.add_prefix('data')
<br>
+ self.log_dir = self.add_prefix('log')
<br>
+ self.conf_dir = self.add_prefix('src')
<br>
+ self.src_dir = self.add_prefix('src/kimchi')
<br>
+ self.plugins_dir = self.add_prefix('plugins')
<br>
+
<br>
+ self.ui_dir = self.add_prefix('ui')
<br>
+ self.mo_dir = self.add_prefix('mo')
<br>
+
<br>
+ def get_prefix(self):
<br>
+ if __file__[0] == '/':
<br>
+ base = os.path.dirname(__file__)
<br>
+ else:
<br>
+ base = os.path.dirname('./%s' % __file__)
<br>
+
<br>
+ if os.access('%s/../../src/kimchi/config.py' % base,
os.F_OK):
<br>
+ return os.path.abspath('%s/../..' % base)
<br>
+ else:
<br>
+ return '@pkgdatadir@'
<br>
+
<br>
+ def add_prefix(self, subdir):
<br>
+ return os.path.join(self.prefix, subdir)
<br>
+
<br>
+ def get_template_path(self, resource):
<br>
+ return os.path.join(self.ui_dir, 'pages/%s.tmpl' %
resource)
<br>
+
<br>
+
<br>
</blockquote>
<br>
<blockquote type="cite">+paths = Paths()
<br>
+
<br>
</blockquote>
<br>
Move it to the constants section
<br>
</blockquote>
We have to put it after the definition of class Paths. If you
insist on it, we will have to move the class Path to a new module.
But it's still not clean to have paths related definition in two
files.<br>
<blockquote cite="mid:52E1C3DF.5050309@linux.vnet.ibm.com"
type="cite">
<br>
<blockquote type="cite">
<br>
+class PluginPaths(Paths):
<br>
<br>
-def get_plugins_dir():
<br>
- prefix = get_prefix()
<br>
- if prefix == '@pkgdatadir@':
<br>
- prefix = '@kimchidir@'
<br>
- return os.path.join(prefix, 'plugins')
<br>
+ def __init__(self, name):
<br>
+ super(PluginPaths, self).__init__()
<br>
+ self.plugin_dir = os.path.join('plugins', name)
<br>
<br>
+ if self.installed:
<br>
+ self.conf_dir = '@sysconfdir@/kimchi/plugins.d'
<br>
+ self.src_dir = os.path.join('@kimchidir@',
self.plugin_dir)
<br>
+ else:
<br>
+ self.conf_dir = self.add_prefix(self.plugin_dir)
<br>
+ self.src_dir = self.add_prefix(self.plugin_dir)
<br>
<br>
-def get_plugin_config(name):
<br>
- prefix = get_prefix()
<br>
- if prefix == '@pkgdatadir@':
<br>
- base_dir = '@sysconfdir@/kimchi/plugins.d/'
<br>
- else:
<br>
- base_dir = os.path.join(prefix, 'plugins')
<br>
- return os.path.join(base_dir, '%s/%s.conf' % (name, name))
<br>
+ self.ui_dir =
self.add_prefix(os.path.join(self.plugin_dir, 'ui'))
<br>
+ self.mo_dir =
self.add_prefix(os.path.join(self.plugin_dir, 'mo'))
<br>
+ self.conf_file = os.path.join(self.conf_dir, '%s.conf'
% name)
<br>
<br>
<br>
-def _get_plugin_ui_dir(name):
<br>
- prefix = get_prefix()
<br>
- if prefix == '@pkgdatadir@':
<br>
- base_dir = '@pkgdatadir@/plugins/'
<br>
- else:
<br>
- base_dir = os.path.join(prefix, 'plugins')
<br>
- return os.path.join(base_dir, '%s/ui' % name)
<br>
</blockquote>
<br>
<blockquote type="cite">+_plugin_paths = {}
<br>
</blockquote>
<br>
Why is it needed?
<br>
</blockquote>
It was kind of cache to avoid
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<h1 class="keyword" style="margin: 0px; padding: 0px 20px 0px 0px;
float: left; line-height: 1.25em; font-size: 30px; color: rgb(0,
153, 68); font-family: Verdana, Geneva, sans-serif; word-wrap:
break-word; font-style: normal; font-variant: normal;
letter-spacing: normal; orphans: auto; text-align: start;
text-indent: 0px; text-transform: none; white-space: normal;
widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);"><br>
</h1>
instantiation many times. but it just have a few, so remove it in
v2.<br class="Apple-interchange-newline">
<br>
<blockquote cite="mid:52E1C3DF.5050309@linux.vnet.ibm.com"
type="cite">
<br>
<blockquote type="cite">
<br>
<br>
-def get_plugin_tab_xml(name):
<br>
- return os.path.join(_get_plugin_ui_dir(name),
'config/tab-ext.xml')
<br>
+def plugin_paths(plugin_name):
<br>
+ if plugin_name not in _plugin_paths:
<br>
+ _plugin_paths[plugin_name] = PluginPaths(plugin_name)
<br>
+ return _plugin_paths[plugin_name]
<br>
<br>
<br>
def _get_config():
<br>
@@ -180,12 +154,12 @@ def _get_config():
<br>
config.set("server", "ssl_key", "")
<br>
config.set("server", "environment", "development")
<br>
config.add_section("logging")
<br>
- config.set("logging", "log_dir", get_default_log_dir())
<br>
+ config.set("logging", "log_dir", paths.log_dir)
<br>
config.set("logging", "log_level", DEFAULT_LOG_LEVEL)
<br>
config.add_section("display")
<br>
config.set("display", "display_proxy_port", "64667")
<br>
<br>
- config_file = os.path.join(get_config_dir(), 'kimchi.conf')
<br>
+ config_file = os.path.join(paths.conf_dir, 'kimchi.conf')
<br>
if os.path.exists(config_file):
<br>
config.read(config_file)
<br>
return config
<br>
@@ -195,4 +169,4 @@ config = _get_config()
<br>
<br>
<br>
if __name__ == '__main__':
<br>
- print get_prefix()
<br>
+ print paths.prefix
<br>
diff --git a/src/kimchi/root.py b/src/kimchi/root.py
<br>
index 3cc6321..ae9fd0e 100644
<br>
--- a/src/kimchi/root.py
<br>
+++ b/src/kimchi/root.py
<br>
@@ -23,11 +23,12 @@
<br>
<br>
import cherrypy
<br>
import json
<br>
+import os
<br>
<br>
<br>
from kimchi import auth
<br>
from kimchi import template
<br>
-from kimchi.config import get_api_schema_file
<br>
+from kimchi.config import paths
<br>
from kimchi.control.base import Resource
<br>
from kimchi.control.config import Config
<br>
from kimchi.control.debugreports import DebugReports
<br>
@@ -67,7 +68,8 @@ class Root(Resource):
<br>
self.host = Host(model)
<br>
self.debugreports = DebugReports(model)
<br>
self.plugins = Plugins(model)
<br>
- self.api_schema =
json.load(open(get_api_schema_file()))
<br>
+ self.api_schema =
json.load(open(os.path.join(paths.src_dir,
<br>
+
'API.json')))
<br>
<br>
def error_production_handler(self, status, message,
traceback, version):
<br>
data = {'code': status, 'reason': message}
<br>
diff --git a/src/kimchi/server.py b/src/kimchi/server.py
<br>
index b820263..58b082b 100644
<br>
--- a/src/kimchi/server.py
<br>
+++ b/src/kimchi/server.py
<br>
@@ -33,6 +33,7 @@ from kimchi import config
<br>
from kimchi import model
<br>
from kimchi import mockmodel
<br>
from kimchi import vnc
<br>
+from kimchi.config import paths, plugin_paths
<br>
from kimchi.root import Root
<br>
from kimchi.utils import get_enabled_plugins, import_class
<br>
<br>
@@ -63,8 +64,8 @@ class Server(object):
<br>
CACHEEXPIRES = 31536000
<br>
configObj = {
<br>
'/': {'tools.trailing_slash.on': False,
<br>
- 'tools.staticdir.root': config.get_prefix(),
<br>
- 'tools.staticfile.root': config.get_prefix(),
<br>
+ 'tools.staticdir.root': paths.prefix,
<br>
+ 'tools.staticfile.root': paths.prefix,
<br>
'request.methods_with_bodies': ('POST', 'PUT'),
<br>
'tools.nocache.on': True,
<br>
'tools.sessions.on': True,
<br>
@@ -211,7 +212,8 @@ class Server(object):
<br>
plugin_config['/ui/config/tab-ext.xml'] = {
<br>
'tools.staticfile.on': True,
<br>
'tools.staticfile.filename':
<br>
- config.get_plugin_tab_xml(plugin_name),
<br>
+
os.path.join(plugin_paths(plugin_name).ui_dir,
<br>
+ 'config/tab-ext.xml'),
<br>
'tools.nocache.on': True}
<br>
except KeyError:
<br>
continue
<br>
@@ -233,7 +235,7 @@ class Server(object):
<br>
cert = options.ssl_cert
<br>
key = options.ssl_key
<br>
if not cert or not key:
<br>
- config_dir = config.get_config_dir()
<br>
+ config_dir = paths.conf_dir
<br>
cert = '%s/kimchi-cert.pem' % config_dir
<br>
key = '%s/kimchi-key.pem' % config_dir
<br>
<br>
diff --git a/src/kimchi/template.py b/src/kimchi/template.py
<br>
index 1f19c4a..1740997 100644
<br>
--- a/src/kimchi/template.py
<br>
+++ b/src/kimchi/template.py
<br>
@@ -28,9 +28,10 @@ import os
<br>
<br>
<br>
from Cheetah.Template import Template
<br>
+from glob import iglob
<br>
<br>
<br>
-from kimchi import config
<br>
+from kimchi.config import paths
<br>
<br>
<br>
def get_lang():
<br>
@@ -54,8 +55,13 @@ def get_lang():
<br>
return langs
<br>
<br>
<br>
+def get_support_languages():
<br>
+ mopath = "%s/*" % paths.mo_dir
<br>
+ return [path.rsplit('/', 1)[1] for path in iglob(mopath)]
<br>
+
<br>
+
<br>
def validate_language(langs):
<br>
- supportLangs = config.get_support_language()
<br>
+ supportLangs = get_support_languages()
<br>
for lang in langs:
<br>
if lang in supportLangs:
<br>
return lang
<br>
@@ -88,12 +94,12 @@ def render(resource, data):
<br>
separators=(',', ':'),
<br>
encoding='iso-8859-1')
<br>
elif can_accept_html():
<br>
- filename = config.get_template_path(resource)
<br>
+ filename = paths.get_template_path(resource)
<br>
try:
<br>
params = {'data': data}
<br>
lang = validate_language(get_lang())
<br>
gettext_conf = {'domain': 'kimchi',
<br>
- 'localedir': config.get_mo_path(),
<br>
+ 'localedir': paths.mo_dir,
<br>
'lang': [lang]}
<br>
params['lang'] = gettext_conf
<br>
return Template(file=filename,
searchList=params).respond()
<br>
diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
<br>
index 5d6e8ea..7077b3b 100644
<br>
--- a/src/kimchi/utils.py
<br>
+++ b/src/kimchi/utils.py
<br>
@@ -28,10 +28,10 @@ import urllib2
<br>
<br>
<br>
from cherrypy.lib.reprconf import Parser
<br>
+from kimchi.config import paths, plugin_paths
<br>
from kimchi.exception import TimeoutExpired
<br>
<br>
<br>
-from kimchi import config
<br>
from threading import Timer
<br>
<br>
<br>
@@ -48,7 +48,7 @@ def is_digit(value):
<br>
<br>
<br>
def _load_plugin_conf(name):
<br>
- plugin_conf = config.get_plugin_config(name)
<br>
+ plugin_conf = plugin_paths(name).conf_file
<br>
if not os.path.exists(plugin_conf):
<br>
cherrypy.log.error_log.error("Plugin configuration
file %s"
<br>
" doesn't exist." %
plugin_conf)
<br>
@@ -62,7 +62,7 @@ def _load_plugin_conf(name):
<br>
<br>
<br>
def get_enabled_plugins():
<br>
- plugin_dir = config.get_plugins_dir()
<br>
+ plugin_dir = paths.plugins_dir
<br>
try:
<br>
dir_contents = os.listdir(plugin_dir)
<br>
except OSError:
<br>
diff --git a/src/kimchid.in b/src/kimchid.in
<br>
index 548fa52..eebc017 100644
<br>
--- a/src/kimchid.in
<br>
+++ b/src/kimchid.in
<br>
@@ -30,12 +30,13 @@ sys.path.insert(1, '@pythondir@')
<br>
<br>
import kimchi.server
<br>
import kimchi.config
<br>
-if kimchi.config.without_installation():
<br>
- sys.path.append(kimchi.config.get_prefix())
<br>
<br>
-from kimchi.config import config
<br>
+from kimchi.config import config, paths
<br>
from optparse import OptionParser
<br>
<br>
+if not paths.installed:
<br>
+ sys.path.append(paths.prefix)
<br>
+
<br>
ACCESS_LOG = "kimchi-access.log"
<br>
ERROR_LOG = "kimchi-error.log"
<br>
<br>
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
<br>
index 42c87a9..e5c9053 100644
<br>
--- a/tests/test_plugin.py
<br>
+++ b/tests/test_plugin.py
<br>
@@ -32,7 +32,7 @@ from functools import partial
<br>
import kimchi.mockmodel
<br>
import kimchi.server
<br>
import utils
<br>
-from kimchi import config
<br>
+from kimchi.config import paths
<br>
<br>
<br>
test_server = None
<br>
@@ -48,7 +48,7 @@ def setUpModule():
<br>
host = '127.0.0.1'
<br>
port = utils.get_free_port('http')
<br>
ssl_port = None
<br>
- sys.path.append(config.get_prefix())
<br>
+ sys.path.append(paths.prefix)
<br>
test_server = utils.run_server(host, port, ssl_port,
test_mode=True,
<br>
model=model)
<br>
<br>
</blockquote>
<br>
</blockquote>
<br>
</body>
</html>