简体   繁体   English

带进度条的 adb 推/拉

[英]adb push/pull with progress bar

It is really annoying if you adb push/pull large files to the device that there's no way to now how far along it is.如果您将大文件 adb 推/拉到设备上,那真的很烦人,现在无法知道它有多远。 Is it possible to run adb push or adb pull and get a progress bar using the 'bar' utility?是否可以运行 adb push 或 adb pull 并使用“bar”实用程序获取进度条?

The main issue here is I think that adb expects two file names, if the input file could be replaced by stdin you could pipe through the 'bar' utility and get a progress bar.这里的主要问题是我认为 adb 需要两个文件名,如果输入文件可以替换为标准输入,则可以通过“bar”实用程序 pipe 并获得进度条。 So far I haven't succeeded in doing so, but I'm not really a shell guru which is why I'm asking here:)到目前为止,我还没有成功,但我并不是真正的 shell 大师,这就是我在这里问的原因:)

Note that I'm on Linux using bash.请注意,我在 Linux 上使用 bash。

It looks like the latest adb has progress support.看起来最新的 adb 有进度支持。

Android Debug Bridge version 1.0.32
device commands:
adb push [-p] <local> <remote>
    - copy file/dir to device
    ('-p' to display the transfer progress)

However, the answers above also work for 'adb install' which do not have a progress option.但是,上面的答案也适用于没有进度选项的“adb install”。 I modified the first answer's script to work this way:我修改了第一个答案的脚本以这种方式工作:

Create "adb-install.sh" somewhere in your PATH and run "adb-install.sh " instead of "adb install -f "在 PATH 中的某处创建“adb-install.sh”并运行“adb-install.sh”而不是“adb install -f”

#!/bin/bash
# adb install with progressbar displayed
# usage: <adb-install.sh> <file.apk>
# original code from: http://stackoverflow.com/questions/6595374/adb-push-pull-with-progress-bar

function usage()
{
    echo "$0 <apk to install>"
    exit 1
}

function progressbar()
{
    bar="================================================================================"
    barlength=${#bar}
    n=$(($1*barlength/100))
    printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
    # echo -ne "\b$1"
}

export -f progressbar

[[ $# < 1 ]] && usage

SRC=$1

[ ! -f $SRC ] && { \
    echo "source file not found"; \
    exit 2; \
}

which adb >/dev/null 2>&1 || { \
    echo "adb doesn't exist in your path"; \
    exit 3; \
}

SIZE=$(ls -l $SRC | awk '{print $5}')
export ADB_TRACE=all

adb install -r $SRC 2>&1 \
    | sed -n '/DATA/p' \
    | awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'

export ADB_TRACE=

echo

echo 'press any key'
read n

Currently I have this little piece of bash:目前我有一小块bash:

function adb_push {
  # NOTE: 65544 is the max size adb seems to transfer in one go
  TOTALSIZE=$(ls -Rl "$1" | awk '{ sum += sprintf("%.0f\n", ($5 / 65544)+0.5) } END { print sum }')
  exp=$(($TOTALSIZE * 7)) # 7 bytes for every line we print - not really accurate if there's a lot of small files :(
  # start bar in the background
  ADB_TRACE=adb adb push "$1" "$2" 2>&1 | unbuffer -p awk '/DATA/ { split($3,a,"="); print a[2] }' | unbuffer -p cut -d":" -s -f1 | unbuffer -p bar -of /dev/null -s $exp
  echo  # Add a newline after the progressbar.
}

It works somewhat, it shows a progress bar going from 0 to 100 which is nice.它有点工作,它显示了一个从 0 到 100 的进度条,这很好。 However, it won't be correct if you do a lot of small files, and worse, the bytes/s and total bytes shown by 'bar' aren't correct.但是,如果你做很多小文件,那将是不正确的,更糟糕的是,'bar' 显示的字节数和总字节数是不正确的。

I challenge you to improve on my script;我挑战你改进我的剧本; it shouldn't be hard;应该不难; ;) ;)

I made a patch to adb to fix this.我为 adb 做了一个补丁来解决这个问题。

https://android-review.googlesource.com/#/c/87748 https://android-review.googlesource.com/#/c/87748

Here is my solution, it will show a simple progressbar and current numeric progress这是我的解决方案,它将显示一个简单的进度条和当前的数字进度

[==================================================] 100%

Usage用法

./progress_adb.sh source destination

progress_adb.sh progress_adb.sh

#!/bin/bash

function usage()
{
    echo "$0 source destination"
    exit 1
}

function progressbar()
{
    bar="=================================================="
    barlength=${#bar}
    n=$(($1*barlength/100))
    printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
    # echo -ne "\b$1"
}

export -f progressbar

[[ $# < 2 ]] && usage

SRC=$1
DST=$2

[ ! -f $SRC ] && { \
    echo "source file not found"; \
    exit 2; \
}

which adb >/dev/null 2>&1 || { \
    echo "adb doesn't exist in your path"; \
    exit 3; \
}

SIZE=$(ls -l $SRC | awk '{print $5}')
ADB_TRACE=adb adb push $SRC $DST 2>&1 \
    | sed -n '/DATA/p' \
    | awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'

echo 

Testing on Ubuntu 14.04在 Ubuntu 14.04 上进行测试

$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

TODO去做

directory support目录支持

progressbar size change when screen size change屏幕尺寸变化时进度条尺寸变化

QtADB has both per-file and total progress bars. QtADB 有每个文件和总进度条。 http://qtadb.wordpress.com/ http://qtadb.wordpress.com/

Well I can give you an Idea:好吧,我可以给你一个想法:

ADB_TRACE=adb adb push <source> <destination> 

returns logs for any command, so for example the copy command, which looks like:返回任何命令的日志,例如复制命令,如下所示:

writex: fd=3 len=65544: 4441544100000100000000021efd  DATA....#....b..

here you can get the total bytes length before, with ls -a, then parse the output of adb with grep or awk, increment an interneral counter and send the current progress to the bar utility.在这里您可以使用 ls -a 获取之前的总字节长度,然后使用 grep 或 awk 解析 adb 的 output ,并增加当前进度条实用程序。

When you succeeded, please post the script here.成功后,请在此处发布脚本。

I found a Python pastebin which is incomplete but close: it needs a \r and arguments support.我发现了一个 Python pastebin ,它不完整但很接近:它需要一个\r和 arguments 支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM