blob: b510444dcbfebd301a361e58c6ff3b718fb6e1b0 [file] [log] [blame]
Mike Frysingerc7f15932013-03-20 13:43:35 -04001#!/usr/bin/python
asharifd8136e42013-02-15 23:18:03 +00002#
3# Copyright 2010 Google Inc. All Rights Reserved.
asharifd8136e42013-02-15 23:18:03 +00004"""Script to get past the login screen of ChromeOS.
5
6"""
7
Luis Lozanof2a3ef42015-12-15 13:49:30 -08008__author__ = 'asharif@google.com (Ahmad Sharif)'
asharifd8136e42013-02-15 23:18:03 +00009
10import datetime
11import fcntl
12import getpass
13import glob
14import optparse
15import os
16import pickle
17import socket
18import sys
19import time
asharif54cf6782013-02-16 02:11:03 +000020import tempfile
asharifd8136e42013-02-15 23:18:03 +000021from utils import logger
22from utils import command_executer
23
24LOGIN_PROMPT_VISIBLE_MAGIC_FILE = '/tmp/uptime-login-prompt-visible'
25LOGGED_IN_MAGIC_FILE = '/var/run/state/logged-in'
26
Luis Lozanof2a3ef42015-12-15 13:49:30 -080027script_header = """
asharifd8136e42013-02-15 23:18:03 +000028import os
29import autox
30import time
asharifd2ced682013-02-16 01:05:17 +000031"""
32
Luis Lozanof2a3ef42015-12-15 13:49:30 -080033wait_for_login_screen = """
asharifd8136e42013-02-15 23:18:03 +000034
35while True:
36 print 'Waiting for login screen to appear...'
37 if os.path.isfile('%s'):
38 break
39 time.sleep(1)
40 print 'Done'
41
42time.sleep(20)
asharifd2ced682013-02-16 01:05:17 +000043""" % LOGIN_PROMPT_VISIBLE_MAGIC_FILE
asharifd8136e42013-02-15 23:18:03 +000044
Luis Lozanof2a3ef42015-12-15 13:49:30 -080045do_login = """
asharifd8136e42013-02-15 23:18:03 +000046xauth_filename = '/home/chronos/.Xauthority'
47os.environ.setdefault('XAUTHORITY', xauth_filename)
48os.environ.setdefault('DISPLAY', ':0.0')
49
50print 'Now sending the hotkeys for logging in.'
51ax = autox.AutoX()
52# navigate to login screen
53ax.send_hotkey('Ctrl+Shift+q')
54ax.send_hotkey('Ctrl+Alt+l')
55# escape out of any login screen menus (e.g., the network select menu)
56time.sleep(2)
57ax.send_hotkey('Escape')
58time.sleep(2)
59ax.send_hotkey('Tab')
60time.sleep(0.5)
61ax.send_hotkey('Tab')
62time.sleep(0.5)
63ax.send_hotkey('Tab')
64time.sleep(0.5)
65ax.send_hotkey('Tab')
66time.sleep(0.5)
67ax.send_hotkey('Return')
68print 'Waiting for Chrome to appear...'
69while True:
70 if os.path.isfile('%s'):
71 break
72 time.sleep(1)
73print 'Done'
asharifd2ced682013-02-16 01:05:17 +000074""" % LOGGED_IN_MAGIC_FILE
asharifd8136e42013-02-15 23:18:03 +000075
Luis Lozanof2a3ef42015-12-15 13:49:30 -080076
asharifd2ced682013-02-16 01:05:17 +000077def RestartUI(remote, chromeos_root, login=True):
asharifd8136e42013-02-15 23:18:03 +000078 chromeos_root = os.path.expanduser(chromeos_root)
79 ce = command_executer.GetCommandExecuter()
80 # First, restart ui.
asharifd2ced682013-02-16 01:05:17 +000081 command = 'rm -rf %s && restart ui' % LOGIN_PROMPT_VISIBLE_MAGIC_FILE
Luis Lozanof2a3ef42015-12-15 13:49:30 -080082 ce.CrosRunCommand(command, machine=remote, chromeos_root=chromeos_root)
asharif54cf6782013-02-16 02:11:03 +000083 host_login_script = tempfile.mktemp()
84 device_login_script = '/tmp/login.py'
asharifd2ced682013-02-16 01:05:17 +000085 login_script_list = [script_header, wait_for_login_screen]
86 if login:
87 login_script_list.append(do_login)
88
Luis Lozanof2a3ef42015-12-15 13:49:30 -080089 full_login_script_contents = '\n'.join(login_script_list)
asharifd2ced682013-02-16 01:05:17 +000090
asharif54cf6782013-02-16 02:11:03 +000091 with open(host_login_script, 'w') as f:
asharifd8136e42013-02-15 23:18:03 +000092 f.write(full_login_script_contents)
asharif54cf6782013-02-16 02:11:03 +000093 ce.CopyFiles(host_login_script,
94 device_login_script,
asharifd8136e42013-02-15 23:18:03 +000095 dest_machine=remote,
96 chromeos_root=chromeos_root,
97 recursive=False,
98 dest_cros=True)
asharif54cf6782013-02-16 02:11:03 +000099 ret = ce.CrosRunCommand('python %s' % device_login_script,
100 chromeos_root=chromeos_root,
101 machine=remote)
102 if os.path.exists(host_login_script):
103 os.remove(host_login_script)
104 return ret
asharifd8136e42013-02-15 23:18:03 +0000105
106
107def Main(argv):
108 """The main function."""
109 parser = optparse.OptionParser()
110 parser.add_option('-r',
111 '--remote',
112 dest='remote',
113 help='The remote ChromeOS box.')
114 parser.add_option('-c',
115 '--chromeos_root',
116 dest='chromeos_root',
117 help='The ChromeOS root.')
118
119 options, args = parser.parse_args(argv)
120
asharif54cf6782013-02-16 02:11:03 +0000121 return RestartUI(options.remote, options.chromeos_root)
asharifd8136e42013-02-15 23:18:03 +0000122
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800123
asharifd8136e42013-02-15 23:18:03 +0000124if __name__ == '__main__':
125 retval = Main(sys.argv)
asharif5ab1c7d2013-02-15 23:43:58 +0000126 sys.exit(retval)