Skip to content

Adds globbing to pull #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions adb-root.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/env python
import argparse, subprocess, logging
#!/usr/bin/env python3
import argparse, subprocess, logging, os

parser = argparse.ArgumentParser(description="read/write files as root on any Android device")
parser.add_argument("action", choices=["push", "pull"], help="pull to copy from device, push to copy to device")
Expand Down Expand Up @@ -52,14 +52,33 @@ def push():

if args.check:
hash_check(args.source, args.target)

def yes_or_no(question):
reply = str(input(question+' (y/n): ')).lower().strip()
if reply[0] == 'y':
return True
if reply[0] == 'n':
return False
else:
return yes_or_no("Please enter y or n:")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It crashes if you hit just enter:

Traceback (most recent call last):
  File "adb-root.py", line 92, in <module>
    pull()
  File "adb-root.py", line 74, in pull
    if yes_or_no("Destination file exits. Ovewrite it ?"):
  File "adb-root.py", line 57, in yes_or_no
    if reply[0] == 'y':
IndexError: string index out of range

Would be better to just default to no (y/N) if the input is empty here.


def pull():
logging.info("Started pulling %s", args.source)
with open(args.target, "w+") as file:
result = subprocess.run(['adb', 'shell', "su -c", "dd if="+args.source], stdout=file)
log_exitcode("Transfer", result)

filelist = subprocess.run(['adb', 'shell', "su -c \"", "ls "+args.source + "\""], encoding='utf-8', stdout=subprocess.PIPE)
for source in filelist.stdout.split('\n'):
if source != '':
source_filename=os.path.basename(source)
destination=args.target + "/" + source_filename
print("Copying " + source + " to " + destination)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use logging instead of print. Level info would be suitable here

if os.path.isfile(destination):
if yes_or_no("Destination file exits. Ovewrite it ?"):
with open(destination, "w+") as file:
result = subprocess.run(['adb', 'shell', "su -c \"", "dd if="+source + "\""], stdout=file)
log_exitcode("Transfer", result)
else:
with open(destination, "w+") as file:
result = subprocess.run(['adb', 'shell', "su -c \"", "dd if="+source + "\""], stdout=file)
log_exitcode("Transfer", result)

if args.check:
hash_check(args.target, args.source)

Expand Down