blob: 32dfcb70c9979ff41a785f3d11c3a16f75a7cfcc [file] [log] [blame]
Han Shen0d398632016-02-01 16:18:19 -08001#!/usr/bin/python2
asharifd8136e42013-02-15 23:18:03 +00002#
Han Shen0d398632016-02-01 16:18:19 -08003# Copyright 2010~2015 Google Inc. All Rights Reserved.
asharifd8136e42013-02-15 23:18:03 +00004"""Script to get past the login screen of ChromeOS.
5
6"""
Han Shen0d398632016-02-01 16:18:19 -08007from __future__ import print_function
asharifd8136e42013-02-15 23:18:03 +00008
Han Shen0d398632016-02-01 16:18:19 -08009import argparse
asharifd8136e42013-02-15 23:18:03 +000010import os
asharifd8136e42013-02-15 23:18:03 +000011import sys
asharif54cf6782013-02-16 02:11:03 +000012import tempfile
Han Shen0d398632016-02-01 16:18:19 -080013
14from cros_utils import command_executer
asharifd8136e42013-02-15 23:18:03 +000015
16LOGIN_PROMPT_VISIBLE_MAGIC_FILE = '/tmp/uptime-login-prompt-visible'
17LOGGED_IN_MAGIC_FILE = '/var/run/state/logged-in'
18
Luis Lozanof2a3ef42015-12-15 13:49:30 -080019script_header = """
asharifd8136e42013-02-15 23:18:03 +000020import os
21import autox
22import time
asharifd2ced682013-02-16 01:05:17 +000023"""
24
Luis Lozanof2a3ef42015-12-15 13:49:30 -080025wait_for_login_screen = """
asharifd8136e42013-02-15 23:18:03 +000026
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)
asharifd2ced682013-02-16 01:05:17 +000035""" % LOGIN_PROMPT_VISIBLE_MAGIC_FILE
asharifd8136e42013-02-15 23:18:03 +000036
Luis Lozanof2a3ef42015-12-15 13:49:30 -080037do_login = """
asharifd8136e42013-02-15 23:18:03 +000038xauth_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'
asharifd2ced682013-02-16 01:05:17 +000066""" % LOGGED_IN_MAGIC_FILE
asharifd8136e42013-02-15 23:18:03 +000067
Luis Lozanof2a3ef42015-12-15 13:49:30 -080068
asharifd2ced682013-02-16 01:05:17 +000069def RestartUI(remote, chromeos_root, login=True):
asharifd8136e42013-02-15 23:18:03 +000070 chromeos_root = os.path.expanduser(chromeos_root)
71 ce = command_executer.GetCommandExecuter()
72 # First, restart ui.
asharifd2ced682013-02-16 01:05:17 +000073 command = 'rm -rf %s && restart ui' % LOGIN_PROMPT_VISIBLE_MAGIC_FILE
Luis Lozanof2a3ef42015-12-15 13:49:30 -080074 ce.CrosRunCommand(command, machine=remote, chromeos_root=chromeos_root)
asharif54cf6782013-02-16 02:11:03 +000075 host_login_script = tempfile.mktemp()
76 device_login_script = '/tmp/login.py'
asharifd2ced682013-02-16 01:05:17 +000077 login_script_list = [script_header, wait_for_login_screen]
78 if login:
79 login_script_list.append(do_login)
80
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081 full_login_script_contents = '\n'.join(login_script_list)
asharifd2ced682013-02-16 01:05:17 +000082
asharif54cf6782013-02-16 02:11:03 +000083 with open(host_login_script, 'w') as f:
asharifd8136e42013-02-15 23:18:03 +000084 f.write(full_login_script_contents)
asharif54cf6782013-02-16 02:11:03 +000085 ce.CopyFiles(host_login_script,
86 device_login_script,
asharifd8136e42013-02-15 23:18:03 +000087 dest_machine=remote,
88 chromeos_root=chromeos_root,
89 recursive=False,
90 dest_cros=True)
asharif54cf6782013-02-16 02:11:03 +000091 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
asharifd8136e42013-02-15 23:18:03 +000097
98
99def Main(argv):
100 """The main function."""
Han Shen0d398632016-02-01 16:18:19 -0800101 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.')
asharifd8136e42013-02-15 23:18:03 +0000110
Han Shen0d398632016-02-01 16:18:19 -0800111 options = parser.parse_args(argv)
asharifd8136e42013-02-15 23:18:03 +0000112
asharif54cf6782013-02-16 02:11:03 +0000113 return RestartUI(options.remote, options.chromeos_root)
asharifd8136e42013-02-15 23:18:03 +0000114
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800115
asharifd8136e42013-02-15 23:18:03 +0000116if __name__ == '__main__':
Han Shen0d398632016-02-01 16:18:19 -0800117 retval = Main(sys.argv[1:])
asharif5ab1c7d2013-02-15 23:43:58 +0000118 sys.exit(retval)