rootfs: cleanups etc.
authorTimo Savola <tsavola@movial.fi>
Fri, 18 Jul 2008 08:10:51 +0000 (11:10 +0300)
committerTimo Savola <tsavola@movial.fi>
Fri, 18 Jul 2008 08:10:51 +0000 (11:10 +0300)
matrix/rootfs.py

index 0f7323f..dfebc10 100644 (file)
@@ -240,26 +240,33 @@ class Builder(object):
                copy_list = self.file_list
 
                for f in self.remove_list:
-                               try:
-                                       copy_list.remove(f)
-                               except:
-                                       pass
+                       try:
+                               copy_list.remove(f)
+                       except:
+                               pass
 
-               n_files = len(copy_list)*1.0
-               current_percent = 0.0
-               current_file = 0.0
+               n_files = len(copy_list)
+               current_percent = -1
+               current_file = 0
 
                old_dir = os.getcwd()
                os.chdir(self.path)
 
-               if os.path.exists(os.path.join(self.builddir, self.target)):
-                       print "%s exists, please remove it and try again" % os.path.join(self.builddir, self.target)
+               basepath = os.path.join(self.builddir, self.target)
+
+               if os.path.exists(os.path.join(basepath)):
+                       print os.path.join(basepath), "exists, please remove it and try again"
                        return
 
                # Copy the wanted files, strip binaries
-               print "Copying files..."
                for f in copy_list:
-                       f = f.replace(self.path, "")
+                       last_percent = current_percent
+                       current_percent = current_file * 100 / n_files
+                       if current_percent != last_percent:
+                               print "\rCopying files: %3d%% " % current_percent,
+                               sys.stdout.flush()
+
+                       f = f.replace(self.path, "", 1)
                        if f[0] == '/':
                                f = f[1:]
                        path = os.path.join(self.builddir, self.target, os.path.dirname(f))
@@ -288,19 +295,17 @@ class Builder(object):
                                        # is from the include files directive
                                        if e.errno == errno.EEXIST:
                                                pass
-                       if (current_file/n_files)*100 > current_percent:
-                               current_percent += 10.0
-                               print "%0.1f%%" % current_percent,
-                               sys.stdout.flush()
 
-                       current_file += 1.0
+                       current_file += 1
+
+               print "\rCopying files: 100%"
 
                # Create extra directory structure
-               print "\nCreating extra directories..."
+               print "Creating extra directories..."
                for d in self.created_paths:
                        if d[0] == '/':
                                d = d[1:]
-                       path = os.path.join(self.builddir, self.target, d)
+                       path = os.path.join(basepath, d)
                        try:
                                os.makedirs(path)
                        except OSError, e:
@@ -310,7 +315,17 @@ class Builder(object):
                # Remove always the fakeroot environment.
                if os.path.exists("/tmp/env.faked"):
                        os.remove("/tmp/env.faked")
-               os.system("touch /tmp/env.faked")
+
+               fakedbs = []
+               for c in config.components.values():
+                       path = os.path.join(c.meta.path, 'fakedb')
+                       if os.path.exists(path):
+                               fakedbs.append(path)
+
+               if fakedbs:
+                       os.system('cat >/tmp/env.faked ' + ' '.join(fakedbs))
+               else:
+                       os.system('touch /tmp/env.faked')
 
                # Create device nodes
                print "Creating device nodes..."
@@ -320,21 +335,21 @@ class Builder(object):
                                device_type,major,minor = self.devices[d]
                        else:
                                device_type,major,minor,mode = self.devices[d]
-                       os.system("%s -m %s %s/%s/dev/%s %s %i %i" % (self.mknod, mode, self.builddir, self.target, d, device_type, major, minor))
+                       os.system("%s -m %s %s %s %i %i" % (self.mknod, mode, os.path.join(basepath, 'dev', d), device_type, major, minor))
 
                # Fix permissions
                print "Adjusting ownerships and permissions..."
                # Make the whole fs root-owned
-               os.system("%s root.root -R %s/%s" % (self.chown, self.builddir, self.target))
+               os.system("%s root.root -R %s" % (self.chown, basepath))
 
                # For sshd:
-               os.system("%s go-r %s/%s/etc/ssh*key*" % (self.chmod, self.builddir, self.target))
+               os.system("%s go-r %s/etc/ssh*key*" % (self.chmod, basepath))
 
                # For /var/empty (sshd requires this):
-               os.system("%s go-rw /%s/%s/var/empty" % (self.chmod, self.builddir, self.target))
+               os.system("%s go-rw /%s/var/empty" % (self.chmod, basepath))
 
                # /dev/null needs to be writable by all:
-               os.system("%s a+rw /%s/%s/dev/null" % (self.chmod, self.builddir, self.target))
+               os.system("%s a+rw /%s/dev/null" % (self.chmod, basepath))
 
                # Set non-root owners
                print "Setting non-root ownerships..."
@@ -342,19 +357,19 @@ class Builder(object):
                        dir = d
                        if dir[0] == '/':
                                dir = dir[1:]
-                       if os.path.exists(os.path.join(self.builddir, self.target, dir)):
+                       if os.path.exists(os.path.join(basepath, dir)):
                                user,group = self.change_owner[d]
-                               os.system("%s %s.%s -R %s/%s/%s " % (self.chown, user, group, self.builddir, self.target, dir))
+                               os.system("%s %s.%s -R %s/%s " % (self.chown, user, group, basepath, dir))
 
                # Build rootfs
                if build_target == "all" or build_target == "rootfs":
                        print "Creating a rootfs..."
-                       os.system("%s -c --one-file-system -C %s/%s -z -f %s/%s.tgz ." % (self.tar, self.builddir, self.target, self.builddir, self.target))
+                       os.system("%s -c --one-file-system -C %s -z -f %s.tgz ." % (self.tar, basepath, basepath))
 
                # Build jffs2
                if build_target == "all" or build_target == "jffs2":
                        print "Creating a root image as JFFS2..."
-                       os.system("%s -p%d -n -e%d -r %s/%s -o %s/%s.jffs2" % (self.mkfs_jffs2, self.flash_pad_size, self.flash_erase_size, self.builddir, self.target, self.builddir, self.target))
+                       os.system("%s -p%d -n -e%d -r %s -o %s.jffs2" % (self.mkfs_jffs2, self.flash_pad_size, self.flash_erase_size, basepath, basepath))
 
                # Build ubifs
                if build_target == "ubi":
@@ -362,7 +377,7 @@ class Builder(object):
                        ubinize_config = ""
                        ubinize_config += "[ubifs]\n"
                        ubinize_config += "mode=%s\n" % self.ubinize_config_mode
-                       ubinize_config += "image=%s/%s.ubifs\n" % (self.builddir, self.target)
+                       ubinize_config += "image=%s.ubifs\n" % (basepath)
                        ubinize_config += "vol_id=%d\n" % self.ubinize_config_vol_id
                        ubinize_config += "vol_size=%d\n" % self.ubinize_config_vol_size
                        ubinize_config += "vol_type=%s\n" % self.ubinize_config_vol_type
@@ -373,32 +388,28 @@ class Builder(object):
                        f.write(ubinize_config)
                        f.close()
 
-                       os.system("%s --compr=%s -r %s/%s -o %s/%s.ubifs -m %d -e %d -c %d" % (self.mkfs_ubifs, self.flash_compression, self.builddir, self.target, self.builddir, self.target, self.ubifs_min_io_size, self.ubifs_leb_size, self.ubifs_max_leb_count))
-                       os.system("%s -m %d -p %d -o %s/%s.ubi %s/ubinize.cfg" % (self.ubinize, self.ubifs_min_io_size, self.ubinize_peb_size, self.builddir, self.target, self.builddir))
+                       os.system("%s --compr=%s -r %s -o %s.ubifs -m %d -e %d -c %d" % (self.mkfs_ubifs, self.flash_compression, basepath, basepath, self.ubifs_min_io_size, self.ubifs_leb_size, self.ubifs_max_leb_count))
+                       os.system("%s -m %d -p %d -o %s.ubi %s/ubinize.cfg" % (self.ubinize, self.ubifs_min_io_size, self.ubinize_peb_size, basepath, self.builddir))
 
                # Build development rootfs
                if build_target == "all" or build_target == "devrootfs":
                        print "Creating a rootstrap..."
-                       os.system("%s -c --one-file-system -C /targets/%s -z -f %s/%s-rootstrap.tgz ." % ("tar", self.path, self.builddir, self.target))
+                       os.system("%s -c --one-file-system -C %s -z -f %s-rootstrap.tgz ." % ("tar", self.path, basepath))
 
                os.chdir(old_dir)
 
                print "Build finished:"
-               if build_target == "all" or build_target == "rootfs":
-                       s = os.stat(self.builddir + "/" + self.target + ".tgz");
-                       print "\t%s/%s.tgz\t%0.2f MiB" % (self.builddir, self.target, s.st_size/1024.0/1024.0)
-               if build_target == "all" or build_target == "jffs2":
-                       s = os.stat(self.builddir + "/" + self.target + ".jffs2");
-                       print "\t%s/%s.jffs2\t%0.2f MiB" % (self.builddir, self.target, s.st_size/1024.0/1024.0)
-               if build_target == "ubi":
-                       s = os.stat(self.builddir + "/" + self.target + ".ubi");
-                       print "\t%s/%s.ubi\t%0.2f MiB" % (self.builddir, self.target, s.st_size/1024.0/1024.0)
-               if build_target == "all" or build_target == "devrootfs":
-                       s = os.stat(self.builddir + "/" + self.target + "-rootstrap.tgz");
-                       print "\t%s/%s-rootstrap.tgz\t%0.2f MiB" % (self.builddir, self.target, s.st_size/1024.0/1024.0)
+               for targets, suffix in ((('rootfs', 'all'), '.tgz'),
+                                       (('jffs2', 'all'), '.jffs2'),
+                                       (('ubi',), '.ubi'),
+                                       (('devrootfs', 'all'), '-rootstrap.tgz')):
+                       if build_target in targets:
+                               fullname = basepath + suffix
+                               s = os.stat(fullname)
+                               size = s.st_size / 1024.0 / 1024.0
+                               print '  %-32s %.2f MiB' % (fullname, size)
 
                if clean:
                        print "Cleaning up"
                        os.remove("/tmp/env.faked")
-                       os.system("rm -rf %s/%s" % (self.builddir, self.target))
-
+                       os.system("rm -rf %s" % basepath)