| Xavier Ducrohet | b958224 | 2009-12-01 13:03:49 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python | 
|  | 2 | # | 
|  | 3 | # Copyright (C) 2008 The Android Open Source Project | 
|  | 4 | # | 
|  | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | # you may not use this file except in compliance with the License. | 
|  | 7 | # You may obtain a copy of the License at | 
|  | 8 | # | 
|  | 9 | # http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | # | 
|  | 11 | # Unless required by applicable law or agreed to in writing, software | 
|  | 12 | # distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | # See the License for the specific language governing permissions and | 
|  | 15 | # limitations under the License. | 
|  | 16 |  | 
|  | 17 | """ | 
|  | 18 | A faux Setup Wizard. Stuffs one or two usernames + passwords into the | 
|  | 19 | database on the device. | 
|  | 20 | """ | 
|  | 21 |  | 
|  | 22 | import sys | 
|  | 23 | if sys.hexversion < 0x02040000: | 
|  | 24 | print "This script requires python 2.4 or higher." | 
|  | 25 | sys.exit(1) | 
|  | 26 |  | 
|  | 27 | import getpass | 
|  | 28 | import subprocess | 
|  | 29 | import time | 
|  | 30 | import sha | 
|  | 31 |  | 
|  | 32 | DB = "/data/data/com.google.android.googleapps/databases/accounts.db" | 
|  | 33 |  | 
|  | 34 | def RunCmd(args): | 
|  | 35 | proc = subprocess.Popen(args, stdout=subprocess.PIPE) | 
|  | 36 | out = proc.stdout.read() | 
|  | 37 | if proc.wait(): | 
|  | 38 | print | 
|  | 39 | print "failed: %s" % " ".join(args) | 
|  | 40 | return None | 
|  | 41 | return out | 
|  | 42 |  | 
|  | 43 | def GetProp(adb_flags, name): | 
|  | 44 | args = ("adb",) + adb_flags + ("shell", "/system/bin/getprop", name) | 
|  | 45 | return RunCmd(args) | 
|  | 46 |  | 
|  | 47 | def SetProp(adb_flags, name, value): | 
|  | 48 | args = ("adb",) + adb_flags + ("shell", "/system/bin/setprop", name, value) | 
|  | 49 | return RunCmd(args) | 
|  | 50 |  | 
|  | 51 | def DbExists(adb_flags): | 
|  | 52 | args = ("adb",) + adb_flags + ("shell", "/system/bin/ls", DB) | 
|  | 53 | result = RunCmd(args) | 
|  | 54 | if result is None: return None | 
|  | 55 | return "No such file" not in result | 
|  | 56 |  | 
|  | 57 | def main(argv): | 
|  | 58 | if len(argv) == 1: | 
|  | 59 | print ("usage: %s [adb flags] " | 
|  | 60 | "[<hosted address[:password]>] " | 
|  | 61 | "[<gmail address[:password]>]") % (argv[0],) | 
|  | 62 | sys.exit(2) | 
|  | 63 |  | 
|  | 64 | argv = argv[1:] | 
|  | 65 |  | 
|  | 66 | gmail = None | 
|  | 67 | hosted = None | 
|  | 68 | while argv and "@" in argv[-1]: | 
|  | 69 | addr = argv.pop() | 
|  | 70 | if "@gmail.com" in addr or "@googlemail.com" in addr: | 
|  | 71 | gmail = addr | 
|  | 72 | else: | 
|  | 73 | hosted = addr | 
|  | 74 |  | 
|  | 75 | adb_flags = tuple(argv) | 
|  | 76 |  | 
|  | 77 | while True: | 
|  | 78 | db = DbExists(adb_flags) | 
|  | 79 | if db is None: | 
|  | 80 | print "failed to contact device; will retry in 3 seconds" | 
|  | 81 | time.sleep(3) | 
|  | 82 | continue | 
|  | 83 |  | 
|  | 84 | if db: | 
|  | 85 | print | 
|  | 86 | print "GoogleLoginService has already started on this device;" | 
|  | 87 | print "it's too late to use this script to add accounts." | 
|  | 88 | print | 
|  | 89 | print "This script only works on a freshly-wiped device (or " | 
|  | 90 | print "emulator) while booting for the first time." | 
|  | 91 | print | 
|  | 92 | break | 
|  | 93 |  | 
|  | 94 | hosted_account = GetProp(adb_flags, "ro.config.hosted_account").strip() | 
|  | 95 | google_account = GetProp(adb_flags, "ro.config.google_account").strip() | 
|  | 96 |  | 
|  | 97 | if hosted and hosted_account: | 
|  | 98 | print | 
|  | 99 | print "A hosted account is already configured on this device;" | 
|  | 100 | print "can't add", hosted_account | 
|  | 101 | print | 
|  | 102 | hosted = None | 
|  | 103 |  | 
|  | 104 | if gmail and google_account: | 
|  | 105 | print | 
|  | 106 | print "A google account is already configured on this device;" | 
|  | 107 | print "can't add", google_account | 
|  | 108 | print | 
|  | 109 | gmail = None | 
|  | 110 |  | 
|  | 111 | if not gmail and not hosted: break | 
|  | 112 |  | 
|  | 113 | if hosted: | 
|  | 114 | SetProp(adb_flags, "ro.config.hosted_account", hosted) | 
|  | 115 | print "set hosted_account to", hosted | 
|  | 116 | if gmail: | 
|  | 117 | SetProp(adb_flags, "ro.config.google_account", gmail) | 
|  | 118 | print "set google_account to", gmail | 
|  | 119 |  | 
|  | 120 | break | 
|  | 121 |  | 
|  | 122 |  | 
|  | 123 |  | 
|  | 124 |  | 
|  | 125 |  | 
|  | 126 |  | 
|  | 127 | if __name__ == "__main__": | 
|  | 128 | main(sys.argv) |