Home | History | Annotate | Download | only in scripts
      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", "su", "root",
     45                                  "/system/bin/getprop", name)
     46   return RunCmd(args)
     47 
     48 def SetProp(adb_flags, name, value):
     49   args = ("adb",) + adb_flags + ("shell", "su", "root",
     50                                  "/system/bin/setprop", name, value)
     51   return RunCmd(args)
     52 
     53 def DbExists(adb_flags):
     54   args = ("adb",) + adb_flags + ("shell", "su", "root",
     55                                  "/system/bin/ls", DB)
     56   result = RunCmd(args)
     57   if result is None: return None
     58   return "No such file" not in result
     59 
     60 def main(argv):
     61   if len(argv) == 1:
     62     print ("usage: %s [adb flags] "
     63            "[<dasher address[:password]>] "
     64            "[<gmail address[:password]>]") % (argv[0],)
     65     sys.exit(2)
     66 
     67   argv = argv[1:]
     68 
     69   gmail = None
     70   dasher = None
     71   while argv and "@" in argv[-1]:
     72     addr = argv.pop()
     73     if "@gmail.com" in addr or "@googlemail.com" in addr:
     74       gmail = addr
     75     else:
     76       dasher = addr
     77 
     78   adb_flags = tuple(argv)
     79 
     80   while True:
     81     db = DbExists(adb_flags)
     82     if db is None:
     83       print "failed to contact device; will retry in 3 seconds"
     84       time.sleep(3)
     85       continue
     86 
     87     if db:
     88       print
     89       print "GoogleLoginService has already started on this device;"
     90       print "it's too late to use this script to add accounts."
     91       print
     92       print "This script only works on a freshly-wiped device (or "
     93       print "emulator) while booting for the first time."
     94       print
     95       break
     96 
     97     hosted_account = GetProp(adb_flags, "ro.config.hosted_account").strip()
     98     google_account = GetProp(adb_flags, "ro.config.google_account").strip()
     99 
    100     if dasher and hosted_account:
    101       print
    102       print "A dasher account is already configured on this device;"
    103       print "can't add", hosted_account
    104       print
    105       dasher = None
    106 
    107     if gmail and google_account:
    108       print
    109       print "A google account is already configured on this device;"
    110       print "can't add", google_account
    111       print
    112       gmail = None
    113 
    114     if not gmail and not dasher: break
    115 
    116     if dasher:
    117       SetProp(adb_flags, "ro.config.hosted_account", dasher)
    118       print "set hosted_account to", dasher
    119     if gmail:
    120       SetProp(adb_flags, "ro.config.google_account", gmail)
    121       print "set google_account to", gmail
    122 
    123     break
    124 
    125 
    126 
    127 
    128 
    129 
    130 if __name__ == "__main__":
    131   main(sys.argv)
    132