Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env 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 | |
| 18 | import os |
| 19 | |
| 20 | from wic import msger |
| 21 | from wic import kickstart |
| 22 | from wic.utils import misc |
| 23 | |
| 24 | |
| 25 | def get_siteconf(): |
| 26 | wic_path = os.path.dirname(__file__) |
| 27 | eos = wic_path.find('scripts') + len('scripts') |
| 28 | scripts_path = wic_path[:eos] |
| 29 | |
| 30 | return scripts_path + "/lib/image/config/wic.conf" |
| 31 | |
| 32 | class ConfigMgr(object): |
| 33 | DEFAULTS = { |
| 34 | 'common': { |
| 35 | "distro_name": "Default Distribution", |
| 36 | "plugin_dir": "/usr/lib/wic/plugins"}, # TODO use prefix also? |
| 37 | 'create': { |
| 38 | "tmpdir": '/var/tmp/wic', |
| 39 | "outdir": './wic-output', |
| 40 | "release": None, |
| 41 | "logfile": None, |
| 42 | "name_prefix": None, |
| 43 | "name_suffix": None} |
| 44 | } |
| 45 | |
| 46 | # make the manager class as singleton |
| 47 | _instance = None |
| 48 | def __new__(cls, *args, **kwargs): |
| 49 | if not cls._instance: |
| 50 | cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs) |
| 51 | |
| 52 | return cls._instance |
| 53 | |
| 54 | def __init__(self, ksconf=None, siteconf=None): |
| 55 | # reset config options |
| 56 | self.reset() |
| 57 | |
| 58 | if not siteconf: |
| 59 | siteconf = get_siteconf() |
| 60 | |
| 61 | # initial options from siteconf |
| 62 | self._siteconf = siteconf |
| 63 | |
| 64 | if ksconf: |
| 65 | self._ksconf = ksconf |
| 66 | |
| 67 | def reset(self): |
| 68 | self.__ksconf = None |
| 69 | self.__siteconf = None |
| 70 | self.create = {} |
| 71 | |
| 72 | # initialize the values with defaults |
| 73 | for sec, vals in self.DEFAULTS.iteritems(): |
| 74 | setattr(self, sec, vals) |
| 75 | |
| 76 | def __set_ksconf(self, ksconf): |
| 77 | if not os.path.isfile(ksconf): |
| 78 | msger.error('Cannot find ks file: %s' % ksconf) |
| 79 | |
| 80 | self.__ksconf = ksconf |
| 81 | self._parse_kickstart(ksconf) |
| 82 | def __get_ksconf(self): |
| 83 | return self.__ksconf |
| 84 | _ksconf = property(__get_ksconf, __set_ksconf) |
| 85 | |
| 86 | def _parse_kickstart(self, ksconf=None): |
| 87 | if not ksconf: |
| 88 | return |
| 89 | |
| 90 | ksobj = kickstart.read_kickstart(ksconf) |
| 91 | |
| 92 | self.create['ks'] = ksobj |
| 93 | self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0] |
| 94 | |
| 95 | self.create['name'] = misc.build_name(ksconf, |
| 96 | self.create['release'], |
| 97 | self.create['name_prefix'], |
| 98 | self.create['name_suffix']) |
| 99 | |
| 100 | configmgr = ConfigMgr() |