Resolve groups to packages
[platform/upstream/mic.git] / mic / configmgr.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright 2011 Intel, Inc.
4 #
5 # This copyrighted material is made available to anyone wishing to use, modify,
6 # copy, or redistribute it subject to the terms and conditions of the GNU
7 # General Public License v.2.  This program is distributed in the hope that it
8 # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
9 # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # See the GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License along with
13 # this program; if not, write to the Free Software Foundation, Inc., 51
14 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat
15 # trademarks that are incorporated in the source code or documentation are not
16 # subject to the GNU General Public License and may only be used or replicated
17 # with the express permission of Red Hat, Inc.
18 #
19
20 import os, sys
21 import ConfigParser
22
23 from mic import kickstart
24 from mic import msger
25 from mic.utils import misc
26 from mic.utils import errors
27
28 DEFAULT_GSITECONF='/etc/mic/mic.conf'
29
30 DEFAULT_OUTDIR='.'
31 DEFAULT_TMPDIR='/var/tmp/mic'
32 DEFAULT_CACHEDIR='/var/tmp/mic/cache'
33
34 DEFAULT_CREATE = {
35     "tmpdir": DEFAULT_TMPDIR,
36     "cachedir": DEFAULT_CACHEDIR,
37     "outdir": DEFAULT_OUTDIR,
38     "arch": "i586",
39     "pkgmgr": "yum",
40     "name": "output",
41     "ksfile": None,
42     "ks": None,
43     "repomd": None,
44     "local_pkgs_path": None,
45     "release": None,
46 }
47
48 class ConfigMgr(object):
49     def __init__(self, ksconf=None, siteconf=None):
50         # reset config options
51         self.reset()
52
53         # initial options from siteconf
54         self._siteconf = siteconf
55         if not self.__siteconf:
56             self._siteconf = DEFAULT_GSITECONF
57
58     def reset(self):
59         self.common = {}
60         self.create = {}
61         self.convert = {}
62         self.chroot = {}
63         self.__ksconf = None
64         self.__siteconf = None
65
66         # initial create
67         for key in DEFAULT_CREATE.keys():
68             self.create[key] = DEFAULT_CREATE[key]
69
70     def __set_siteconf(self, siteconf):
71         try:
72             self.__siteconf = siteconf
73             self.parse_siteconf(siteconf)
74         except ConfigParser.Error, error:
75             raise errors.ConfigError("%s" % error)
76     def __get_siteconf(self):
77         return self.__siteconf
78     _siteconf = property(__get_siteconf, __set_siteconf)
79
80     def __set_ksconf(self, ksconf):
81         if not os.path.isfile(ksconf):
82             msger.error('Cannot find ks file: %s' % ksconf)
83
84         self.__ksconf = ksconf
85         self.parse_kickstart(ksconf)
86     def __get_ksconf(self):
87         return self.__ksconf
88     _ksconf = property(__get_ksconf, __set_ksconf)
89     def parse_siteconf(self, siteconf = None):
90         if not siteconf:
91             return
92
93         from ConfigParser import SafeConfigParser
94         siteconf_parser = SafeConfigParser()
95         if not os.path.exists(siteconf):
96             raise errors.ConfigError("Failed to find config file: %s" % siteconf)
97         siteconf_parser.read(siteconf)
98
99         for option in siteconf_parser.options('common'):
100             value = siteconf_parser.get('common', option)
101             self.common[option] = value
102
103         for option in siteconf_parser.options('create'):
104             value = siteconf_parser.get('create', option)
105             self.create[option] = value
106
107         for option in siteconf_parser.options('convert'):
108             value = siteconf_parser.get('convert', option)
109             self.convert[option] = value
110
111         for option in siteconf_parser.options('chroot'):
112             value = siteconf_parser.get('chroot', option)
113             self.chroot[option] = value
114
115     def parse_kickstart(self, ksconf=None):
116         if not ksconf:
117             return
118
119         ks = kickstart.read_kickstart(ksconf)
120
121         self.create['ks'] = ks
122         self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
123
124         msger.info("Retrieving repo metadata:")
125         ksrepos = misc.get_repostrs_from_ks(ks)
126         self.create['repomd'] = misc.get_metadata_from_repos(ksrepos, self.create['cachedir'])
127         kickstart.resolve_groups(self.create, self.create['repomd'])
128         msger.raw(" DONE")
129
130 def getConfigMgr():
131     return configmgr
132
133 configmgr = ConfigMgr()