Patch for Bug #227013 - /boot not mounted
[moblin-image-creator.eeepc.git] / libs / mic_cfg.py
blobe146e6e14f23589bac832719d5b8885ba105ee6a
1 #!/usr/bin/python -tt
2 # vim: ai ts=4 sts=4 et sw=4
4 # Copyright (c) 2007 Intel Corporation
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation; version 2 of the License
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc., 59
17 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 # This file contains utility functions which do not need to be inside any of
20 # our current classes
22 import gettext
23 import os
24 import platform
25 import pwd
26 import re
27 import sys
29 from ConfigParser import SafeConfigParser
31 _ = gettext.lgettext
33 config = None
34 DEFAULT_CONFIG_DIR = os.path.expanduser("/usr/share/pdk/default_config/")
35 CONFIG_DIR = os.path.expanduser("~/.image-creator")
36 # List of valid sections for our config file
37 BASE_SECTIONS = [ "platform", "installimage", "distribution" ]
38 VALID_SECTIONS = BASE_SECTIONS + [ "general" ]
40 # Default values
41 DEFAULTS = [
42 ('general', 'var_dir', '/var/lib/moblin-image-creator'),
46 def main():
47 # We will print out the configuration as a debugging aid
48 readConfig()
49 for section in sorted(config.sections()):
50 print "[%s]" % section
51 for option in config.options(section):
52 value = config.get(section, option)
53 print "%s=%s" % (option, value)
54 print
55 return
57 def configDir():
58 return CONFIG_DIR
60 def readConfig():
61 if not os.path.isdir(CONFIG_DIR):
62 print _("~/.image-creator/ directory did not exist. Creating")
63 os.makedirs(CONFIG_DIR)
65 user_config_file = os.path.join(CONFIG_DIR, "image-creator.cfg")
66 # FIXME: This is temporary to help out people
67 old_config = os.path.join(CONFIG_DIR, "platforms.cfg")
68 if os.path.exists(old_config):
69 print _("Error: The file: %s exists") % old_config
70 print _("This file is no longer used. Please convert it over to the new file and then delete it")
71 print _("Please create a: %s file") % user_config_file
72 print _("And set it up like the file: %s" % os.path.join(DEFAULT_CONFIG_DIR, "defaults.cfg"))
73 sys.exit(1)
75 global config
76 config = SafeConfigParser()
77 addDefaults()
78 for filename in sorted(os.listdir(DEFAULT_CONFIG_DIR)):
79 full_name = os.path.join(DEFAULT_CONFIG_DIR, filename)
80 config.read(full_name)
81 verifyConfig(full_name)
82 user_config_file = os.path.join(CONFIG_DIR, "image-creator.cfg")
83 if os.path.isfile(user_config_file):
84 config.read(user_config_file)
85 verifyConfig(user_config_file)
86 fixupConfig()
87 addUserInfo()
89 def verifyConfig(filename):
90 """Make sure that we don't have any unknown sections in the config file"""
91 for section in config.sections():
92 result = re.search(r'^(.*)\.', section)
93 if result:
94 section = result.group(1)
95 if section not in BASE_SECTIONS:
96 print _("Invalid section: %s") % section
97 print _("Found in file: %s") % filename
98 sys.exit(1)
99 continue
100 if section not in VALID_SECTIONS:
101 print _("Invalid section: %s") % section
102 print _("Found in file: %s") % filename
103 sys.exit(1)
105 def fixupConfig():
106 """This takes care of fixing up the config data. Main thing it does right
107 now is fix up stuff for 'platform.platformname' sections"""
108 for base_section in BASE_SECTIONS:
109 # If we don't have the base section, then we know we will not copy
110 # anything over
111 if not config.has_section(base_section):
112 continue
113 for custom_section in config.sections():
114 # See if we have a custom section
115 if re.search(base_section + r'\.', custom_section):
116 for option in config.options(base_section):
117 if not config.has_option(custom_section, option):
118 value = config.get(base_section, option)
119 config.set(custom_section, option, value)
121 def addUserInfo():
122 # Try to figure out if we have been invoked via sudo
123 sudo = 0
124 if os.getuid() == 0:
125 # our user ID is root, so we may have been invoked via sudo
126 if 'SUDO_USER' in os.environ:
127 username = os.environ['SUDO_USER']
128 sudo = 1
129 elif 'USER' in os.environ:
130 username = os.environ['USER']
131 else:
132 username = "root"
133 else:
134 if 'USER' in os.environ:
135 username = os.environ['USER']
136 else:
137 username = pwd.getpwuid(os.getuid()).pw_name
138 #Try to get uid and gid from the user name, which may not exist
139 try:
140 pwd.getpwnam(username)
141 except KeyError:
142 username = 'root'
144 userid = pwd.getpwnam(username).pw_uid
145 groupid = pwd.getpwnam(username).pw_gid
146 config.add_section('userinfo')
147 config.set('userinfo', 'groupid', "%s" % groupid)
148 config.set('userinfo', 'user', username)
149 config.set('userinfo', 'userid', "%s" % userid)
150 config.set('userinfo', 'sudo', "%s" % sudo)
152 def addDefaults():
153 for section, option, value in DEFAULTS:
154 if not config.has_section(section):
155 config.add_section(section)
156 config.set(section, option, value)
157 # What distribution are we running on
158 dist = platform.dist()[0]
159 config.set('general', 'distribution', dist)
160 if dist == "debian":
161 pkg_manager = "apt"
162 elif dist == "fedora":
163 pkg_manager = "yum"
164 else:
165 pkg_manager = _("unsupported distribution")
166 config.set('general', 'package_manager', pkg_manager)
168 if '__main__' == __name__:
169 sys.exit(main())
170 else:
171 readConfig()