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"""
7from __future__ import print_function
8
9import argparse
10import os
11import sys
12import tempfile
13
14from cros_utils import command_executer
15
16LOGIN_PROMPT_VISIBLE_MAGIC_FILE = '/tmp/uptime-login-prompt-visible'
17LOGGED_IN_MAGIC_FILE = '/var/run/state/logged-in'
18
19script_header = """
20import os
21import autox
22import time
23"""
24
25wait_for_login_screen = """
26
27while 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
34time.sleep(20)
35""" % LOGIN_PROMPT_VISIBLE_MAGIC_FILE
36
37do_login = """
38xauth_filename = '/home/chronos/.Xauthority'
39os.environ.setdefault('XAUTHORITY', xauth_filename)
40os.environ.setdefault('DISPLAY', ':0.0')
41
42print 'Now sending the hotkeys for logging in.'
43ax = autox.AutoX()
44# navigate to login screen
45ax.send_hotkey('Ctrl+Shift+q')
46ax.send_hotkey('Ctrl+Alt+l')
47# escape out of any login screen menus (e.g., the network select menu)
48time.sleep(2)
49ax.send_hotkey('Escape')
50time.sleep(2)
51ax.send_hotkey('Tab')
52time.sleep(0.5)
53ax.send_hotkey('Tab')
54time.sleep(0.5)
55ax.send_hotkey('Tab')
56time.sleep(0.5)
57ax.send_hotkey('Tab')
58time.sleep(0.5)
59ax.send_hotkey('Return')
60print 'Waiting for Chrome to appear...'
61while True:
62  if os.path.isfile('%s'):
63    break
64  time.sleep(1)
65print 'Done'
66""" % LOGGED_IN_MAGIC_FILE
67
68
69def 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
99def 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
116if __name__ == '__main__':
117  retval = Main(sys.argv[1:])
118  sys.exit(retval)
119