Openembedded Core Discussions
 help / color / mirror / Atom feed
From: Ed Bartosh <ed.bartosh@linux.intel.com>
To: openembedded-core@lists.openembedded.org
Subject: [wic][PATCH 16/20] wic: Refactor fstab update code
Date: Mon, 29 Jun 2015 22:10:29 +0300	[thread overview]
Message-ID: <1435605033-11509-17-git-send-email-ed.bartosh@linux.intel.com> (raw)
In-Reply-To: <1435605033-11509-1-git-send-email-ed.bartosh@linux.intel.com>

Made the code to backup and restore fstab only if it's modified.

Cleaned up the code. Made it more pythonic.

Improved code readability by moving code from several tiny
methods into one place.

Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>

diff --git a/scripts/lib/wic/imager/direct.py b/scripts/lib/wic/imager/direct.py
index 58a9e9d..561c396 100644
--- a/scripts/lib/wic/imager/direct.py
+++ b/scripts/lib/wic/imager/direct.py
@@ -90,74 +90,50 @@ class DirectImageCreator(BaseImageCreator):
                     return realnum + 1
                 return realnum
 
-    def __write_fstab(self, image_rootfs):
+    def _write_fstab(self, image_rootfs):
         """overriden to generate fstab (temporarily) in rootfs. This is called
         from _create, make sure it doesn't get called from
         BaseImage.create()
         """
-        if image_rootfs is None:
-            return None
+        if not image_rootfs:
+            return
+
+        fstab_path = image_rootfs + "/etc/fstab"
+        if not os.path.isfile(fstab_path):
+            return
 
-        fstab = image_rootfs + "/etc/fstab"
-        if not os.path.isfile(fstab):
-            return None
+        with open(fstab_path) as fstab:
+            fstab_lines = fstab.readlines()
 
-        parts = self._get_parts()
+        if self._update_fstab(fstab_lines, self._get_parts()):
+            shutil.copyfile(fstab_path, fstab_path + ".orig")
 
-        self._save_fstab(fstab)
-        fstab_lines = self._get_fstab(fstab, parts)
-        self._update_fstab(fstab_lines, parts)
-        self._write_fstab(fstab, fstab_lines)
+            with open(fstab_path, "w") as fstab:
+                fstab.writelines(fstab_lines)
 
-        return fstab
+            return fstab_path
 
     def _update_fstab(self, fstab_lines, parts):
         """Assume partition order same as in wks"""
-        for num, p in enumerate(parts, 1):
+        updated = False
+        for num, part in enumerate(parts, 1):
             pnum = self.__get_part_num(num, parts)
-            if not p.mountpoint or p.mountpoint == "/" or p.mountpoint == "/boot" or pnum == 0:
+            if not pnum or not part.mountpoint \
+               or part.mountpoint in ("/", "/boot"):
                 continue
 
-            part = ''
             # mmc device partitions are named mmcblk0p1, mmcblk0p2..
-            if p.disk.startswith('mmcblk'):
-                part = 'p'
-
-            device_name = "/dev/" + p.disk + part + str(pnum)
-
-            opts = "defaults"
-            if p.fsopts:
-                opts = p.fsopts
-
-            fstab_entry = device_name + "\t" + \
-                          p.mountpoint + "\t" + \
-                          p.fstype + "\t" + \
-                          opts + "\t0\t0\n"
-            fstab_lines.append(fstab_entry)
-
-    def _write_fstab(self, fstab, fstab_lines):
-        fstab = open(fstab, "w")
-        for line in fstab_lines:
-            fstab.write(line)
-        fstab.close()
-
-    def _save_fstab(self, fstab):
-        """Save the current fstab in rootfs"""
-        shutil.copyfile(fstab, fstab + ".orig")
-
-    def _restore_fstab(self, fstab):
-        """Restore the saved fstab in rootfs"""
-        if fstab is None:
-            return
-        shutil.move(fstab + ".orig", fstab)
+            prefix = 'p' if  part.disk.startswith('mmcblk') else ''
+            device_name = "/dev/%s%s%d" % (part.disk, prefix, pnum)
+
+            opts = part.fsopts if part.fsopts else "defaults"
+            line = "\t".join([device_name, part.mountpoint, part.fstype,
+                              opts, "0", "0"]) + "\n"
 
-    def _get_fstab(self, fstab, parts):
-        """Return the desired contents of /etc/fstab."""
-        f = open(fstab, "r")
-        fstab_contents = f.readlines()
-        f.close()
+            fstab_lines.append(line)
+            updated = True
 
-        return fstab_contents
+        return updated
 
     def set_bootimg_dir(self, bootimg_dir):
         """
@@ -250,7 +226,7 @@ class DirectImageCreator(BaseImageCreator):
             if not self.ks.handler.bootloader.source and p.mountpoint == "/boot":
                 self.ks.handler.bootloader.source = p.source
 
-        fstab = self.__write_fstab(self.rootfs_dir.get("ROOTFS_DIR"))
+        fstab_path = self._write_fstab(self.rootfs_dir.get("ROOTFS_DIR"))
 
         for p in parts:
             # need to create the filesystems in order to get their
@@ -277,7 +253,8 @@ class DirectImageCreator(BaseImageCreator):
                                        part_type=p.part_type,
                                        uuid=p.uuid)
 
-        self._restore_fstab(fstab)
+        if fstab_path:
+            shutil.move(fstab_path + ".orig", fstab_path)
 
         self.__image.layout_partitions(self.ptable_format)
 
-- 
2.1.4



  parent reply	other threads:[~2015-06-29 19:11 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-29 19:10 [wic][PATCH 00/20] miscellaneous fixes. poky-conrib:ed/wic/misc Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 01/20] wic: Fix misleading message Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 02/20] wic: Test rootfs plugin using image recipes Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 03/20] wic: Test rootfs plugin using rootfs paths Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 04/20] wic: Refactor getting bitbake variables Ed Bartosh
2015-06-29 21:48   ` Christopher Larson
2015-06-30  8:11     ` Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 05/20] wic: Include mount point into image report Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 06/20] wic: Remove annoing debug message Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 07/20] wic: Turn off debug output for 'bitbake -e' Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 08/20] wic: Refactor prepare_rootfs API Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 09/20] wic: Rename partition images Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 10/20] wic: Get rid of useless variable 'image_rootfs' Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 11/20] wic: Call methods better way Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 12/20] wic: Refactor prepare_empty_partition API Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 13/20] wic: Remove duplicated code Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 14/20] wic: Fix naming conflict Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 15/20] wic: Add --uuid partition option Ed Bartosh
2015-06-29 19:10 ` Ed Bartosh [this message]
2015-06-29 19:10 ` [wic][PATCH 17/20] wic: Remove __write_partition method Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 18/20] wic: Fix confusing error message Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 19/20] wic: Code cleanup: long lines, identation and whitespaces Ed Bartosh
2015-06-29 19:10 ` [wic][PATCH 20/20] wic: Code cleanup: unused imports Ed Bartosh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1435605033-11509-17-git-send-email-ed.bartosh@linux.intel.com \
    --to=ed.bartosh@linux.intel.com \
    --cc=openembedded-core@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox