0
Help us understand the problem. What are the problem?

posted at

updated at

pure-python-adbでkillが動かない場合

ライブラリを修正する

以下にある
%appdata%..\Local\Packages\PythonSoftwareFoundation.Python.3.x_xxxxxxxx\LocalCache\local-packages\Python39\

site-packages\ppadb\command\transport\__init__.py
の22行目くらい

#        cmd = "shell:{}".format(cmd)
        cmd = "shell,v2,TERM=xterm256color,raw:{}".format(cmd)

こんな感じで使うと動くようになる

self.device.shell("kill -9 `ps -ef | grep " + option + "|grep -v grep | awk '{print $2}'`")

ついでに2重rootの許可とremountも実装するとこんな感じ

diff --git a/ppadb/command/transport/__init__.py b/ppadb/command/transport/__init__.py
index 1adadf6..8bfba12 100644
--- a/ppadb/command/transport/__init__.py
+++ b/ppadb/command/transport/__init__.py
@@ -16,10 +16,23 @@ class Transport(Command):

         return connection

+    def remount(self):
+        conn = self.create_connection(timeout=timeout)
+
+        cmd = "remount:"
+        conn.send(cmd)
+
+        if handler:
+            handler(conn)
+        else:
+            result = conn.read_all()
+            conn.close()
+            return result.decode("utf-8")
+
     def shell(self, cmd, handler=None, timeout=None):
         conn = self.create_connection(timeout=timeout)

-        cmd = "shell:{}".format(cmd)
+        cmd = "shell,v2,TERM=xterm256color,raw:{}".format(cmd)
         conn.send(cmd)

         if handler:
@@ -185,6 +198,8 @@ class Transport(Command):

             if "restarting adbd as root" in result:
                 return True
+            elif "adbd is already running as root" in result:
+                return True
             else:
                 raise RuntimeError(result.strip())

exec-outは以下も追加(screencapしか考慮していない)

    def exec(self, cmd, handler=None, timeout=None):
        conn = self.create_connection(timeout=timeout)

        cmd = "exec:{}".format(cmd)
        conn.send(cmd)

        if handler:
            handler(conn)
        else:
            result = conn.read_all()
            conn.close()
            return result

使用例

        buf = self.device.exec("screencap -p")

# 以下は普通関数化すると思う
        try:
            dir_name = os.path.dirname(option)
            os.makedirs(os.path.join("captures", dir_name), exist_ok=True)
            out_path = os.path.join("captures", option + "-" + str(self.test_count) + ".png")
            f = open(out_path, "wb")
            f.write(buf)
            f.close()
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
0
Help us understand the problem. What are the problem?