summaryrefslogtreecommitdiffstats
path: root/scripts/lib/mic/conf.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/mic/conf.py')
-rw-r--r--scripts/lib/mic/conf.py197
1 files changed, 197 insertions, 0 deletions
diff --git a/scripts/lib/mic/conf.py b/scripts/lib/mic/conf.py
new file mode 100644
index 0000000000..b850d80520
--- /dev/null
+++ b/scripts/lib/mic/conf.py
@@ -0,0 +1,197 @@
1#!/usr/bin/python -tt
2#
3# Copyright (c) 2011 Intel, Inc.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the Free
7# Software Foundation; version 2 of the License
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12# for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc., 59
16# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18import os, sys, re
19import ConfigParser
20
21from mic import msger
22from mic import kickstart
23from mic.utils import misc, runner, proxy, errors
24
25
26def get_siteconf():
27 mic_path = os.path.dirname(__file__)
28 eos = mic_path.find('scripts') + len('scripts')
29 scripts_path = mic_path[:eos]
30
31 return scripts_path + "/lib/image/config/wic.conf"
32
33class ConfigMgr(object):
34 prefer_backends = ["zypp", "yum"]
35
36 DEFAULTS = {'common': {
37 "distro_name": "Default Distribution",
38 "plugin_dir": "/usr/lib/wic/plugins", # TODO use prefix also?
39 },
40 'create': {
41 "tmpdir": '/var/tmp/wic',
42 "cachedir": '/var/tmp/wic/cache',
43 "outdir": './wic-output',
44
45 "arch": None, # None means auto-detect
46 "pkgmgr": "auto",
47 "name": "output",
48 "ksfile": None,
49 "ks": None,
50 "repomd": None,
51 "local_pkgs_path": None,
52 "release": None,
53 "logfile": None,
54 "record_pkgs": [],
55 "pack_to": None,
56 "name_prefix": None,
57 "name_suffix": None,
58 "proxy": None,
59 "no_proxy": None,
60 "copy_kernel": False,
61 "install_pkgs": None,
62 "repourl": {},
63 "localrepos": [], # save localrepos
64 "runtime": "bootstrap",
65 },
66 'chroot': {
67 "saveto": None,
68 },
69 'convert': {
70 "shell": False,
71 },
72 'bootstrap': {
73 "rootdir": '/var/tmp/wic-bootstrap',
74 "packages": [],
75 },
76 }
77
78 # make the manager class as singleton
79 _instance = None
80 def __new__(cls, *args, **kwargs):
81 if not cls._instance:
82 cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
83
84 return cls._instance
85
86 def __init__(self, ksconf=None, siteconf=None):
87 # reset config options
88 self.reset()
89
90 if not siteconf:
91 siteconf = get_siteconf()
92
93 # initial options from siteconf
94 self._siteconf = siteconf
95
96 if ksconf:
97 self._ksconf = ksconf
98
99 def reset(self):
100 self.__ksconf = None
101 self.__siteconf = None
102
103 # initialize the values with defaults
104 for sec, vals in self.DEFAULTS.iteritems():
105 setattr(self, sec, vals)
106
107 def __set_siteconf(self, siteconf):
108 try:
109 self.__siteconf = siteconf
110 self._parse_siteconf(siteconf)
111 except ConfigParser.Error, error:
112 raise errors.ConfigError("%s" % error)
113 def __get_siteconf(self):
114 return self.__siteconf
115 _siteconf = property(__get_siteconf, __set_siteconf)
116
117 def __set_ksconf(self, ksconf):
118 if not os.path.isfile(ksconf):
119 msger.error('Cannot find ks file: %s' % ksconf)
120
121 self.__ksconf = ksconf
122 self._parse_kickstart(ksconf)
123 def __get_ksconf(self):
124 return self.__ksconf
125 _ksconf = property(__get_ksconf, __set_ksconf)
126
127 def _parse_siteconf(self, siteconf):
128 if not siteconf:
129 return
130
131 if not os.path.exists(siteconf):
132 msger.warning("cannot read config file: %s" % siteconf)
133 return
134
135 parser = ConfigParser.SafeConfigParser()
136 parser.read(siteconf)
137
138 for section in parser.sections():
139 if section in self.DEFAULTS:
140 getattr(self, section).update(dict(parser.items(section)))
141
142 # append common section items to other sections
143 for section in self.DEFAULTS.keys():
144 if section != "common":
145 getattr(self, section).update(self.common)
146
147 # check and normalize the scheme of proxy url
148 if self.create['proxy']:
149 m = re.match('^(\w+)://.*', self.create['proxy'])
150 if m:
151 scheme = m.group(1)
152 if scheme not in ('http', 'https', 'ftp', 'socks'):
153 msger.error("%s: proxy scheme is incorrect" % siteconf)
154 else:
155 msger.warning("%s: proxy url w/o scheme, use http as default"
156 % siteconf)
157 self.create['proxy'] = "http://" + self.create['proxy']
158
159 proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
160
161 # bootstrap option handling
162 self.set_runtime(self.create['runtime'])
163 if isinstance(self.bootstrap['packages'], basestring):
164 packages = self.bootstrap['packages'].replace('\n', ' ')
165 if packages.find(',') != -1:
166 packages = packages.split(',')
167 else:
168 packages = packages.split()
169 self.bootstrap['packages'] = packages
170
171 def _parse_kickstart(self, ksconf=None):
172 if not ksconf:
173 return
174
175 ksconf = misc.normalize_ksfile(ksconf,
176 self.create['release'],
177 self.create['arch'])
178
179 ks = kickstart.read_kickstart(ksconf)
180
181 self.create['ks'] = ks
182 self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
183
184 self.create['name'] = misc.build_name(ksconf,
185 self.create['release'],
186 self.create['name_prefix'],
187 self.create['name_suffix'])
188
189 def set_runtime(self, runtime):
190 if runtime not in ("bootstrap", "native"):
191 msger.error("Invalid runtime mode: %s" % runtime)
192
193 if misc.get_distro()[0] in ("tizen", "Tizen"):
194 runtime = "native"
195 self.create['runtime'] = runtime
196
197configmgr = ConfigMgr()