From: Tzung-Bi Shih <tzungbi@kernel.org>
To: bleung@chromium.org, groeck@chromium.org
Cc: chrome-platform@lists.linux.dev, tzungbi@kernel.org,
guillaume.tucker@collabora.com, denys.f@collabora.com,
ricardo.canuelo@collabora.com
Subject: [PATCH 13/14] treewide: don't use "+" operator for constructing paths
Date: Mon, 13 Mar 2023 17:44:30 +0800 [thread overview]
Message-ID: <20230313094431.507952-14-tzungbi@kernel.org> (raw)
In-Reply-To: <20230313094431.507952-1-tzungbi@kernel.org>
Don't use "+" operator for constructing paths. Use os.path.join()
instead.
Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
cros/helpers/mcu.py | 19 ++++++++++++-------
cros/helpers/sysfs.py | 6 ++++--
cros/tests/cros_ec_accel.py | 10 +++++-----
cros/tests/cros_ec_extcon.py | 11 ++++++-----
cros/tests/cros_ec_rtc.py | 5 +++--
5 files changed, 30 insertions(+), 21 deletions(-)
diff --git a/cros/helpers/mcu.py b/cros/helpers/mcu.py
index 6c12affd4073..e2d2db0ef1ed 100644
--- a/cros/helpers/mcu.py
+++ b/cros/helpers/mcu.py
@@ -132,7 +132,8 @@ def check_mcu_abi(s, name):
""" Checks that the MCU character device exists in /dev and then verifies
the standard MCU ABI in /sys/class/chromeos.
"""
- if not os.path.exists("/dev/" + name):
+ dev = os.path.join("/dev", name)
+ if not os.path.exists(dev):
s.skipTest("MCU " + name + " not supported, skipping")
files = ["flashinfo", "reboot", "version"]
sysfs_check_attributes_exists(
@@ -142,7 +143,8 @@ def check_mcu_abi(s, name):
def mcu_hello(s, name):
""" Checks basic comunication with MCU. """
- if not os.path.exists("/dev/" + name):
+ dev = os.path.join("/dev", name)
+ if not os.path.exists(dev):
s.skipTest("MCU " + name + " not present, skipping")
param = ec_params_hello()
param.in_data = 0xA0B0C0D0 # magic number that the EC expects on HELLO
@@ -156,7 +158,7 @@ def mcu_hello(s, name):
cmd.outsize = sizeof(response)
memmove(addressof(cmd.data), addressof(param), cmd.insize)
- with open("/dev/" + name) as fh:
+ with open(dev) as fh:
fcntl.ioctl(fh, EC_DEV_IOCXCMD, cmd)
memmove(addressof(response), addressof(cmd.data), cmd.outsize)
@@ -165,7 +167,8 @@ def mcu_hello(s, name):
s.assertEqual(response.out_data, 0xA1B2C3D4)
def mcu_get_version(name):
- if os.path.exists("/dev/" + name):
+ dev = os.path.join("/dev", name)
+ if os.path.exists(dev):
response = ec_response_get_version()
cmd = cros_ec_command()
@@ -174,7 +177,7 @@ def mcu_get_version(name):
cmd.insize = sizeof(response)
cmd.outsize = 0
- with open("/dev/" + name) as fh:
+ with open(dev) as fh:
fcntl.ioctl(fh, EC_DEV_IOCXCMD, cmd)
memmove(addressof(response), addressof(cmd.data), cmd.insize)
@@ -188,13 +191,15 @@ def mcu_reboot(name):
cmd.insize = 0
cmd.outsize = 0
try:
- with open("/dev/" + name) as fh:
+ dev = os.path.join("/dev", name)
+ with open(dev) as fh:
fcntl.ioctl(fh, EC_DEV_IOCXCMD, cmd)
except IOError:
pass
def check_mcu_reboot_rw(s, name):
- if not os.path.exists("/dev/" + name):
+ dev = os.path.join("/dev", name)
+ if not os.path.exists(dev):
s.skipTest("cros_fp not present, skipping")
mcu_reboot(name)
response = mcu_get_version(name)
diff --git a/cros/helpers/sysfs.py b/cros/helpers/sysfs.py
index 4d3b6735ef45..882bc4d14243 100755
--- a/cros/helpers/sysfs.py
+++ b/cros/helpers/sysfs.py
@@ -20,7 +20,8 @@ def sysfs_check_attributes_exists(s, path, name, files, check_devtype):
try:
for devname in os.listdir(path):
if check_devtype:
- with open(path + "/" + devname + "/name") as fh:
+ p = os.path.join(path, devname, "name")
+ with open(p) as fh:
devtype = fh.read()
if not devtype.startswith(name):
continue
@@ -29,7 +30,8 @@ def sysfs_check_attributes_exists(s, path, name, files, check_devtype):
continue
match += 1
for filename in files:
- s.assertEqual(os.path.exists(path + "/" + devname + "/" + filename), 1)
+ p = os.path.join(path, devname, filename)
+ s.assertEqual(os.path.exists(p), 1)
except IOError as e:
s.skipTest("Exception occured: {0}, skipping".format(e.strerror))
if match == 0:
diff --git a/cros/tests/cros_ec_accel.py b/cros/tests/cros_ec_accel.py
index 3d6a54acac16..15a92b7dd35d 100755
--- a/cros/tests/cros_ec_accel.py
+++ b/cros/tests/cros_ec_accel.py
@@ -56,17 +56,17 @@ class TestCrosECAccel(unittest.TestCase):
match = 0
try:
for devname in os.listdir("/sys/bus/iio/devices"):
- base_path = "/sys/bus/iio/devices/" + devname + "/"
- with open(base_path + "name") as fh:
+ base_path = os.path.join("/sys/bus/iio/devices", devname)
+ with open(os.path.join(base_path, "name")) as fh:
devtype = fh.read()
if devtype.startswith("cros-ec-accel"):
- location = read_file(base_path + "location")
- accel_scale = float(read_file(base_path + "scale"))
+ location = read_file(os.path.join(base_path, "location"))
+ accel_scale = float(read_file(os.path.join(base_path, "scale")))
exp = ACCEL_1G_IN_MS2
err = exp * ACCEL_MAG_VALID_OFFSET
mag = 0
for axis in ["x", "y", "z"]:
- axis_path = base_path + "in_accel_" + axis + "_raw"
+ axis_path = os.path.join(base_path, "in_accel_" + axis + "_raw")
value = int(read_file(axis_path))
value *= accel_scale
mag += value * value
diff --git a/cros/tests/cros_ec_extcon.py b/cros/tests/cros_ec_extcon.py
index b2864b52f4c0..8c496b868bb3 100755
--- a/cros/tests/cros_ec_extcon.py
+++ b/cros/tests/cros_ec_extcon.py
@@ -11,22 +11,23 @@ class TestCrosECextcon(unittest.TestCase):
match = 0
try:
for devname in os.listdir("/sys/class/extcon"):
- devtype = read_file("/sys/class/extcon/" + devname + "/name")
+ d = os.path.join("/sys/class/extcon", devname)
+ devtype = read_file(os.path.join(d, "name"))
if ".spi:ec@0:extcon@" in devtype:
self.assertEqual(
- os.path.exists("/sys/class/extcon/" + devname + "/state"), 1
+ os.path.exists(os.path.join(d, "state")), 1
)
- for cable in os.listdir("/sys/class/extcon/" + devname):
+ for cable in os.listdir(d):
if cable.startswith("cable"):
self.assertEqual(
os.path.exists(
- "/sys/class/extcon/" + devname + "/" + cable + "/name"
+ os.path.join(d, cable, "name")
),
1,
)
self.assertEqual(
os.path.exists(
- "/sys/class/extcon/" + devname + "/" + cable + "/state"
+ os.path.join(d, cable, "state")
),
1,
)
diff --git a/cros/tests/cros_ec_rtc.py b/cros/tests/cros_ec_rtc.py
index e59fa383659d..2d1ef3d58d85 100755
--- a/cros/tests/cros_ec_rtc.py
+++ b/cros/tests/cros_ec_rtc.py
@@ -14,7 +14,8 @@ class TestCrosECRTC(unittest.TestCase):
match = 0
try:
for devname in os.listdir("/sys/class/rtc"):
- with open("/sys/class/rtc/" + devname + "/name") as fh:
+ d = os.path.join("/sys/class/rtc", devname)
+ with open(os.path.join(d, "name")) as fh:
devtype = fh.read()
if devtype.startswith("cros-ec-rtc"):
files = [
@@ -28,7 +29,7 @@ class TestCrosECRTC(unittest.TestCase):
match += 1
for filename in files:
self.assertEqual(
- os.path.exists("/sys/class/rtc/" + devname + "/" + filename), 1
+ os.path.exists(os.path.join(d, filename)), 1
)
except IOError as e:
self.skipTest("Exception occured: {0}, skipping".format(e.strerror))
--
2.40.0.rc1.284.g88254d51c5-goog
next prev parent reply other threads:[~2023-03-13 9:44 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-13 9:44 [PATCH 00/14] cros-ec-tests: fix some exceptions and clean-ups Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 01/14] lava_runner: rename to LavaTestResult Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 02/14] lava_runner: use TextTestRunner Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 03/14] lava_runner: use TextTestResult Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 04/14] lava_runner: respect to verbosity flag from unittest framework Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 05/14] helpers/sysfs: fix NameError Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 06/14] helpers/mcu: fix ResourceWarning on /dev/cros_ec Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 07/14] cros_ec_pwm: fix ResourceWarning on /sys/kernel/debug/pwm Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 08/14] treewide: use context manager on file objects Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 09/14] cros_ec_pwm: use RE to search EC backlight PWM Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 10/14] helpers/mcu: fix packet too long error Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 11/14] helpers/mcu: fix EC_CMD_GET_FEATURES usage Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 12/14] treewide: remove "r" in open() if reading mode Tzung-Bi Shih
2023-03-13 9:44 ` Tzung-Bi Shih [this message]
2023-03-13 9:44 ` [PATCH 14/14] treewide: don't use "+" operator for concatenating strings Tzung-Bi Shih
2023-03-13 11:53 ` [PATCH 00/14] cros-ec-tests: fix some exceptions and clean-ups Ricardo Cañuelo
2023-03-14 3:09 ` Tzung-Bi Shih
2023-03-14 6:47 ` Guillaume Charles Tucker
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=20230313094431.507952-14-tzungbi@kernel.org \
--to=tzungbi@kernel.org \
--cc=bleung@chromium.org \
--cc=chrome-platform@lists.linux.dev \
--cc=denys.f@collabora.com \
--cc=groeck@chromium.org \
--cc=guillaume.tucker@collabora.com \
--cc=ricardo.canuelo@collabora.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