forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroidfiles.py
More file actions
executable file
·67 lines (56 loc) · 2.06 KB
/
androidfiles.py
File metadata and controls
executable file
·67 lines (56 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import sys, os
import subprocess
import posixpath
def is_android(path):
return path.startswith("android:")
def strip_schema(path):
'''Strip off any android: prefix in the path.
'''
if path.startswith("android:"):
return path[8:]
return path
def listdir(path, sort=True, sizes=False):
'''List the contents of the given directory.
Returns a list of filenames if sizes=False.
If sizes=True, returns a list of tuples (filename, int size).
'''
proc = subprocess.Popen(["adb", "shell", "ls", "-l", strip_schema(path)],
shell=False,
stdout=subprocess.PIPE)
stdout_lines = proc.communicate()[0].split('\n')
file_list = []
for l in stdout_lines:
l = l.strip().split()
if len(l) == 7:
if sizes:
try:
file_list.append((l[-1], int(l[3])))
except:
# This could happen for the initial "Total:" line
pass
else:
file_list.append(l[-1])
if sort:
file_list.sort()
return file_list
def copyto(f, outdir, fname):
'''Copy a local file (f is the full pathname) to the android device
at android location outdir, android new filename fname.
'''
subprocess.call(["adb", "push", f, posixpath.join(strip_schema(outdir),
fname)])
if __name__ == "__main__":
# copyto('/home/akkana/POD/Science/Story_Collider/249076872-the-story-collider-jonaki-bhattacharyya-losing-control.mp3', 'android:/mnt/extSdCard/Music/Podcasts', '16-05-99-so-special.mp3')
if len(sys.argv) < 2:
print "Usage: %s path [path ...]" % os.path.basename(sys.argv[0])
sys.exit(1)
sizes = True
for path in (sys.argv[1:]):
files = listdir(path, sizes=sizes)
if sizes:
print "\n", path, ":"
for f in files:
print "%d\t%s" % (f[1], f[0])
else:
print path, ":", ', '.join(files)