From: Harald Seiler <hws@denx.de>
To: isar-users@googlegroups.com
Subject: [PATCH 2/4] Use modern python formatting
Date: Tue, 12 Feb 2019 10:20:44 +0100 [thread overview]
Message-ID: <20190212092046.6508-3-hws@denx.de> (raw)
In-Reply-To: <20190212092046.6508-1-hws@denx.de>
The use of % for formatting is discouraged because it has side-
effects that are not immediately obvious. This commit refactors
all uses of % to a better formatting style.
As f-Strings are not availible in in some python versions we support
(<3.6) the formatter of choice is .format() or manual concatenation
in cases where it is more concise.
This commit additionally refactors the showdata and listtasks tasks
to make better use of the new formatter code.
Signed-off-by: Harald Seiler <hws@denx.de>
---
meta/classes/base.bbclass | 37 +++++++++++++++-------
meta/classes/wic-img.bbclass | 21 ++++++++----
.../recipes-core/isar-bootstrap/isar-bootstrap.inc | 12 +++----
3 files changed, 46 insertions(+), 24 deletions(-)
diff --git a/meta/classes/base.bbclass b/meta/classes/base.bbclass
index 6fcf452..50c4fbf 100644
--- a/meta/classes/base.bbclass
+++ b/meta/classes/base.bbclass
@@ -32,27 +32,40 @@ do_showdata[nostamp] = "1"
python do_showdata() {
for e in d.keys():
if d.getVarFlag(e, 'python'):
- bb.plain("\npython %s () {\n%s}\n" % (e, d.getVar(e, True)))
+ code = d.getVar(e, True)
+ if code.startswith("def"):
+ bb.plain("\n" + code + "\n")
+ else:
+ bb.plain(
+ "\npython {name} () {{\n{code}}}\n".format(
+ name=e,
+ code=code,
+ )
+ )
}
# Derived from Open Embedded: openembedded-core/meta/classes/utility-tasks.bbclass
addtask listtasks
do_listtasks[nostamp] = "1"
python do_listtasks() {
- taskdescs = {}
+ tasks = {}
maxlen = 0
for e in d.keys():
if d.getVarFlag(e, 'task'):
maxlen = max(maxlen, len(e))
if e.endswith('_setscene'):
- desc = "%s (setscene version)" % (d.getVarFlag(e[:-9], 'doc') or '')
+ tasks[e] = (d.getVarFlag(e[:-9], 'doc') or '') + " (setscene version)"
else:
- desc = d.getVarFlag(e, 'doc') or ''
- taskdescs[e] = desc
-
- tasks = sorted(taskdescs.keys())
- for taskname in tasks:
- bb.plain("%s %s" % (taskname.ljust(maxlen), taskdescs[taskname]))
+ tasks[e] = d.getVarFlag(e, 'doc') or ''
+
+ for name, desc in sorted(tasks.items()):
+ bb.plain(
+ "{0:{len}} {1}".format(
+ name,
+ desc,
+ len=maxlen,
+ )
+ )
}
root_cleandirs() {
@@ -81,8 +94,10 @@ python() {
for i in rcleandirs:
if not i.startswith(tmpdir):
- bb.fatal("root_cleandirs entry %s is not contained in "
- "TMPDIR %s" % (i, tmpdir))
+ bb.fatal(
+ "root_cleandirs entry {} is not contained in TMPDIR {}"
+ .format(i, tmpdir)
+ )
ws = re.match("^\s*", d.getVar(e, False)).group()
if flags.get('python'):
diff --git a/meta/classes/wic-img.bbclass b/meta/classes/wic-img.bbclass
index 76602d8..9cb869c 100644
--- a/meta/classes/wic-img.bbclass
+++ b/meta/classes/wic-img.bbclass
@@ -5,6 +5,8 @@
#
python () {
+ import itertools
+
wks_full_path = None
wks_file = d.getVar('WKS_FILE', True)
@@ -18,14 +20,21 @@ python () {
wks_full_path = wks_file
else:
bbpaths = d.getVar('BBPATH', True).split(':')
+ corebase_paths = bbpaths
+
corebase = d.getVar('COREBASE', True)
- search_path = ':'.join('%s/wic' % p for p in bbpaths) + ':' + \
- ':'.join('%s/scripts/lib/wic/canned-wks' % l \
- for l in (bbpaths + [corebase]))
+ if corebase is not None:
+ corebase_paths.append(corebase)
+
+ search_path = ":".join(itertools.chain(
+ (p + "/wic" for p in bbpaths),
+ (l + "/scripts/lib/wic/canned-wks"
+ for l in (corebase_paths)),
+ ))
wks_full_path = bb.utils.which(search_path, wks_file)
if not wks_full_path:
- bb.fatal("WKS_FILE '%s' not found" % wks_file)
+ bb.fatal("WKS_FILE '{}' not found".format(wks_file))
d.setVar('WKS_FULL_PATH', wks_full_path)
}
@@ -68,12 +77,12 @@ python do_rootfs_wicenv () {
for var in wicvars.split():
value = d.getVar(var, True)
if value:
- envf.write('%s="%s"\n' % (var, value.strip()))
+ envf.write('{}="{}"\n'.format(var, value.strip()))
# this part is stolen from OE ./meta/recipes-core/meta/wic-tools.bb
with open(os.path.join(outdir, "wic-tools.env"), 'w') as envf:
for var in ('RECIPE_SYSROOT_NATIVE', 'STAGING_DATADIR', 'STAGING_LIBDIR'):
- envf.write('%s="%s"\n' % (var, d.getVar(var, True).strip()))
+ envf.write('{}="{}"\n'.format(var, d.getVar(var, True).strip()))
}
diff --git a/meta/recipes-core/isar-bootstrap/isar-bootstrap.inc b/meta/recipes-core/isar-bootstrap/isar-bootstrap.inc
index 234d339..5114714 100644
--- a/meta/recipes-core/isar-bootstrap/isar-bootstrap.inc
+++ b/meta/recipes-core/isar-bootstrap/isar-bootstrap.inc
@@ -40,18 +40,16 @@ python () {
d.setVar("DEBOOTSTRAP_KEYRING", "--keyring ${APTKEYRING}")
for key in distro_apt_keys.split():
url = urlparse(key)
- filename = ''.join([wd, url.path])
- d.appendVar("SRC_URI", " %s" % key)
- d.appendVar("APTKEYFILES", " %s" % filename)
+ d.appendVar("SRC_URI", " " + key)
+ d.appendVar("APTKEYFILES", " " + wd + url.path)
if bb.utils.to_boolean(d.getVar('ISAR_USE_CACHED_BASE_REPO')):
own_pub_key = d.getVar("BASE_REPO_KEY", False)
if own_pub_key:
d.setVar("DEBOOTSTRAP_KEYRING", "--keyring ${APTKEYRING}")
for key in own_pub_key.split():
url = urlparse(key)
- filename = ''.join([wd, url.path])
- d.appendVar("SRC_URI", " %s" % key)
- d.appendVar("APTKEYFILES", " %s" % filename)
+ d.appendVar("SRC_URI", " " + key)
+ d.appendVar("APTKEYFILES", " " + wd + url.path)
}
def aggregate_files(d, file_list, file_out):
@@ -170,7 +168,7 @@ def get_distro_suite(d, is_host):
def get_distro_components_argument(d, is_host):
components = get_distro_primary_source_entry(d, is_host)[2]
if components and components.strip():
- return "--components=%s" % ",".join(components.split())
+ return "--components=" + ",".join(components.split())
else:
return ""
--
2.14.1
next prev parent reply other threads:[~2019-02-12 9:20 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-12 9:20 [PATCH 0/4] Python refactoring Harald Seiler
2019-02-12 9:20 ` [PATCH 1/4] Remove all uses of subprocess.call(shell=True) Harald Seiler
2019-02-12 9:51 ` Claudius Heine
2019-02-12 9:20 ` Harald Seiler [this message]
2019-02-12 9:20 ` [PATCH 3/4] image: Remove recursion in get_image_name Harald Seiler
2019-02-12 9:20 ` [PATCH 4/4] wic: Refactor fakeroot script Harald Seiler
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=20190212092046.6508-3-hws@denx.de \
--to=hws@denx.de \
--cc=isar-users@googlegroups.com \
/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