-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage-ringtones.py
executable file
·108 lines (83 loc) · 2.74 KB
/
manage-ringtones.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python3
#
from optparse import OptionParser
import filecmp
import os
import shutil
import subprocess
def backup(directory):
print('Backing up device ringtones')
pull('/sdcard/Ringtones', directory)
pull('/sdcard/Notifications', directory)
pull('/sdcard/Alarms', directory)
pass
def restore(directory):
print('Restoring device ringtones')
subprocess.call(['adb', 'root'])
restoreSection('Ringtones', directory)
restoreSection('Notifications', directory)
restoreSection('Alarms', directory)
pass
def restoreSection(section, directory):
push('%s/%s' % (directory, section), '/sdcard/')
lines = subprocess.check_output(['adb', 'shell', 'ls', '/product/media/audio/%s/' % section.lower()]).decode("utf-8")
systemFiles = lines.split()
for file in systemFiles:
subprocess.call(['adb', 'shell', 'rm', '-f', '/sdcard/%s/%s' %(section, file)])
pass
def get(directory):
print('Copying system ringtones from device')
subprocess.call(['adb', 'root'])
getSection('ringtones', directory)
getSection('notifications', directory)
getSection('alarms', directory)
pass
def getSection(section, directory):
tmpDir = directory + '/tmp'
localSection = section.capitalize()
pull('/product/media/audio/' + section, tmpDir)
files = os.listdir(tmpDir)
dstDir = '%s/%s' % (directory, localSection)
for file in files:
src = tmpDir + '/' + file
dst = '%s/%s' % (dstDir, file)
if os.path.exists(dst):
if filecmp.cmp(src, dst, shallow=False):
os.remove(dst)
else:
os.remove(src)
continue
shutil.move(src, dst)
os.removedirs(tmpDir)
pass
def pull(fromDir, toDir):
subprocess.call(['adb', 'pull', fromDir, toDir])
pass
def push(fromDir, toDir):
subprocess.call(['adb', 'push', fromDir, toDir])
pass
usage = "usage: %prog [options] backup|restore|get"
parser = OptionParser(usage=usage)
parser.add_option("-d", "--dir", dest="dir", default='.', help="Backup/Restore directory", metavar="DIR")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.print_usage()
exit(1)
command = args[0]
directory = options.dir
if os.path.exists(directory):
if not os.path.isdir:
print('Path is not a directory: %s' % (directory))
os.makedirs(directory + "/Ringtones", exist_ok=True)
os.makedirs(directory + "/Alarms", exist_ok=True)
os.makedirs(directory + "/Notifications", exist_ok=True)
if 'backup'.startswith(command):
backup(directory)
elif 'restore'.startswith(command):
restore(directory)
elif 'get'.startswith(command):
get(directory)
else:
print('Invalid command: %s' % (command))
parser.print_usage()
exit(1)