~unidistro/unidistro/unidistro

« back to all changes in this revision

Viewing changes to framework/unidistro/debootstrapModule.py

  • Committer: Enrique Hernández Bello
  • Date: 2009-06-25 19:02:23 UTC
  • Revision ID: quique@osl.ull.es-20090625190223-lutikl73d2uikw4s
Removed spaces before brackets in all functions and methods.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
 
47
47
class createDebootstrap:
48
48
 
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") 
53
53
 
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
58
 
                                sys.exit (1)
 
58
                                sys.exit(1)
59
59
                else:
60
60
                        try:
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'))
64
64
                        except IOError, e:
65
65
                                print e
66
66
 
67
67
 
68
 
        def create (self):
 
68
        def create(self):
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" % (
72
72
                        DBOOTSTRAP_PATH, 
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")
77
77
                        )
78
78
                )
79
79
                if retcode != 0:
80
80
                        print "Error creating debootstrap, return code: %d" % retcode
81
 
                        sys.exit (retcode)
 
81
                        sys.exit(retcode)
82
82
                # From guadalinex
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)
86
 
                kernelConfig.close ()
 
84
                kernelConfig = open(os.path.join(self._project_dir, 'source', 'etc', 'kernel-img.conf'), "w")
 
85
                kernelConfig.write(KERNEL_PACKAGE_DIRTY_HACK)
 
86
                kernelConfig.close()
87
87
                try:
88
88
                        sources_list = ""
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)
93
 
                        apt.close ()
 
91
                        apt = open(os.path.join(self._project_dir, 'source', 'etc', 'apt', 'sources.list'), "w")
 
92
                        apt.write(sources_list)
 
93
                        apt.close()
94
94
                except TypeError:
95
95
                        pass
96
96
        
97
97
 
98
 
        def delete (self):
 
98
        def delete(self):
99
99
                ''' Just delete the debootstrap '''
100
100
                try:
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:
104
104
                                print e
105
 
                                sys.exit (e.errno)
 
105
                                sys.exit(e.errno)
106
106
 
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'))
111
111
 
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')
115
115
 
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')
119
119
 
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')
123
123
                
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')
127
 
 
128
 
 
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')
 
127
 
 
128
 
 
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"):
134
134
                        try:
135
 
                                path = volatiles.search(line).groups ()[1]
 
135
                                path = volatiles.search(line).groups()[1]
136
136
                        except:
137
137
                                continue
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
141
141
 
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')
145
145
 
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')
149
149
 
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')
153
153
 
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')
157
157
 
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'))
161
 
 
162
 
 
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'))
 
161
 
 
162
 
 
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'))
 
168
                self._close_chroot()
169
169
 
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)
177
177
 
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)
184
184
 
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'))
190
 
 
191
 
 
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'))
 
190
 
 
191
 
 
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()
196
196
 
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))
 
204
                self._close_chroot()
 
205
                self._remove_fake_utils()
206
206
 
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()
211
211
                if distUpgrade:
212
212
                        upgradeType = "dist-upgrade"
213
213
                else:
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 ()
223
 
 
224
 
 
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))
 
221
                self._close_chroot()
 
222
                self._remove_fake_utils()
 
223
 
 
224
 
 
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
235
235
                if autoremove:
236
236
                        removeType = 'auto'
237
237
                else:
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 ()
242
 
 
243
 
 
244
 
 
245
 
        def clean_chroot (self):
 
240
                self._close_chroot()
 
241
                self._remove_fake_utils()
 
242
 
 
243
 
 
244
 
 
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'))
 
249
                self._close_chroot()
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
253
253
                try:
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'))
255
255
                except:
256
256
                        pass
257
257
                try:
258
 
                        os.unlink (os.path.join(self._project_dir, 'source/boot/*.bak'))
 
258
                        os.unlink(os.path.join(self._project_dir, 'source/boot/*.bak'))
259
259
                except:
260
260
                        pass
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'))
264
264