oeqa/selftest/recipetool: Use with to control file handle lifetime
PATCH 1/9 oeqa/selftest/recipetool: Use with to control file handle lifetime
--- meta/lib/oeqa/selftest/cases/recipetool.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
meta/lib/oeqa/selftest/cases/recipetool.py +3 -1
@@ -685,7 +685,9 @@ class RecipetoolAppendsrcTests(RecipetoolAppendsrcBase): self._test_appendsrcfile(testrecipe, filepath, srcdir=subdir) bitbake('%s:do_unpack' % testrecipe) - self.assertEqual(open(self.testfile, 'r').read(), open(os.path.join(srcdir, filepath), 'r').read()) + with open(self.testfile, 'r') as testfile: + with open(os.path.join(srcdir, filepath), 'r') as makefilein: + self.assertEqual(testfile.read(), makefilein.read()) def test_recipetool_appendsrcfiles_basic(self, destdir=None): newfiles = [self.testfile]
Signature · 1 line
2.11.0
PATCH 2/9 oe.types.path: Use with to control file handle lifetime
--- meta/lib/oe/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
meta/lib/oe/types.py +2 -2
@@ -154,7 +154,8 @@ def path(value, relativeto='', normalize='true', mustexist='false'): if boolean(mustexist): try: - open(value, 'r') + with open(value, 'r'): + pass except IOError as exc: if exc.errno == errno.ENOENT: raise ValueError("{0}: {1}".format(value, os.strerror(errno.ENOENT)))
@@ -183,4 +184,3 @@ def qemu_use_kvm(kvm, target_arch): elif build_arch == target_arch: use_kvm = True return use_kvm -
Signature · 1 line
2.11.0
PATCH 3/9 lib/oe/packagedata: Use with to control file handle lifetime
In reply to Ola x Nilsson--- meta/lib/oe/packagedata.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
meta/lib/oe/packagedata.py +2 -3
@@ -17,9 +17,8 @@ def read_pkgdatafile(fn): if os.access(fn, os.R_OK): import re - f = open(fn, 'r') - lines = f.readlines() - f.close() + with open(fn, 'r') as f: + lines = f.readlines() r = re.compile("([^:]+):\s*(.*)") for l in lines: m = r.match(l)
Signature · 1 line
2.11.0
PATCH 4/9 lib/oe/package_manager: Use with to control file handle lifetime
In reply to Ola x Nilsson--- meta/lib/oe/package_manager.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
meta/lib/oe/package_manager.py +4 -4
@@ -298,7 +298,7 @@ class DpkgIndexer(Indexer): release.write("Label: %s\n" % arch) cmd += "PSEUDO_UNLOAD=1 %s release . >> Release" % apt_ftparchive - + index_cmds.append(cmd) deb_dirs_found = True
@@ -655,7 +655,7 @@ def create_packages_dir(d, subrepo_dir, deploydir, taskname, filterbydependencie pn = d.getVar("PN") seendirs = set() multilibs = {} - + bb.utils.remove(subrepo_dir, recurse=True) bb.utils.mkdirhier(subrepo_dir)
@@ -1006,8 +1006,8 @@ class RpmPM(PackageManager): def load_old_install_solution(self): if not os.path.exists(self.solution_manifest): return [] - - return open(self.solution_manifest, 'r').read().split() + with open(self.solution_manifest, 'r') as fd: + return fd.read().split() def _script_num_prefix(self, path): files = os.listdir(path)
Signature · 1 line
2.11.0
PATCH 5/9 tinderbox.bbclass: Use with to control file handle lifetime
In reply to Ola x Nilsson--- meta/classes/tinderclient.bbclass | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
meta/classes/tinderclient.bbclass +3 -3
@@ -82,8 +82,8 @@ def tinder_format_http_post(d,status,log): # we only need on build_status.pl but sending it # always does not hurt try: - f = open(d.getVar('TMPDIR')+'/tinder-machine.id', 'r') - id = f.read() + with open(d.getVar('TMPDIR') + '/tinder-machine.id') as f: + id = f.read() variables['machine_id'] = id except: pass
@@ -231,7 +231,7 @@ def tinder_tinder_start(d, event): config = tinder_print_info(d) #env = tinder_print_env() time_end = tinder_time_string() - packages = " ".join( event.getPkgs() ) + packages = " ".join( event.getPkgs() ) output = [] output.append( "---> TINDERBOX PRINTING CONFIGURATION %(time_start)s" )
Signature · 1 line
2.11.0
PATCH 6/9 report-error.bbclass: Use with to control file handle lifetime
In reply to Ola x Nilsson--- meta/classes/report-error.bbclass | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
meta/classes/report-error.bbclass +2 -6
@@ -78,19 +78,15 @@ python errorreport_handler () { taskdata['task'] = task if log: try: - logFile = codecs.open(log, 'r', 'utf-8') - logdata = logFile.read() - + with codecs.open(log, encoding='utf-8') as logFile: + logdata = logFile.read() # Replace host-specific paths so the logs are cleaner for d in ("TOPDIR", "TMPDIR"): s = e.data.getVar(d) if s: logdata = logdata.replace(s, d) - - logFile.close() except: logdata = "Unable to read log file" - else: logdata = "No Log"
Signature · 1 line
2.11.0
PATCH 7/9 package.bbclass: Use with to manage file handle lifetimes
In reply to Ola x Nilsson--- meta/classes/package.bbclass | 76 ++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 42 deletions(-)
meta/classes/package.bbclass +34 -42
@@ -826,8 +826,9 @@ python fixup_perms () { # Now we actually load from the configuration files for conf in get_fs_perms_list(d).split(): - if os.path.exists(conf): - f = open(conf) + if not os.path.exists(conf): + continue + with open(conf) as f: for line in f: if line.startswith('#'): continue
@@ -848,7 +849,6 @@ python fixup_perms () { fs_perms_table[entry.path] = entry if entry.path in fs_link_table: fs_link_table.pop(entry.path) - f.close() # Debug -- list out in-memory table #for dir in fs_perms_table:
@@ -1424,10 +1424,9 @@ fi pkgdest = d.getVar('PKGDEST') pkgdatadir = d.getVar('PKGDESTWORK') - data_file = pkgdatadir + d.expand("/${PN}" ) - f = open(data_file, 'w') - f.write("PACKAGES: %s\n" % packages) - f.close() + data_file = pkgdatadir + d.expand("/${PN}") + with open(data_file, 'w') as fd: + fd.write("PACKAGES: %s\n" % packages) pn = d.getVar('PN') global_variants = (d.getVar('MULTILIB_GLOBAL_VARIANTS') or "").split()
@@ -1778,21 +1777,20 @@ python package_do_shlibs() { bb.note("Renaming %s to %s" % (old, new)) os.rename(old, new) pkgfiles[pkg].remove(old) - + shlibs_file = os.path.join(shlibswork_dir, pkg + ".list") if len(sonames): - fd = open(shlibs_file, 'w') - for s in sonames: - if s[0] in shlib_provider and s[1] in shlib_provider[s[0]]: - (old_pkg, old_pkgver) = shlib_provider[s[0]][s[1]] - if old_pkg != pkg: - bb.warn('%s-%s was registered as shlib provider for %s, changing it to %s-%s because it was built later' % (old_pkg, old_pkgver, s[0], pkg, pkgver)) - bb.debug(1, 'registering %s-%s as shlib provider for %s' % (pkg, pkgver, s[0])) - fd.write(s[0] + ':' + s[1] + ':' + s[2] + '\n') - if s[0] not in shlib_provider: - shlib_provider[s[0]] = {} - shlib_provider[s[0]][s[1]] = (pkg, pkgver) - fd.close() + with open(shlibs_file, 'w') as fd: + for s in sonames: + if s[0] in shlib_provider and s[1] in shlib_provider[s[0]]: + (old_pkg, old_pkgver) = shlib_provider[s[0]][s[1]] + if old_pkg != pkg: + bb.warn('%s-%s was registered as shlib provider for %s, changing it to %s-%s because it was built later' % (old_pkg, old_pkgver, s[0], pkg, pkgver)) + bb.debug(1, 'registering %s-%s as shlib provider for %s' % (pkg, pkgver, s[0])) + fd.write(s[0] + ':' + s[1] + ':' + s[2] + '\n') + if s[0] not in shlib_provider: + shlib_provider[s[0]] = {} + shlib_provider[s[0]][s[1]] = (pkg, pkgver) if needs_ldconfig and use_ldconfig: bb.debug(1, 'adding ldconfig call to postinst for %s' % pkg) postinst = d.getVar('pkg_postinst_%s' % pkg)
@@ -1864,11 +1862,10 @@ python package_do_shlibs() { deps_file = os.path.join(pkgdest, pkg + ".shlibdeps") if os.path.exists(deps_file): os.remove(deps_file) - if len(deps): - fd = open(deps_file, 'w') - for dep in sorted(deps): - fd.write(dep + '\n') - fd.close() + if deps: + with open(deps_file, 'w') as fd: + for dep in sorted(deps): + fd.write(dep + '\n') } python package_do_pkgconfig () {
@@ -1898,9 +1895,8 @@ python package_do_pkgconfig () { pkgconfig_provided[pkg].append(name) if not os.access(file, os.R_OK): continue - f = open(file, 'r') - lines = f.readlines() - f.close() + with open(file, 'r') as f: + lines = f.readlines() for l in lines: m = var_re.match(l) if m:
@@ -1918,10 +1914,9 @@ python package_do_pkgconfig () { for pkg in packages.split(): pkgs_file = os.path.join(shlibswork_dir, pkg + ".pclist") if pkgconfig_provided[pkg] != []: - f = open(pkgs_file, 'w') - for p in pkgconfig_provided[pkg]: - f.write('%s\n' % p) - f.close() + with open(pkgs_file, 'w') as f: + for p in pkgconfig_provided[pkg]: + f.write('%s\n' % p) # Go from least to most specific since the last one found wins for dir in reversed(shlibs_dirs):
@@ -1931,9 +1926,8 @@ python package_do_pkgconfig () { m = re.match(r'^(.*)\.pclist$', file) if m: pkg = m.group(1) - fd = open(os.path.join(dir, file)) - lines = fd.readlines() - fd.close() + with open(os.path.join(dir, file)) as fd: + lines = fd.readlines() pkgconfig_provided[pkg] = [] for l in lines: pkgconfig_provided[pkg].append(l.rstrip())
@@ -1951,10 +1945,9 @@ python package_do_pkgconfig () { bb.note("couldn't find pkgconfig module '%s' in any package" % n) deps_file = os.path.join(pkgdest, pkg + ".pcdeps") if len(deps): - fd = open(deps_file, 'w') - for dep in deps: - fd.write(dep + '\n') - fd.close() + with open(deps_file, 'w') as fd: + for dep in deps: + fd.write(dep + '\n') } def read_libdep_files(d):
@@ -1965,9 +1958,8 @@ 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 = open(depsfile) - lines = fd.readlines() - fd.close() + with open(depsfile) as fd: + lines = fd.readlines() for l in lines: l.rstrip() deps = bb.utils.explode_dep_versions2(l)
Signature · 1 line
2.11.0
PATCH 8/9 devtool-source.bbclass: Use with to manage file handle lifetime
In reply to Ola x NilssonReplace copy-and-if with a filtering list comprehension.
--- meta/classes/devtool-source.bbclass | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
meta/classes/devtool-source.bbclass +3 -5
@@ -97,17 +97,15 @@ python devtool_post_unpack() { local_files = oe.recipeutils.get_recipe_local_files(d) if is_kernel_yocto: - for key in local_files.copy(): - if key.endswith('scc'): - sccfile = open(local_files[key], 'r') + for key in [f for f in local_files if f.endswith('scc')]: + with open(local_files[key], 'r') as sccfile: for l in sccfile: line = l.split() if line and line[0] in ('kconf', 'patch'): cfg = os.path.join(os.path.dirname(local_files[key]), line[-1]) - if not cfg in local_files.values(): + if cfg not in local_files.values(): local_files[line[-1]] = cfg shutil.copy2(cfg, workdir) - sccfile.close() # Ignore local files with subdir={BP} srcabspath = os.path.abspath(srcsubdir)
Signature · 1 line
2.11.0
PATCH 9/9 libc-package.bbclass: Use with to manage filehandle in do_spit_gconvs
In reply to Ola x NilssonTweak the write loop slightly to avoid dict lookups that can easily be done in the for loop.
--- meta/classes/libc-package.bbclass | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
meta/classes/libc-package.bbclass +7 -8
@@ -346,14 +346,13 @@ python package_do_split_gconvs () { if use_bin == "compile": makefile = oe.path.join(d.getVar("WORKDIR"), "locale-tree", "Makefile") - m = open(makefile, "w") - m.write("all: %s\n\n" % " ".join(commands.keys())) - total = len(commands) - for i, cmd in enumerate(commands): - m.write(cmd + ":\n") - m.write("\t@echo 'Progress %d/%d'\n" % (i, total)) - m.write("\t" + commands[cmd] + "\n\n") - m.close() + with open(makefile, "w") as m: + m.write("all: %s\n\n" % " ".join(commands.keys())) + total = len(commands) + for i, (maketarget, makerecipe) in enumerate(commands.items()): + m.write(maketarget + ":\n") + m.write("\t@echo 'Progress %d/%d'\n" % (i, total)) + m.write("\t" + makerecipe + "\n\n") d.setVar("EXTRA_OEMAKE", "-C %s ${PARALLEL_MAKE}" % (os.path.dirname(makefile))) d.setVarFlag("oe_runmake", "progress", "outof:Progress\s(\d+)/(\d+)") bb.note("Executing binary locale generation makefile")
Signature · 1 line
2.11.0