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