diff --git a/airtest/core/android/adb.py b/airtest/core/android/adb.py index f0cc006e2..8c73b9b23 100644 --- a/airtest/core/android/adb.py +++ b/airtest/core/android/adb.py @@ -484,6 +484,9 @@ def push(self, local, remote): >>> adb = device().adb >>> adb.push("test.txt", "/data/local/tmp") + >>> adb.push("test.txt", "/data/local/tmp/test.txt") + >>> adb.push("testNoExtFile", "/data/local/tmp/testNoExtFile") # test no extension file + >>> adb.push("testNoExtFile", "/data/local/tmp/testNoExtFile2") # test no extension file with new name(for backward compatibility) >>> new_name = adb.push("test space.txt", "/data/local/tmp") # test the space in file name >>> print(new_name) @@ -494,52 +497,53 @@ def push(self, local, remote): >>> adb.push("test_dir", "/sdcard/Android/data/com.test.package/files/test_dir") """ - _, ext = os.path.splitext(remote) - if ext: - # The target path is a file - dst_parent = os.path.dirname(remote) + src_is_file = os.path.isfile(local) + + if src_is_file: + # If local file has extension and remote path has no extension, treat remote path as a directory + src_ext = os.path.splitext(local)[-1] + dst_ext = os.path.splitext(remote)[-1] + if dst_ext == '' and src_ext != '': + dst_path = f"{remote}/{os.path.basename(local)}" + else: + dst_path = remote else: - dst_parent = remote + if os.path.basename(local) != os.path.basename(remote): + dst_path = f"{remote}/{os.path.basename(local)}" + else: + dst_path = remote # If the target file already exists, delete it first to avoid overwrite failure - src_filename = os.path.basename(local) - _, src_ext = os.path.splitext(local) - if src_ext: - dst_path = f"{dst_parent}/{src_filename}" - else: - if src_filename == os.path.basename(remote): - dst_path = remote - else: - dst_path = f"{dst_parent}/{src_filename}" try: self.shell(f"rm -r {dst_path}") except: pass # If the target folder has multiple levels that have never been created, try to create them + dst_parent = os.path.dirname(dst_path) try: self.shell(f"mkdir -p {dst_parent}") except: pass # Push the file to the tmp directory to avoid permission issues - tmp_path = f"{TMP_PATH}/{src_filename}" + tmp_path = f"{TMP_PATH}/{os.path.basename(dst_path)}" try: self.cmd(["push", local, tmp_path]) except: - self.cmd(["push", local, dst_parent]) + self.cmd(["push", local, dst_path]) else: try: - if src_ext: + if src_is_file: try: - self.shell(f'mv "{tmp_path}" "{remote}"') + self.shell(f'mv "{tmp_path}" "{dst_parent}"') except: - self.shell(f'mv "{tmp_path}" "{remote}"') + self.shell(f'mv "{tmp_path}" "{dst_parent}"') else: try: - self.shell(f'cp -frp "{tmp_path}/*" "{remote}"') + self.shell(f'cp -frp "{tmp_path}" "{dst_parent}"') except: - self.shell(f'mv "{tmp_path}" "{remote}"') + self.shell(f'mv "{tmp_path}" "{dst_parent}"') finally: try: if TMP_PATH != dst_parent: