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