detect site conf file, use /etc/mic/mic.conf by default
authorGui Chen <gui.chen@intel.com>
Tue, 24 Apr 2012 09:35:24 +0000 (17:35 +0800)
committerGui Chen <gui.chen@intel.com>
Sat, 28 Apr 2012 06:06:38 +0000 (14:06 +0800)
Signed-off-by: Gui Chen <gui.chen@intel.com>
mic/conf.py
setup.py

index b199483..dbfd540 100644 (file)
@@ -15,7 +15,7 @@
 # with this program; if not, write to the Free Software Foundation, Inc., 59
 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
-import os, re
+import os, sys, re
 import ConfigParser
 
 import msger
@@ -24,6 +24,17 @@ from .utils import misc, runner, proxy, errors
 
 DEFAULT_GSITECONF = '/etc/mic/mic.conf'
 
+def get_siteconf(siteconf="etc/mic/mic.conf"):
+    mic_path = os.path.dirname(__file__)
+    path_ptn = re.compile(r"(?P<prefix>.*)\/lib(64)?\/.*")
+    m = path_ptn.match(mic_path)
+    if m:
+        if m.group('prefix') == "/usr":
+            return DEFAULT_GSITECONF
+        else:
+            return os.path.join(m.group('prefix'), siteconf)
+    return None
+
 class ConfigMgr(object):
     DEFAULTS = {'common': {
                     "distro_name": "Default Distribution",
@@ -74,12 +85,13 @@ class ConfigMgr(object):
         # reset config options
         self.reset()
 
+        if not siteconf:
+            siteconf = get_siteconf()
+        if not siteconf or not os.path.exists(siteconf):
+            siteconf = DEFAULT_GSITECONF
+
         # initial options from siteconf
-        if siteconf:
-            self._siteconf = siteconf
-        else:
-            # use default site config
-            self._siteconf = DEFAULT_GSITECONF
+        self._siteconf = siteconf
 
         if ksconf:
             self._ksconf = ksconf
index 22706fd..e4dee0f 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -58,7 +58,7 @@ IMAGER_PLUGINS = glob.glob(os.path.join("plugins", "imager", "*.py"))
 BACKEND_PLUGINS = glob.glob(os.path.join("plugins", "backend", "*.py"))
 
 # the following code to do a simple parse for '--prefix' opts
-prefix = '/usr'
+prefix = sys.prefix
 is_next = False
 for arg in sys.argv:
     if is_next:
@@ -66,10 +66,13 @@ for arg in sys.argv:
         break
     if '--prefix=' in arg:
         prefix = arg[9:]
+        break
     elif '--prefix' == arg:
         is_next = True
 
-if prefix == '/usr':
+# expand the path, for user may type some unexcepted format
+prefix = os.path.abspath(os.path.expanduser(prefix)).rstrip('/')
+if prefix.lstrip('/') == 'usr':
     etc_prefix = '/etc'
 else:
     etc_prefix = os.path.join(prefix, 'etc')