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
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
29 from ConfigParser
import SafeConfigParser
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" ]
42 ('general', 'var_dir', '/var/lib/moblin-image-creator'),
47 # We will print out the configuration as a debugging aid
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
)
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"))
76 config
= SafeConfigParser()
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
)
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
)
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
100 if section
not in VALID_SECTIONS
:
101 print _("Invalid section: %s") % section
102 print _("Found in file: %s") % filename
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
111 if not config
.has_section(base_section
):
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
)
122 # Try to figure out if we have been invoked via sudo
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']
129 elif 'USER' in os
.environ
:
130 username
= os
.environ
['USER']
134 if 'USER' in os
.environ
:
135 username
= os
.environ
['USER']
137 username
= pwd
.getpwuid(os
.getuid()).pw_name
138 #Try to get uid and gid from the user name, which may not exist
140 pwd
.getpwnam(username
)
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
)
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
)
162 elif dist
== "fedora":
165 pkg_manager
= _("unsupported distribution")
166 config
.set('general', 'package_manager', pkg_manager
)
168 if '__main__' == __name__
: