Home | History | Annotate | Download | only in toolchain-utils
      1 #!/usr/bin/env python2
      2 #
      3 # Copyright 2010~2015 Google Inc. All Rights Reserved.
      4 """Script to get past the login screen of ChromeOS."""
      5 
      6 from __future__ import print_function
      7 
      8 import argparse
      9 import os
     10 import sys
     11 import tempfile
     12 
     13 from cros_utils import command_executer
     14 
     15 LOGIN_PROMPT_VISIBLE_MAGIC_FILE = '/tmp/uptime-login-prompt-visible'
     16 LOGGED_IN_MAGIC_FILE = '/var/run/state/logged-in'
     17 
     18 script_header = """
     19 import os
     20 import autox
     21 import time
     22 """
     23 
     24 wait_for_login_screen = """
     25 
     26 while True:
     27   print 'Waiting for login screen to appear...'
     28   if os.path.isfile('%s'):
     29     break
     30   time.sleep(1)
     31   print 'Done'
     32 
     33 time.sleep(20)
     34 """ % LOGIN_PROMPT_VISIBLE_MAGIC_FILE
     35 
     36 do_login = """
     37 xauth_filename = '/home/chronos/.Xauthority'
     38 os.environ.setdefault('XAUTHORITY', xauth_filename)
     39 os.environ.setdefault('DISPLAY', ':0.0')
     40 
     41 print 'Now sending the hotkeys for logging in.'
     42 ax = autox.AutoX()
     43 # navigate to login screen
     44 ax.send_hotkey('Ctrl+Shift+q')
     45 ax.send_hotkey('Ctrl+Alt+l')
     46 # escape out of any login screen menus (e.g., the network select menu)
     47 time.sleep(2)
     48 ax.send_hotkey('Escape')
     49 time.sleep(2)
     50 ax.send_hotkey('Tab')
     51 time.sleep(0.5)
     52 ax.send_hotkey('Tab')
     53 time.sleep(0.5)
     54 ax.send_hotkey('Tab')
     55 time.sleep(0.5)
     56 ax.send_hotkey('Tab')
     57 time.sleep(0.5)
     58 ax.send_hotkey('Return')
     59 print 'Waiting for Chrome to appear...'
     60 while True:
     61   if os.path.isfile('%s'):
     62     break
     63   time.sleep(1)
     64 print 'Done'
     65 """ % LOGGED_IN_MAGIC_FILE
     66 
     67 
     68 def RestartUI(remote, chromeos_root, login=True):
     69   chromeos_root = os.path.expanduser(chromeos_root)
     70   ce = command_executer.GetCommandExecuter()
     71   # First, restart ui.
     72   command = 'rm -rf %s && restart ui' % LOGIN_PROMPT_VISIBLE_MAGIC_FILE
     73   ce.CrosRunCommand(command, machine=remote, chromeos_root=chromeos_root)
     74   host_login_script = tempfile.mktemp()
     75   device_login_script = '/tmp/login.py'
     76   login_script_list = [script_header, wait_for_login_screen]
     77   if login:
     78     login_script_list.append(do_login)
     79 
     80   full_login_script_contents = '\n'.join(login_script_list)
     81 
     82   with open(host_login_script, 'w') as f:
     83     f.write(full_login_script_contents)
     84   ce.CopyFiles(
     85       host_login_script,
     86       device_login_script,
     87       dest_machine=remote,
     88       chromeos_root=chromeos_root,
     89       recursive=False,
     90       dest_cros=True)
     91   ret = ce.CrosRunCommand(
     92       'python %s' % device_login_script,
     93       chromeos_root=chromeos_root,
     94       machine=remote)
     95   if os.path.exists(host_login_script):
     96     os.remove(host_login_script)
     97   return ret
     98 
     99 
    100 def Main(argv):
    101   """The main function."""
    102   parser = argparse.ArgumentParser()
    103   parser.add_argument(
    104       '-r', '--remote', dest='remote', help='The remote ChromeOS box.')
    105   parser.add_argument(
    106       '-c', '--chromeos_root', dest='chromeos_root', help='The ChromeOS root.')
    107 
    108   options = parser.parse_args(argv)
    109 
    110   return RestartUI(options.remote, options.chromeos_root)
    111 
    112 
    113 if __name__ == '__main__':
    114   retval = Main(sys.argv[1:])
    115   sys.exit(retval)
    116