class/lib: Fix up various file access methods

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

(From OE-Core rev: a43e0a8ecd0441131e929daf998c3cd454d9c8f3)

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Richard Purdie 2013-05-09 17:05:58 +01:00
parent d2ef952851
commit 566628d8cd
12 changed files with 79 additions and 74 deletions

View File

@ -146,6 +146,7 @@ def qemuimagetest_main(d):
if not os.path.isfile(fulltestcase): if not os.path.isfile(fulltestcase):
raise bb.build.FuncFailed("Testcase %s not found" % fulltestcase) raise bb.build.FuncFailed("Testcase %s not found" % fulltestcase)
list.append((item, casefile, fulltestcase)) list.append((item, casefile, fulltestcase))
f.close()
final_list = check_list(list) final_list = check_list(list)
return final_list return final_list

View File

@ -518,9 +518,10 @@ def package_qa_check_buildpaths(path, name, d, elf, messages):
return return
tmpdir = d.getVar('TMPDIR', True) tmpdir = d.getVar('TMPDIR', True)
file_content = open(path).read() with open(path) as f:
if tmpdir in file_content: file_content = f.read()
messages.append("File %s in package contained reference to tmpdir" % package_qa_clean_path(path,d)) 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" 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: for file in files:
path = os.path.join(root,file) path = os.path.join(root,file)
if file.endswith(".la"): if file.endswith(".la"):
file_content = open(path).read() with open(path) as f:
if workdir in file_content: file_content = f.read()
error_msg = "%s failed sanity test (workdir) in path %s" % (file,root) if workdir in file_content:
sane = package_qa_handle_error("la", error_msg, d) 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"): elif file.endswith(".pc"):
file_content = open(path).read() with open(path) as f:
if pkgconfigcheck in file_content: file_content = f.read()
error_msg = "%s failed sanity test (tmpdir) in path %s" % (file,root) if pkgconfigcheck in file_content:
sane = package_qa_handle_error("pkgconfig", error_msg, d) error_msg = "%s failed sanity test (tmpdir) in path %s" % (file,root)
sane = package_qa_handle_error("pkgconfig", error_msg, d)
return sane return sane

View File

@ -146,7 +146,7 @@ python package_do_split_gconvs () {
def calc_gconv_deps(fn, pkg, file_regex, output_pattern, group): def calc_gconv_deps(fn, pkg, file_regex, output_pattern, group):
deps = [] deps = []
f = open(fn, "r") f = open(fn, "rb")
c_re = re.compile('^copy "(.*)"') c_re = re.compile('^copy "(.*)"')
i_re = re.compile('^include "(\w+)".*') i_re = re.compile('^include "(\w+)".*')
for l in f.readlines(): 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): def calc_charmap_deps(fn, pkg, file_regex, output_pattern, group):
deps = [] deps = []
f = open(fn, "r") f = open(fn, "rb")
c_re = re.compile('^copy "(.*)"') c_re = re.compile('^copy "(.*)"')
i_re = re.compile('^include "(\w+)".*') i_re = re.compile('^include "(\w+)".*')
for l in f.readlines(): 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): def calc_locale_deps(fn, pkg, file_regex, output_pattern, group):
deps = [] deps = []
f = open(fn, "r") f = open(fn, "rb")
c_re = re.compile('^copy "(.*)"') c_re = re.compile('^copy "(.*)"')
i_re = re.compile('^include "(\w+)".*') i_re = re.compile('^include "(\w+)".*')
for l in f.readlines(): for l in f.readlines():

View File

@ -32,10 +32,11 @@ def base_get_scmbasepath(d):
def base_get_metadata_monotone_branch(path, d): def base_get_metadata_monotone_branch(path, d):
monotone_branch = "<unknown>" monotone_branch = "<unknown>"
try: try:
monotone_branch = file( "%s/_MTN/options" % path ).read().strip() with open("%s/_MTN/options" % path) as f:
if monotone_branch.startswith( "database" ): monotone_branch = f.read().strip()
monotone_branch_words = monotone_branch.split() if monotone_branch.startswith( "database" ):
monotone_branch = monotone_branch_words[ monotone_branch_words.index( "branch" )+1][1:-1] monotone_branch_words = monotone_branch.split()
monotone_branch = monotone_branch_words[ monotone_branch_words.index( "branch" )+1][1:-1]
except: except:
pass pass
return monotone_branch return monotone_branch
@ -43,10 +44,11 @@ def base_get_metadata_monotone_branch(path, d):
def base_get_metadata_monotone_revision(path, d): def base_get_metadata_monotone_revision(path, d):
monotone_revision = "<unknown>" monotone_revision = "<unknown>"
try: try:
monotone_revision = file( "%s/_MTN/revision" % path ).read().strip() with open("%s/_MTN/revision" % path) as f:
if monotone_revision.startswith( "format_version" ): monotone_revision = f.read().strip()
monotone_revision_words = monotone_revision.split() if monotone_revision.startswith( "format_version" ):
monotone_revision = monotone_revision_words[ monotone_revision_words.index( "old_revision" )+1][1:-1] monotone_revision_words = monotone_revision.split()
monotone_revision = monotone_revision_words[ monotone_revision_words.index( "old_revision" )+1][1:-1]
except IOError: except IOError:
pass pass
return monotone_revision return monotone_revision
@ -54,7 +56,8 @@ def base_get_metadata_monotone_revision(path, d):
def base_get_metadata_svn_revision(path, d): def base_get_metadata_svn_revision(path, d):
revision = "<unknown>" revision = "<unknown>"
try: 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: except IOError:
pass pass
return revision return revision

View File

@ -1096,7 +1096,8 @@ python emit_pkgdata() {
def get_directory_size(dir): def get_directory_size(dir):
if os.listdir(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: else:
size = 0 size = 0
return size return size
@ -1203,7 +1204,7 @@ python emit_pkgdata() {
g = glob('*') g = glob('*')
if g or allow_empty == "1": if g or allow_empty == "1":
packagedfile = pkgdatadir + '/runtime/%s.packaged' % pkg 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): if bb.data.inherits_class('kernel', d) or bb.data.inherits_class('module-base', d):
write_extra_runtime_pkgs(variants, packages, pkgdatadir) write_extra_runtime_pkgs(variants, packages, pkgdatadir)
@ -1633,7 +1634,7 @@ def read_libdep_files(d):
for extension in ".shlibdeps", ".pcdeps", ".clilibdeps": for extension in ".shlibdeps", ".pcdeps", ".clilibdeps":
depsfile = d.expand("${PKGDEST}/" + pkg + extension) depsfile = d.expand("${PKGDEST}/" + pkg + extension)
if os.access(depsfile, os.R_OK): if os.access(depsfile, os.R_OK):
fd = file(depsfile) fd = open(depsfile)
lines = fd.readlines() lines = fd.readlines()
fd.close() fd.close()
for l in lines: for l in lines:

View File

@ -227,7 +227,7 @@ python do_package_deb () {
bb.mkdirhier(controldir) bb.mkdirhier(controldir)
os.chmod(controldir, 0755) os.chmod(controldir, 0755)
try: try:
ctrlfile = file(os.path.join(controldir, 'control'), 'wb') ctrlfile = open(os.path.join(controldir, 'control'), 'w')
# import codecs # import codecs
# ctrlfile = codecs.open("someFile", "w", "utf-8") # ctrlfile = codecs.open("someFile", "w", "utf-8")
except OSError: except OSError:
@ -355,7 +355,7 @@ python do_package_deb () {
if not scriptvar: if not scriptvar:
continue continue
try: try:
scriptfile = file(os.path.join(controldir, script), 'w') scriptfile = open(os.path.join(controldir, script), 'w')
except OSError: except OSError:
bb.utils.unlockfile(lf) bb.utils.unlockfile(lf)
raise bb.build.FuncFailed("unable to open %s script file for writing." % script) 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) conffiles_str = localdata.getVar("CONFFILES", True)
if conffiles_str: if conffiles_str:
try: try:
conffiles = file(os.path.join(controldir, 'conffiles'), 'w') conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
except OSError: except OSError:
bb.utils.unlockfile(lf) bb.utils.unlockfile(lf)
raise bb.build.FuncFailed("unable to open conffiles for writing.") raise bb.build.FuncFailed("unable to open conffiles for writing.")

View File

@ -268,7 +268,7 @@ python do_package_ipk () {
controldir = os.path.join(root, 'CONTROL') controldir = os.path.join(root, 'CONTROL')
bb.mkdirhier(controldir) bb.mkdirhier(controldir)
try: try:
ctrlfile = file(os.path.join(controldir, 'control'), 'w') ctrlfile = open(os.path.join(controldir, 'control'), 'w')
except OSError: except OSError:
bb.utils.unlockfile(lf) bb.utils.unlockfile(lf)
raise bb.build.FuncFailed("unable to open control file for writing.") raise bb.build.FuncFailed("unable to open control file for writing.")
@ -369,7 +369,7 @@ python do_package_ipk () {
if not scriptvar: if not scriptvar:
continue continue
try: try:
scriptfile = file(os.path.join(controldir, script), 'w') scriptfile = open(os.path.join(controldir, script), 'w')
except OSError: except OSError:
bb.utils.unlockfile(lf) bb.utils.unlockfile(lf)
raise bb.build.FuncFailed("unable to open %s script file for writing." % script) 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) conffiles_str = localdata.getVar("CONFFILES", True)
if conffiles_str: if conffiles_str:
try: try:
conffiles = file(os.path.join(controldir, 'conffiles'), 'w') conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
except OSError: except OSError:
bb.utils.unlockfile(lf) bb.utils.unlockfile(lf)
raise bb.build.FuncFailed("unable to open conffiles for writing.") raise bb.build.FuncFailed("unable to open conffiles for writing.")

View File

@ -504,8 +504,7 @@ def write_rpm_perfiledata(srcname, d):
outdepends = workdir + "/" + srcname + ".requires" outdepends = workdir + "/" + srcname + ".requires"
try: try:
from __builtin__ import file dependsfile = open(outdepends, 'w')
dependsfile = file(outdepends, 'w')
except OSError: except OSError:
raise bb.build.FuncFailed("unable to open spec file for writing.") 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" outprovides = workdir + "/" + srcname + ".provides"
try: try:
from __builtin__ import file providesfile = open(outprovides, 'w')
providesfile = file(outprovides, 'w')
except OSError: except OSError:
raise bb.build.FuncFailed("unable to open spec file for writing.") raise bb.build.FuncFailed("unable to open spec file for writing.")
@ -1005,8 +1003,7 @@ python write_specfile () {
# Write the SPEC file # Write the SPEC file
try: try:
from __builtin__ import file specfile = open(outspecfile, 'w')
specfile = file(outspecfile, 'w')
except OSError: except OSError:
raise bb.build.FuncFailed("unable to open spec file for writing.") raise bb.build.FuncFailed("unable to open spec file for writing.")

View File

@ -561,14 +561,14 @@ def check_sanity(sanity_data):
last_sstate_dir = "" last_sstate_dir = ""
sanityverfile = 'conf/sanity_info' sanityverfile = 'conf/sanity_info'
if os.path.exists(sanityverfile): if os.path.exists(sanityverfile):
f = open(sanityverfile, 'r') with open(sanityverfile, 'r') as f:
for line in f: for line in f:
if line.startswith('SANITY_VERSION'): if line.startswith('SANITY_VERSION'):
last_sanity_version = int(line.split()[1]) last_sanity_version = int(line.split()[1])
if line.startswith('TMPDIR'): if line.startswith('TMPDIR'):
last_tmpdir = line.split()[1] last_tmpdir = line.split()[1]
if line.startswith('SSTATE_DIR'): if line.startswith('SSTATE_DIR'):
last_sstate_dir = line.split()[1] last_sstate_dir = line.split()[1]
sanity_version = int(sanity_data.getVar('SANITY_VERSION', True) or 1) sanity_version = int(sanity_data.getVar('SANITY_VERSION', True) or 1)
network_error = False network_error = False
@ -584,25 +584,24 @@ def check_sanity(sanity_data):
if last_sstate_dir != sstate_dir: if last_sstate_dir != sstate_dir:
messages = messages + check_sanity_sstate_dir_change(sstate_dir, sanity_data) messages = messages + check_sanity_sstate_dir_change(sstate_dir, sanity_data)
if os.path.exists("conf") and not messages: if os.path.exists("conf") and not messages:
f = open(sanityverfile, 'w') with open(sanityverfile, 'w') as f:
f.write("SANITY_VERSION %s\n" % sanity_version) f.write("SANITY_VERSION %s\n" % sanity_version)
f.write("TMPDIR %s\n" % tmpdir) f.write("TMPDIR %s\n" % tmpdir)
f.write("SSTATE_DIR %s\n" % sstate_dir) f.write("SSTATE_DIR %s\n" % sstate_dir)
# #
# Check that TMPDIR hasn't changed location since the last time we were run # Check that TMPDIR hasn't changed location since the last time we were run
# #
checkfile = os.path.join(tmpdir, "saved_tmpdir") checkfile = os.path.join(tmpdir, "saved_tmpdir")
if os.path.exists(checkfile): if os.path.exists(checkfile):
f = open(checkfile, "r") with open(checkfile, "r") as f:
saved_tmpdir = f.read().strip() saved_tmpdir = f.read().strip()
if (saved_tmpdir != tmpdir): 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 messages = messages + "Error, TMPDIR has changed location. You need to either move it back to %s or rebuild\n" % saved_tmpdir
else: else:
bb.utils.mkdirhier(tmpdir) bb.utils.mkdirhier(tmpdir)
f = open(checkfile, "w") with open(checkfile, "w") as f:
f.write(tmpdir) f.write(tmpdir)
f.close()
# #
# Check the 'ABI' of TMPDIR # Check the 'ABI' of TMPDIR
@ -610,33 +609,32 @@ def check_sanity(sanity_data):
current_abi = sanity_data.getVar('OELAYOUT_ABI', True) current_abi = sanity_data.getVar('OELAYOUT_ABI', True)
abifile = sanity_data.getVar('SANITY_ABIFILE', True) abifile = sanity_data.getVar('SANITY_ABIFILE', True)
if os.path.exists(abifile): if os.path.exists(abifile):
f = open(abifile, "r") with open(abifile, "r") as f:
abi = f.read().strip() abi = f.read().strip()
if not abi.isdigit(): if not abi.isdigit():
f = open(abifile, "w") with open(abifile, "w") as f:
f.write(current_abi) f.write(current_abi)
elif abi == "2" and current_abi == "3": elif abi == "2" and current_abi == "3":
bb.note("Converting staging from layout version 2 to layout version 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("mv ${TMPDIR}/staging ${TMPDIR}/sysroots"), shell=True)
subprocess.call(sanity_data.expand("ln -s sysroots ${TMPDIR}/staging"), 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) 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") with open(abifile, "w") as f:
f.write(current_abi) f.write(current_abi)
elif abi == "3" and current_abi == "4": elif abi == "3" and current_abi == "4":
bb.note("Converting staging layout from version 3 to layout version 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}")): 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("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) subprocess.call(sanity_data.expand("ln -s ${STAGING_BINDIR_CROSS} ${STAGING_DIR_NATIVE}${bindir_native}/${MULTIMACH_HOST_SYS}"), shell=True)
with open(abifile, "w") as f:
f = open(abifile, "w") f.write(current_abi)
f.write(current_abi)
elif abi == "4": 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" 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": elif abi == "5" and current_abi == "6":
bb.note("Converting staging layout from version 5 to layout version 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) subprocess.call(sanity_data.expand("mv ${TMPDIR}/pstagelogs ${SSTATE_MANIFESTS}"), shell=True)
f = open(abifile, "w") with open(abifile, "w") as f:
f.write(current_abi) f.write(current_abi)
elif abi == "7" and current_abi == "8": 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" 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"): 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. # 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) 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: else:
f = open(abifile, "w") with open(abifile, "w") as f:
f.write(current_abi) f.write(current_abi)
f.close()
oeroot = sanity_data.getVar('COREBASE') oeroot = sanity_data.getVar('COREBASE')
if oeroot.find ('+') != -1: if oeroot.find ('+') != -1:

View File

@ -12,7 +12,7 @@ def read_pkgdatafile(fn):
if os.access(fn, os.R_OK): if os.access(fn, os.R_OK):
import re import re
f = file(fn, 'r') f = open(fn, 'r')
lines = f.readlines() lines = f.readlines()
f.close() f.close()
r = re.compile("([^:]+):\s*(.*)") r = re.compile("([^:]+):\s*(.*)")

View File

@ -7,11 +7,13 @@ except ImportError:
def read_file(filename): def read_file(filename):
try: try:
f = file( filename, "r" ) f = open( filename, "r" )
except IOError as reason: except IOError as reason:
return "" # WARNING: can't raise an error now because of the new RDEPENDS handling. This is a bit ugly. :M: return "" # WARNING: can't raise an error now because of the new RDEPENDS handling. This is a bit ugly. :M:
else: else:
return f.read().strip() data = f.read().strip()
f.close()
return data
return None return None
def ifelse(condition, iftrue = True, iffalse = False): def ifelse(condition, iftrue = True, iffalse = False):

View File

@ -262,6 +262,7 @@ python do_package_prepend () {
d.appendVar('ALTERNATIVE_%s' % (pn), ' ' + alt_name) d.appendVar('ALTERNATIVE_%s' % (pn), ' ' + alt_name)
d.setVarFlag('ALTERNATIVE_LINK_NAME', alt_name, alt_link_name) d.setVarFlag('ALTERNATIVE_LINK_NAME', alt_name, alt_link_name)
f.close()
} }
pkg_postinst_${PN} () { pkg_postinst_${PN} () {