diff mbox series

[13/14] treewide: don't use "+" operator for constructing paths

Message ID 20230313094431.507952-14-tzungbi@kernel.org (mailing list archive)
State Handled Elsewhere
Headers show
Series cros-ec-tests: fix some exceptions and clean-ups | expand

Commit Message

Tzung-Bi Shih March 13, 2023, 9:44 a.m. UTC
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 mbox series

Patch

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))