]> code.ossystems Code Review - openembedded-core.git/commitdiff
class/lib: Fix up various file access methods
authorRichard Purdie <richard.purdie@linuxfoundation.org>
Thu, 9 May 2013 16:05:58 +0000 (17:05 +0100)
committerRichard Purdie <richard.purdie@linuxfoundation.org>
Thu, 9 May 2013 21:25:31 +0000 (22:25 +0100)
There are various bits of cruft that have built up around our file accesses. This patch
cleans some of them up, specifically:

 * Remove pointless "from __builtin__ import file"
 * Use open(), not file()
 * Wrap file usage in a with container to ensure files are closed
 * Add missing .close() calls in some cases

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
12 files changed:
meta/classes/imagetest-qemu.bbclass
meta/classes/insane.bbclass
meta/classes/libc-package.bbclass
meta/classes/metadata_scm.bbclass
meta/classes/package.bbclass
meta/classes/package_deb.bbclass
meta/classes/package_ipk.bbclass
meta/classes/package_rpm.bbclass
meta/classes/sanity.bbclass
meta/lib/oe/packagedata.py
meta/lib/oe/utils.py
meta/recipes-core/busybox/busybox.inc

index 63ba08718e29282a392f8c0d0acd28af5979cc35..c30d1cbc8569376233eaa9c8667bba1cd94be1f0 100644 (file)
@@ -146,6 +146,7 @@ def qemuimagetest_main(d):
                        if not os.path.isfile(fulltestcase):
                             raise bb.build.FuncFailed("Testcase %s not found" % fulltestcase)
                        list.append((item, casefile, fulltestcase))
+                    f.close()
         final_list = check_list(list)
         return final_list
 
index 809aa457a10867cf1b0fa792d1f0723a66b4e843..4d2392e9084dcbd458baa3483c99456170b2e802 100644 (file)
@@ -518,9 +518,10 @@ def package_qa_check_buildpaths(path, name, d, elf, messages):
         return
 
     tmpdir = d.getVar('TMPDIR', True)
-    file_content = open(path).read()
-    if tmpdir in file_content:
-        messages.append("File %s in package contained reference to tmpdir" % package_qa_clean_path(path,d))
+    with open(path) as f:
+        file_content = f.read()
+        if tmpdir in file_content:
+            messages.append("File %s in package contained reference to tmpdir" % package_qa_clean_path(path,d))
 
 
 QAPATHTEST[xorg-driver-abi] = "package_qa_check_xorg_driver_abi"
@@ -634,15 +635,17 @@ def package_qa_check_staged(path,d):
         for file in files:
             path = os.path.join(root,file)
             if file.endswith(".la"):
-                file_content = open(path).read()
-                if workdir in file_content:
-                    error_msg = "%s failed sanity test (workdir) in path %s" % (file,root)
-                    sane = package_qa_handle_error("la", error_msg, d)
+                with open(path) as f:
+                    file_content = f.read()
+                    if workdir in file_content:
+                        error_msg = "%s failed sanity test (workdir) in path %s" % (file,root)
+                        sane = package_qa_handle_error("la", error_msg, d)
             elif file.endswith(".pc"):
-                file_content = open(path).read()
-                if pkgconfigcheck in file_content:
-                    error_msg = "%s failed sanity test (tmpdir) in path %s" % (file,root)
-                    sane = package_qa_handle_error("pkgconfig", error_msg, d)
+                with open(path) as f:
+                    file_content = f.read()
+                    if pkgconfigcheck in file_content:
+                        error_msg = "%s failed sanity test (tmpdir) in path %s" % (file,root)
+                        sane = package_qa_handle_error("pkgconfig", error_msg, d)
 
     return sane
 
index 3a131540f3a2444acfcd0e43aec79ae3e2d3e272..74e2078544bf727e2f366ac415816e1857df08fc 100644 (file)
@@ -146,7 +146,7 @@ python package_do_split_gconvs () {
 
     def calc_gconv_deps(fn, pkg, file_regex, output_pattern, group):
         deps = []
-        f = open(fn, "r")
+        f = open(fn, "rb")
         c_re = re.compile('^copy "(.*)"')
         i_re = re.compile('^include "(\w+)".*')
         for l in f.readlines():
@@ -167,7 +167,7 @@ python package_do_split_gconvs () {
 
     def calc_charmap_deps(fn, pkg, file_regex, output_pattern, group):
         deps = []
-        f = open(fn, "r")
+        f = open(fn, "rb")
         c_re = re.compile('^copy "(.*)"')
         i_re = re.compile('^include "(\w+)".*')
         for l in f.readlines():
@@ -187,7 +187,7 @@ python package_do_split_gconvs () {
 
     def calc_locale_deps(fn, pkg, file_regex, output_pattern, group):
         deps = []
-        f = open(fn, "r")
+        f = open(fn, "rb")
         c_re = re.compile('^copy "(.*)"')
         i_re = re.compile('^include "(\w+)".*')
         for l in f.readlines():
index e9b207c24f0b8362f793d33ddfcf733d882ef6d0..8d3988ace8e8a5679f29011e6d941c16b398ead1 100644 (file)
@@ -32,10 +32,11 @@ def base_get_scmbasepath(d):
 def base_get_metadata_monotone_branch(path, d):
     monotone_branch = "<unknown>"
     try:
-        monotone_branch = file( "%s/_MTN/options" % path ).read().strip()
-        if monotone_branch.startswith( "database" ):
-            monotone_branch_words = monotone_branch.split()
-            monotone_branch = monotone_branch_words[ monotone_branch_words.index( "branch" )+1][1:-1]
+        with open("%s/_MTN/options" % path) as f:
+            monotone_branch = f.read().strip()
+            if monotone_branch.startswith( "database" ):
+                monotone_branch_words = monotone_branch.split()
+                monotone_branch = monotone_branch_words[ monotone_branch_words.index( "branch" )+1][1:-1]
     except:
         pass
     return monotone_branch
@@ -43,10 +44,11 @@ def base_get_metadata_monotone_branch(path, d):
 def base_get_metadata_monotone_revision(path, d):
     monotone_revision = "<unknown>"
     try:
-        monotone_revision = file( "%s/_MTN/revision" % path ).read().strip()
-        if monotone_revision.startswith( "format_version" ):
-            monotone_revision_words = monotone_revision.split()
-            monotone_revision = monotone_revision_words[ monotone_revision_words.index( "old_revision" )+1][1:-1]
+        with open("%s/_MTN/revision" % path) as f:
+            monotone_revision = f.read().strip()
+            if monotone_revision.startswith( "format_version" ):
+                monotone_revision_words = monotone_revision.split()
+                monotone_revision = monotone_revision_words[ monotone_revision_words.index( "old_revision" )+1][1:-1]
     except IOError:
         pass
     return monotone_revision
@@ -54,7 +56,8 @@ def base_get_metadata_monotone_revision(path, d):
 def base_get_metadata_svn_revision(path, d):
     revision = "<unknown>"
     try:
-        revision = file( "%s/.svn/entries" % path ).readlines()[3].strip()
+        with open("%s/.svn/entries" % path) as f:
+            revision = f.readlines()[3].strip()
     except IOError:
         pass
     return revision
index 7d0684c95c57b2925d3247c2ec93ec529986e6d5..36b3ae5109deb61c397c4b7175ca89d666c41fc5 100644 (file)
@@ -1096,7 +1096,8 @@ python emit_pkgdata() {
 
     def get_directory_size(dir):
         if os.listdir(dir):
-            size = int(os.popen('du -sk %s' % dir).readlines()[0].split('\t')[0])
+            with os.popen('du -sk %s' % dir) as f:
+                size = int(f.readlines()[0].split('\t')[0])
         else:
             size = 0
         return size
@@ -1203,7 +1204,7 @@ python emit_pkgdata() {
         g = glob('*')
         if g or allow_empty == "1":
             packagedfile = pkgdatadir + '/runtime/%s.packaged' % pkg
-            file(packagedfile, 'w').close()
+            open(packagedfile, 'w').close()
 
     if bb.data.inherits_class('kernel', d) or bb.data.inherits_class('module-base', d):
         write_extra_runtime_pkgs(variants, packages, pkgdatadir)
@@ -1633,7 +1634,7 @@ def read_libdep_files(d):
         for extension in ".shlibdeps", ".pcdeps", ".clilibdeps":
             depsfile = d.expand("${PKGDEST}/" + pkg + extension)
             if os.access(depsfile, os.R_OK):
-                fd = file(depsfile)
+                fd = open(depsfile)
                 lines = fd.readlines()
                 fd.close()
                 for l in lines:
index 853b5ea8a397f49f6f7b11a76f339f4327452320..313758f43f2437c5ccbabad637762ef35c40c16b 100644 (file)
@@ -227,7 +227,7 @@ python do_package_deb () {
         bb.mkdirhier(controldir)
         os.chmod(controldir, 0755)
         try:
-            ctrlfile = file(os.path.join(controldir, 'control'), 'wb')
+            ctrlfile = open(os.path.join(controldir, 'control'), 'w')
             # import codecs
             # ctrlfile = codecs.open("someFile", "w", "utf-8")
         except OSError:
@@ -355,7 +355,7 @@ python do_package_deb () {
             if not scriptvar:
                 continue
             try:
-                scriptfile = file(os.path.join(controldir, script), 'w')
+                scriptfile = open(os.path.join(controldir, script), 'w')
             except OSError:
                 bb.utils.unlockfile(lf)
                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
@@ -367,7 +367,7 @@ python do_package_deb () {
         conffiles_str = localdata.getVar("CONFFILES", True)
         if conffiles_str:
             try:
-                conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
+                conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
             except OSError:
                 bb.utils.unlockfile(lf)
                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
index 5873f712056d951bf47574c04c171110e6295a72..f6797ada9d54691b50f37c881e675d9214990e31 100644 (file)
@@ -268,7 +268,7 @@ python do_package_ipk () {
         controldir = os.path.join(root, 'CONTROL')
         bb.mkdirhier(controldir)
         try:
-            ctrlfile = file(os.path.join(controldir, 'control'), 'w')
+            ctrlfile = open(os.path.join(controldir, 'control'), 'w')
         except OSError:
             bb.utils.unlockfile(lf)
             raise bb.build.FuncFailed("unable to open control file for writing.")
@@ -369,7 +369,7 @@ python do_package_ipk () {
             if not scriptvar:
                 continue
             try:
-                scriptfile = file(os.path.join(controldir, script), 'w')
+                scriptfile = open(os.path.join(controldir, script), 'w')
             except OSError:
                 bb.utils.unlockfile(lf)
                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
@@ -380,7 +380,7 @@ python do_package_ipk () {
         conffiles_str = localdata.getVar("CONFFILES", True)
         if conffiles_str:
             try:
-                conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
+                conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
             except OSError:
                 bb.utils.unlockfile(lf)
                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
index 3a2997637b1ac378ad3078a96ee56350b46efa5a..25b14dd79df54ea44dd01a66c8b3a4dd5753a87f 100644 (file)
@@ -504,8 +504,7 @@ def write_rpm_perfiledata(srcname, d):
     outdepends = workdir + "/" + srcname + ".requires"
 
     try:
-        from __builtin__ import file
-        dependsfile = file(outdepends, 'w')
+        dependsfile = open(outdepends, 'w')
     except OSError:
         raise bb.build.FuncFailed("unable to open spec file for writing.")
 
@@ -518,8 +517,7 @@ def write_rpm_perfiledata(srcname, d):
     outprovides = workdir + "/" + srcname + ".provides"
 
     try:
-        from __builtin__ import file
-        providesfile = file(outprovides, 'w')
+        providesfile = open(outprovides, 'w')
     except OSError:
         raise bb.build.FuncFailed("unable to open spec file for writing.")
 
@@ -1005,8 +1003,7 @@ python write_specfile () {
 
     # Write the SPEC file
     try:
-        from __builtin__ import file
-        specfile = file(outspecfile, 'w')
+        specfile = open(outspecfile, 'w')
     except OSError:
         raise bb.build.FuncFailed("unable to open spec file for writing.")
 
index 1fb4a6c6293636a7da6b6d9b7a892b9b78286436..62fe7e4ef7f1a98608599daee1358f9eea531664 100644 (file)
@@ -561,14 +561,14 @@ def check_sanity(sanity_data):
     last_sstate_dir = ""
     sanityverfile = 'conf/sanity_info'
     if os.path.exists(sanityverfile):
-        f = open(sanityverfile, 'r')
-        for line in f:
-            if line.startswith('SANITY_VERSION'):
-                last_sanity_version = int(line.split()[1])
-            if line.startswith('TMPDIR'):
-                last_tmpdir = line.split()[1]
-            if line.startswith('SSTATE_DIR'):
-                last_sstate_dir = line.split()[1]
+        with open(sanityverfile, 'r') as f:
+            for line in f:
+                if line.startswith('SANITY_VERSION'):
+                    last_sanity_version = int(line.split()[1])
+                if line.startswith('TMPDIR'):
+                    last_tmpdir = line.split()[1]
+                if line.startswith('SSTATE_DIR'):
+                    last_sstate_dir = line.split()[1]
     
     sanity_version = int(sanity_data.getVar('SANITY_VERSION', True) or 1)
     network_error = False
@@ -584,25 +584,24 @@ def check_sanity(sanity_data):
         if last_sstate_dir != sstate_dir:
             messages = messages + check_sanity_sstate_dir_change(sstate_dir, sanity_data)
     if os.path.exists("conf") and not messages:
-        f = open(sanityverfile, 'w')
-        f.write("SANITY_VERSION %s\n" % sanity_version) 
-        f.write("TMPDIR %s\n" % tmpdir) 
-        f.write("SSTATE_DIR %s\n" % sstate_dir) 
+        with open(sanityverfile, 'w') as f:
+            f.write("SANITY_VERSION %s\n" % sanity_version) 
+            f.write("TMPDIR %s\n" % tmpdir) 
+            f.write("SSTATE_DIR %s\n" % sstate_dir) 
 
     #
     # Check that TMPDIR hasn't changed location since the last time we were run
     #
     checkfile = os.path.join(tmpdir, "saved_tmpdir")
     if os.path.exists(checkfile):
-        f = open(checkfile, "r")
-        saved_tmpdir = f.read().strip()
-        if (saved_tmpdir != tmpdir):
-            messages = messages + "Error, TMPDIR has changed location. You need to either move it back to %s or rebuild\n" % saved_tmpdir
+        with open(checkfile, "r") as f:
+            saved_tmpdir = f.read().strip()
+            if (saved_tmpdir != tmpdir):
+                messages = messages + "Error, TMPDIR has changed location. You need to either move it back to %s or rebuild\n" % saved_tmpdir
     else:
         bb.utils.mkdirhier(tmpdir)
-        f = open(checkfile, "w")
-        f.write(tmpdir)
-    f.close()
+        with open(checkfile, "w") as f:
+            f.write(tmpdir)
 
     #
     # Check the 'ABI' of TMPDIR
@@ -610,33 +609,32 @@ def check_sanity(sanity_data):
     current_abi = sanity_data.getVar('OELAYOUT_ABI', True)
     abifile = sanity_data.getVar('SANITY_ABIFILE', True)
     if os.path.exists(abifile):
-        f = open(abifile, "r")
-        abi = f.read().strip()
+        with open(abifile, "r") as f:
+            abi = f.read().strip()
         if not abi.isdigit():
-            f = open(abifile, "w")
-            f.write(current_abi)
+            with open(abifile, "w") as f:
+                f.write(current_abi)
         elif abi == "2" and current_abi == "3":
             bb.note("Converting staging from layout version 2 to layout version 3")
             subprocess.call(sanity_data.expand("mv ${TMPDIR}/staging ${TMPDIR}/sysroots"), shell=True)
             subprocess.call(sanity_data.expand("ln -s sysroots ${TMPDIR}/staging"), shell=True)
             subprocess.call(sanity_data.expand("cd ${TMPDIR}/stamps; for i in */*do_populate_staging; do new=`echo $i | sed -e 's/do_populate_staging/do_populate_sysroot/'`; mv $i $new; done"), shell=True)
-            f = open(abifile, "w")
-            f.write(current_abi)
+            with open(abifile, "w") as f:
+                f.write(current_abi)
         elif abi == "3" and current_abi == "4":
             bb.note("Converting staging layout from version 3 to layout version 4")
             if os.path.exists(sanity_data.expand("${STAGING_DIR_NATIVE}${bindir_native}/${MULTIMACH_HOST_SYS}")):
                 subprocess.call(sanity_data.expand("mv ${STAGING_DIR_NATIVE}${bindir_native}/${MULTIMACH_HOST_SYS} ${STAGING_BINDIR_CROSS}"), shell=True)
                 subprocess.call(sanity_data.expand("ln -s ${STAGING_BINDIR_CROSS} ${STAGING_DIR_NATIVE}${bindir_native}/${MULTIMACH_HOST_SYS}"), shell=True)
-
-            f = open(abifile, "w")
-            f.write(current_abi)
+            with open(abifile, "w") as f:
+                f.write(current_abi)
         elif abi == "4":
             messages = messages + "Staging layout has changed. The cross directory has been deprecated and cross packages are now built under the native sysroot.\nThis requires a rebuild.\n"
         elif abi == "5" and current_abi == "6":
             bb.note("Converting staging layout from version 5 to layout version 6")
             subprocess.call(sanity_data.expand("mv ${TMPDIR}/pstagelogs ${SSTATE_MANIFESTS}"), shell=True)
-            f = open(abifile, "w")
-            f.write(current_abi)
+            with open(abifile, "w") as f:
+                f.write(current_abi)
         elif abi == "7" and current_abi == "8":
             messages = messages + "Your configuration is using stamp files including the sstate hash but your build directory was built with stamp files that do not include this.\nTo continue, either rebuild or switch back to the OEBasic signature handler with BB_SIGNATURE_HANDLER = 'OEBasic'.\n"
         elif (abi != current_abi and current_abi == "9"):
@@ -645,9 +643,8 @@ def check_sanity(sanity_data):
             # Code to convert from one ABI to another could go here if possible.
             messages = messages + "Error, TMPDIR has changed its layout version number (%s to %s) and you need to either rebuild, revert or adjust it at your own risk.\n" % (abi, current_abi)
     else:
-        f = open(abifile, "w")
-        f.write(current_abi)
-    f.close()
+        with open(abifile, "w") as f:
+            f.write(current_abi)
 
     oeroot = sanity_data.getVar('COREBASE')
     if oeroot.find ('+') != -1:
index 62fd71898e2b2e62d5221c3847b8fdec7874ea46..14c38bdc0f652c29de4a460e6fb64e4ae7ee2427 100644 (file)
@@ -12,7 +12,7 @@ def read_pkgdatafile(fn):
 
     if os.access(fn, os.R_OK):
         import re
-        f = file(fn, 'r')
+        f = open(fn, 'r')
         lines = f.readlines()
         f.close()
         r = re.compile("([^:]+):\s*(.*)")
index ec8260d9bdcc8aab7b751e141409f7fa338784b2..0a2092b24bfb2e2b4553b509fd36bd03f81004ab 100644 (file)
@@ -7,11 +7,13 @@ except ImportError:
 
 def read_file(filename):
     try:
-        f = file( filename, "r" )
+        f = open( filename, "r" )
     except IOError as reason:
         return "" # WARNING: can't raise an error now because of the new RDEPENDS handling. This is a bit ugly. :M:
     else:
-        return f.read().strip()
+        data = f.read().strip()
+        f.close()
+        return data
     return None
 
 def ifelse(condition, iftrue = True, iffalse = False):
index 59e0141e1907b3bfc8c8c2bd9fe594f2c82d7743..00c88abcba140fe9f56473711898f79cf1a25a55 100644 (file)
@@ -262,6 +262,7 @@ python do_package_prepend () {
 
         d.appendVar('ALTERNATIVE_%s' % (pn), ' ' + alt_name)
         d.setVarFlag('ALTERNATIVE_LINK_NAME', alt_name, alt_link_name)
+    f.close()
 }
 
 pkg_postinst_${PN} () {