47
47
class createDebootstrap:
49
def __init__ (self, config):
49
def __init__(self, config):
50
50
''' This should create the base dir if it not exist '''
51
51
self._config = config
52
52
self._project_dir = self._config.get_key("project_dir")
54
54
print "Base dir: ", self._project_dir
55
55
if os.path.exists(self._project_dir):
56
if not os.path.isdir (self._project_dir):
56
if not os.path.isdir(self._project_dir):
57
57
print "Error, %s is not a directory" % self._project_dir
61
os.mkdir (self._project_dir)
62
os.mkdir (os.path.join (self._project_dir, 'source'))
63
os.mkdir (os.path.join (self._project_dir, 'live'))
61
os.mkdir(self._project_dir)
62
os.mkdir(os.path.join(self._project_dir, 'source'))
63
os.mkdir(os.path.join(self._project_dir, 'live'))
69
69
''' Just create the debootstrap '''
70
70
#TODO: Add some cheks
71
retcode = os.system ("%s --arch %s %s %s %s" % (
71
retcode = os.system("%s --arch %s %s %s %s" % (
73
73
self._config.get_key("arch"),
74
74
self._config.get_key("base_distribution"),
75
os.path.join (self._project_dir, 'source'),
75
os.path.join(self._project_dir, 'source'),
76
76
self._config.get_key("repository_remote_base")
80
80
print "Error creating debootstrap, return code: %d" % retcode
83
83
# HACK: This is a ugly hack for avoiding the debconf ask to configure the kernel package
84
kernelConfig = open (os.path.join (self._project_dir, 'source', 'etc', 'kernel-img.conf'), "w")
85
kernelConfig.write (KERNEL_PACKAGE_DIRTY_HACK)
84
kernelConfig = open(os.path.join(self._project_dir, 'source', 'etc', 'kernel-img.conf'), "w")
85
kernelConfig.write(KERNEL_PACKAGE_DIRTY_HACK)
89
for repo in self._config.get_key ('build_repositories'):
89
for repo in self._config.get_key('build_repositories'):
90
90
sources_list += "%s\n" % repo
91
apt = open (os.path.join (self._project_dir, 'source', 'etc', 'apt', 'sources.list'), "w")
92
apt.write (sources_list)
91
apt = open(os.path.join(self._project_dir, 'source', 'etc', 'apt', 'sources.list'), "w")
92
apt.write(sources_list)
99
99
''' Just delete the debootstrap '''
101
shutil.rmtree (os.path.join (self._project_dir, 'source'))
101
shutil.rmtree(os.path.join(self._project_dir, 'source'))
102
102
except OSError, e:
103
103
if e.errno != errno.NOENT:
107
def _prepare_chroot (self):
107
def _prepare_chroot(self):
108
108
print "copying dns config"
109
shutil.copyfile ("/etc/resolv.conf", os.path.join (self._project_dir, 'source', 'etc', 'resolv.conf'))
110
shutil.copyfile ("/etc/hosts", os.path.join (self._project_dir, 'source', 'etc', 'hosts'))
109
shutil.copyfile("/etc/resolv.conf", os.path.join(self._project_dir, 'source', 'etc', 'resolv.conf'))
110
shutil.copyfile("/etc/hosts", os.path.join(self._project_dir, 'source', 'etc', 'hosts'))
112
112
print "mounting /proc"
113
if os.system ("/bin/mount -t proc none %s" % os.path.join (self._project_dir, 'source', 'proc')) != 0:
114
print "problem mounting proc on %s" % os.path.join (self._project_dir, 'source', 'proc')
113
if os.system("/bin/mount -t proc none %s" % os.path.join(self._project_dir, 'source', 'proc')) != 0:
114
print "problem mounting proc on %s" % os.path.join(self._project_dir, 'source', 'proc')
116
116
print "mounting /dev"
117
if os.system ("/bin/mount -t dev -o bind /dev %s" % os.path.join (self._project_dir, 'source', 'dev')) != 0:
118
print "problem mounting dev on %s" % os.path.join (self._project_dir, 'source', 'dev')
117
if os.system("/bin/mount -t dev -o bind /dev %s" % os.path.join(self._project_dir, 'source', 'dev')) != 0:
118
print "problem mounting dev on %s" % os.path.join(self._project_dir, 'source', 'dev')
120
120
print "mounting /dev/pts"
121
if os.system ("/bin/mount -t devpts none %s" % os.path.join (self._project_dir, 'source', 'dev/pts')) != 0:
122
print "problem mounting devpts on %s" % os.path.join (self._project_dir, 'source', 'dev/pts')
121
if os.system("/bin/mount -t devpts none %s" % os.path.join(self._project_dir, 'source', 'dev/pts')) != 0:
122
print "problem mounting devpts on %s" % os.path.join(self._project_dir, 'source', 'dev/pts')
124
124
print "mounting /sys"
125
if os.system ("/bin/mount -t sysfs none %s" % os.path.join (self._project_dir, 'source', 'sys')) != 0:
126
print "problem mounting sysfs on %s" % os.path.join (self._project_dir, 'source', 'sys')
129
def _close_chroot (self):
130
basedir = os.path.join (self._project_dir, 'source').replace ("/", "\/")
125
if os.system("/bin/mount -t sysfs none %s" % os.path.join(self._project_dir, 'source', 'sys')) != 0:
126
print "problem mounting sysfs on %s" % os.path.join(self._project_dir, 'source', 'sys')
129
def _close_chroot(self):
130
basedir = os.path.join(self._project_dir, 'source').replace("/", "\/")
131
131
reText = "(.*) (" + basedir + ".*/volatile) (.*)"
132
132
volatiles = re.compile(reText)
133
133
for line in open("/proc/mounts"):
135
path = volatiles.search(line).groups ()[1]
135
path = volatiles.search(line).groups()[1]
138
138
print "umounting %s" % path
139
if os.system ("/bin/umount %s" % path ) != 0:
139
if os.system("/bin/umount %s" % path ) != 0:
140
140
print "problem Umounting %s" % path
142
142
print "umounting /sys"
143
if os.system ("/bin/umount -l -f %s" % os.path.join (self._project_dir, 'source', 'sys')) != 0:
144
print "problem Umounting %s" % os.path.join (self._project_dir, 'source', 'sys')
143
if os.system("/bin/umount -l -f %s" % os.path.join(self._project_dir, 'source', 'sys')) != 0:
144
print "problem Umounting %s" % os.path.join(self._project_dir, 'source', 'sys')
146
146
print "umounting /dev/pts"
147
if os.system ("/bin/umount %s" % os.path.join (self._project_dir, 'source', 'dev/pts')) != 0:
148
print "problem Umounting %s" % os.path.join (self._project_dir, 'source', 'dev/pts')
147
if os.system("/bin/umount %s" % os.path.join(self._project_dir, 'source', 'dev/pts')) != 0:
148
print "problem Umounting %s" % os.path.join(self._project_dir, 'source', 'dev/pts')
150
150
print "umounting /dev"
151
if os.system ("/bin/umount -l -f %s" % os.path.join (self._project_dir, 'source', 'dev')) != 0:
152
print "problem Umounting %s" % os.path.join (self._project_dir, 'source', 'dev')
151
if os.system("/bin/umount -l -f %s" % os.path.join(self._project_dir, 'source', 'dev')) != 0:
152
print "problem Umounting %s" % os.path.join(self._project_dir, 'source', 'dev')
154
154
print "umounting /proc"
155
if os.system ("/bin/umount -l -f %s" % os.path.join (self._project_dir, 'source', 'proc')) != 0:
156
print "problem Umounting %s" % os.path.join (self._project_dir, 'source', 'proc')
155
if os.system("/bin/umount -l -f %s" % os.path.join(self._project_dir, 'source', 'proc')) != 0:
156
print "problem Umounting %s" % os.path.join(self._project_dir, 'source', 'proc')
158
158
print "cleanning dns config"
159
os.unlink (os.path.join (self._project_dir, 'source', 'etc', 'resolv.conf'))
160
os.unlink (os.path.join (self._project_dir, 'source', 'etc', 'hosts'))
163
def edit_by_hand (self):
159
os.unlink(os.path.join(self._project_dir, 'source', 'etc', 'resolv.conf'))
160
os.unlink(os.path.join(self._project_dir, 'source', 'etc', 'hosts'))
163
def edit_by_hand(self):
164
164
''' This should not be used '''
165
self._prepare_chroot ()
165
self._prepare_chroot()
166
166
print "Entering in chroot"
167
os.system ("/usr/sbin/chroot %s" % os.path.join (self._project_dir, 'source'))
168
self._close_chroot ()
167
os.system("/usr/sbin/chroot %s" % os.path.join(self._project_dir, 'source'))
170
def _install_fake_utils (self):
170
def _install_fake_utils(self):
171
171
print "Changing start-stop-daemon"
172
shutil.move (os.path.join(self._project_dir,'source/sbin/start-stop-daemon'), os.path.join(self._project_dir, 'source/sbin/start-stop-daemon.REAL'))
173
start_stop_daemon = open (os.path.join(self._project_dir, 'source/sbin/start-stop-daemon'), "w")
174
start_stop_daemon.write (FAKE_START_STOP_DAEMON)
175
start_stop_daemon.close ()
176
os.chmod (os.path.join(self._project_dir, 'source/sbin/start-stop-daemon'), 0755)
172
shutil.move(os.path.join(self._project_dir,'source/sbin/start-stop-daemon'), os.path.join(self._project_dir, 'source/sbin/start-stop-daemon.REAL'))
173
start_stop_daemon = open(os.path.join(self._project_dir, 'source/sbin/start-stop-daemon'), "w")
174
start_stop_daemon.write(FAKE_START_STOP_DAEMON)
175
start_stop_daemon.close()
176
os.chmod(os.path.join(self._project_dir, 'source/sbin/start-stop-daemon'), 0755)
178
178
print "Changing invoke-rc.d"
179
shutil.move (os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d'), os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d.REAL'))
180
start_stop_daemon = open (os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d'), "w")
181
start_stop_daemon.write (FAKE_INVOKE_RC)
182
start_stop_daemon.close ()
183
os.chmod (os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d'), 0755)
179
shutil.move(os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d'), os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d.REAL'))
180
start_stop_daemon = open(os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d'), "w")
181
start_stop_daemon.write(FAKE_INVOKE_RC)
182
start_stop_daemon.close()
183
os.chmod(os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d'), 0755)
185
def _remove_fake_utils (self):
185
def _remove_fake_utils(self):
186
186
print "Restore start-stop-daemon"
187
shutil.move (os.path.join(self._project_dir, 'source/sbin/start-stop-daemon.REAL'), os.path.join(self._project_dir, 'source/sbin/start-stop-daemon'))
187
shutil.move(os.path.join(self._project_dir, 'source/sbin/start-stop-daemon.REAL'), os.path.join(self._project_dir, 'source/sbin/start-stop-daemon'))
188
188
print "Restore invoke-rc.d"
189
shutil.move (os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d.REAL'), os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d'))
192
def install_packages (self, packages):
189
shutil.move(os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d.REAL'), os.path.join(self._project_dir, 'source/usr/sbin/invoke-rc.d'))
192
def install_packages(self, packages):
193
193
''' This method install the packages list into chroot '''
194
self._prepare_chroot ()
195
self._install_fake_utils ()
194
self._prepare_chroot()
195
self._install_fake_utils()
197
197
os.putenv('LC_ALL', 'C')
198
198
os.putenv('LANGUAGE', 'C')
199
199
os.putenv('DEBIAN_FRONTEND', 'noninteractive')
200
if type (packages) != str:
201
packages = ' '.join (packages)
200
if type(packages) != str:
201
packages = ' '.join(packages)
202
202
print 'Installing packages: %s' % packages
203
os.system ("/usr/sbin/chroot %s bash -c 'apt-get update;apt-get -f -y --force-yes install %s'" % (os.path.join (self._project_dir, 'source'), packages))
204
self._close_chroot ()
205
self._remove_fake_utils ()
203
os.system("/usr/sbin/chroot %s bash -c 'apt-get update;apt-get -f -y --force-yes install %s'" % (os.path.join(self._project_dir, 'source'), packages))
205
self._remove_fake_utils()
207
def upgrade_packages (self, distUpgrade = True):
207
def upgrade_packages(self, distUpgrade = True):
208
208
''' This method upgrades the chroot's packages '''
209
self._prepare_chroot ()
210
self._install_fake_utils ()
209
self._prepare_chroot()
210
self._install_fake_utils()
212
212
upgradeType = "dist-upgrade"
217
217
os.putenv('LANGUAGE', 'C')
218
218
os.putenv('DEBIAN_FRONTEND', 'noninteractive')
219
219
print "Upgrading packages"
220
os.system ("/usr/sbin/chroot %s bash -c 'apt-get update;apt-get -f -y --force-yes %s'" % (os.path.join (self._project_dir, 'source'), upgradeType))
221
self._close_chroot ()
222
self._remove_fake_utils ()
225
def remove_packages (self, packages, autoremove=False):
220
os.system("/usr/sbin/chroot %s bash -c 'apt-get update;apt-get -f -y --force-yes %s'" % (os.path.join(self._project_dir, 'source'), upgradeType))
222
self._remove_fake_utils()
225
def remove_packages(self, packages, autoremove=False):
226
226
''' This method remove the packages list into chroot '''
227
self._prepare_chroot ()
228
self._install_fake_utils ()
227
self._prepare_chroot()
228
self._install_fake_utils()
229
229
os.putenv('LC_ALL', 'C')
230
230
os.putenv('LANGUAGE', 'C')
231
231
os.putenv('DEBIAN_FRONTEND', 'noninteractive')
232
if type (packages) != str:
233
packages = ' '.join (packages)
232
if type(packages) != str:
233
packages = ' '.join(packages)
234
234
print "Removing packages: %s" % packages
236
236
removeType = 'auto'
238
238
removeType = 'remove'
239
239
os.system("/usr/sbin/chroot %s bash -c 'apt-get -f -y --force-yes %s --purge %s'" % (os.path.join(self._project_dir, 'source'), removeType, packages))
240
self._close_chroot ()
241
self._remove_fake_utils ()
245
def clean_chroot (self):
241
self._remove_fake_utils()
245
def clean_chroot(self):
246
246
''' Delete /var/lin/apt/lists, /var/cache/apt/archives, /etc/resolv.conf ... '''
247
self._prepare_chroot ()
248
os.system ("/usr/sbin/chroot %s bash -c 'apt-get clean'" % os.path.join (self._project_dir, 'source'))
249
self._close_chroot ()
247
self._prepare_chroot()
248
os.system("/usr/sbin/chroot %s bash -c 'apt-get clean'" % os.path.join(self._project_dir, 'source'))
250
250
print "cleanning /var/lib/apt/lists"
251
251
#TODO: Add some checks here ..
252
252
# Hack to prevent kde-guidance regenerate xorg.conf file
254
os.unlink (os.path.join(self._project_dir, 'source/var/lib/guidance/guidance-gfxhardware-snapshot'))
254
os.unlink(os.path.join(self._project_dir, 'source/var/lib/guidance/guidance-gfxhardware-snapshot'))
258
os.unlink (os.path.join(self._project_dir, 'source/boot/*.bak'))
258
os.unlink(os.path.join(self._project_dir, 'source/boot/*.bak'))
261
shutil.rmtree (os.path.join(self._project_dir, 'source/var/lib/apt/lists'))
262
os.mkdir (os.path.join(self._project_dir, 'source/var/lib/apt/lists'))
263
os.mkdir (os.path.join(self._project_dir, 'source/var/lib/apt/lists/partial'))
261
shutil.rmtree(os.path.join(self._project_dir, 'source/var/lib/apt/lists'))
262
os.mkdir(os.path.join(self._project_dir, 'source/var/lib/apt/lists'))
263
os.mkdir(os.path.join(self._project_dir, 'source/var/lib/apt/lists/partial'))