blob: 06ff8ff03f9d442abf867a289d5be3b9badab2aa [file] [log] [blame]
Caroline Ticef6ef4392017-04-06 17:16:05 -07001#!/usr/bin/env python2
asharifd8136e42013-02-15 23:18:03 +00002#
Han Shen0d398632016-02-01 16:18:19 -08003# Copyright 2010~2015 Google Inc. All Rights Reserved.
Caroline Ticef6ef4392017-04-06 17:16:05 -07004"""Script to get past the login screen of ChromeOS."""
asharifd8136e42013-02-15 23:18:03 +00005
Han Shen0d398632016-02-01 16:18:19 -08006from __future__ import print_function
asharifd8136e42013-02-15 23:18:03 +00007
Han Shen0d398632016-02-01 16:18:19 -08008import argparse
asharifd8136e42013-02-15 23:18:03 +00009import os
asharifd8136e42013-02-15 23:18:03 +000010import sys
asharif54cf6782013-02-16 02:11:03 +000011import tempfile
Han Shen0d398632016-02-01 16:18:19 -080012
13from cros_utils import command_executer
asharifd8136e42013-02-15 23:18:03 +000014
15LOGIN_PROMPT_VISIBLE_MAGIC_FILE = '/tmp/uptime-login-prompt-visible'
16LOGGED_IN_MAGIC_FILE = '/var/run/state/logged-in'
17
Luis Lozanof2a3ef42015-12-15 13:49:30 -080018script_header = """
asharifd8136e42013-02-15 23:18:03 +000019import os
20import autox
21import time
asharifd2ced682013-02-16 01:05:17 +000022"""
23
Luis Lozanof2a3ef42015-12-15 13:49:30 -080024wait_for_login_screen = """
asharifd8136e42013-02-15 23:18:03 +000025
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)
asharifd2ced682013-02-16 01:05:17 +000034""" % LOGIN_PROMPT_VISIBLE_MAGIC_FILE
asharifd8136e42013-02-15 23:18:03 +000035
Luis Lozanof2a3ef42015-12-15 13:49:30 -080036do_login = """
asharifd8136e42013-02-15 23:18:03 +000037xauth_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'
asharifd2ced682013-02-16 01:05:17 +000065""" % LOGGED_IN_MAGIC_FILE
asharifd8136e42013-02-15 23:18:03 +000066
Luis Lozanof2a3ef42015-12-15 13:49:30 -080067
asharifd2ced682013-02-16 01:05:17 +000068def RestartUI(remote, chromeos_root, login=True):
asharifd8136e42013-02-15 23:18:03 +000069 chromeos_root = os.path.expanduser(chromeos_root)
70 ce = command_executer.GetCommandExecuter()
71 # First, restart ui.
asharifd2ced682013-02-16 01:05:17 +000072 command = 'rm -rf %s && restart ui' % LOGIN_PROMPT_VISIBLE_MAGIC_FILE
Luis Lozanof2a3ef42015-12-15 13:49:30 -080073 ce.CrosRunCommand(command, machine=remote, chromeos_root=chromeos_root)
asharif54cf6782013-02-16 02:11:03 +000074 host_login_script = tempfile.mktemp()
75 device_login_script = '/tmp/login.py'
asharifd2ced682013-02-16 01:05:17 +000076 login_script_list = [script_header, wait_for_login_screen]
77 if login:
78 login_script_list.append(do_login)
79
Luis Lozanof2a3ef42015-12-15 13:49:30 -080080 full_login_script_contents = '\n'.join(login_script_list)
asharifd2ced682013-02-16 01:05:17 +000081
asharif54cf6782013-02-16 02:11:03 +000082 with open(host_login_script, 'w') as f:
asharifd8136e42013-02-15 23:18:03 +000083 f.write(full_login_script_contents)
Caroline Ticef6ef4392017-04-06 17:16:05 -070084 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)
asharif54cf6782013-02-16 02:11:03 +000095 if os.path.exists(host_login_script):
96 os.remove(host_login_script)
97 return ret
asharifd8136e42013-02-15 23:18:03 +000098
99
100def Main(argv):
101 """The main function."""
Han Shen0d398632016-02-01 16:18:19 -0800102 parser = argparse.ArgumentParser()
Caroline Ticef6ef4392017-04-06 17:16:05 -0700103 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.')
asharifd8136e42013-02-15 23:18:03 +0000107
Han Shen0d398632016-02-01 16:18:19 -0800108 options = parser.parse_args(argv)
asharifd8136e42013-02-15 23:18:03 +0000109
asharif54cf6782013-02-16 02:11:03 +0000110 return RestartUI(options.remote, options.chromeos_root)
asharifd8136e42013-02-15 23:18:03 +0000111
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800112
asharifd8136e42013-02-15 23:18:03 +0000113if __name__ == '__main__':
Han Shen0d398632016-02-01 16:18:19 -0800114 retval = Main(sys.argv[1:])
asharif5ab1c7d2013-02-15 23:43:58 +0000115 sys.exit(retval)