Mike Frysinger | c7f1593 | 2013-03-20 13:43:35 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | |
| 5 | """Script to get past the login screen of ChromeOS. |
| 6 | |
| 7 | """ |
| 8 | |
| 9 | __author__ = "asharif@google.com (Ahmad Sharif)" |
| 10 | |
| 11 | import datetime |
| 12 | import fcntl |
| 13 | import getpass |
| 14 | import glob |
| 15 | import optparse |
| 16 | import os |
| 17 | import pickle |
| 18 | import socket |
| 19 | import sys |
| 20 | import time |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 21 | import tempfile |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 22 | from utils import logger |
| 23 | from utils import command_executer |
| 24 | |
| 25 | LOGIN_PROMPT_VISIBLE_MAGIC_FILE = '/tmp/uptime-login-prompt-visible' |
| 26 | LOGGED_IN_MAGIC_FILE = '/var/run/state/logged-in' |
| 27 | |
| 28 | |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 29 | script_header=""" |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 30 | import os |
| 31 | import autox |
| 32 | import time |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 33 | """ |
| 34 | |
| 35 | wait_for_login_screen=""" |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 36 | |
| 37 | while True: |
| 38 | print 'Waiting for login screen to appear...' |
| 39 | if os.path.isfile('%s'): |
| 40 | break |
| 41 | time.sleep(1) |
| 42 | print 'Done' |
| 43 | |
| 44 | time.sleep(20) |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 45 | """ % LOGIN_PROMPT_VISIBLE_MAGIC_FILE |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 46 | |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 47 | |
| 48 | do_login=""" |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 49 | xauth_filename = '/home/chronos/.Xauthority' |
| 50 | os.environ.setdefault('XAUTHORITY', xauth_filename) |
| 51 | os.environ.setdefault('DISPLAY', ':0.0') |
| 52 | |
| 53 | print 'Now sending the hotkeys for logging in.' |
| 54 | ax = autox.AutoX() |
| 55 | # navigate to login screen |
| 56 | ax.send_hotkey('Ctrl+Shift+q') |
| 57 | ax.send_hotkey('Ctrl+Alt+l') |
| 58 | # escape out of any login screen menus (e.g., the network select menu) |
| 59 | time.sleep(2) |
| 60 | ax.send_hotkey('Escape') |
| 61 | time.sleep(2) |
| 62 | ax.send_hotkey('Tab') |
| 63 | time.sleep(0.5) |
| 64 | ax.send_hotkey('Tab') |
| 65 | time.sleep(0.5) |
| 66 | ax.send_hotkey('Tab') |
| 67 | time.sleep(0.5) |
| 68 | ax.send_hotkey('Tab') |
| 69 | time.sleep(0.5) |
| 70 | ax.send_hotkey('Return') |
| 71 | print 'Waiting for Chrome to appear...' |
| 72 | while True: |
| 73 | if os.path.isfile('%s'): |
| 74 | break |
| 75 | time.sleep(1) |
| 76 | print 'Done' |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 77 | """ % LOGGED_IN_MAGIC_FILE |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 78 | |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 79 | def RestartUI(remote, chromeos_root, login=True): |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 80 | chromeos_root = os.path.expanduser(chromeos_root) |
| 81 | ce = command_executer.GetCommandExecuter() |
| 82 | # First, restart ui. |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 83 | command = 'rm -rf %s && restart ui' % LOGIN_PROMPT_VISIBLE_MAGIC_FILE |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 84 | ce.CrosRunCommand(command, machine=remote, |
| 85 | chromeos_root=chromeos_root) |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 86 | host_login_script = tempfile.mktemp() |
| 87 | device_login_script = '/tmp/login.py' |
asharif | d2ced68 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 88 | login_script_list = [script_header, wait_for_login_screen] |
| 89 | if login: |
| 90 | login_script_list.append(do_login) |
| 91 | |
| 92 | full_login_script_contents = "\n".join(login_script_list) |
| 93 | |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 94 | with open(host_login_script, 'w') as f: |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 95 | f.write(full_login_script_contents) |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 96 | ce.CopyFiles(host_login_script, |
| 97 | device_login_script, |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 98 | dest_machine=remote, |
| 99 | chromeos_root=chromeos_root, |
| 100 | recursive=False, |
| 101 | dest_cros=True) |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 102 | ret = ce.CrosRunCommand('python %s' % device_login_script, |
| 103 | chromeos_root=chromeos_root, |
| 104 | machine=remote) |
| 105 | if os.path.exists(host_login_script): |
| 106 | os.remove(host_login_script) |
| 107 | return ret |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 108 | |
| 109 | |
| 110 | def Main(argv): |
| 111 | """The main function.""" |
| 112 | parser = optparse.OptionParser() |
| 113 | parser.add_option('-r', |
| 114 | '--remote', |
| 115 | dest='remote', |
| 116 | help='The remote ChromeOS box.') |
| 117 | parser.add_option('-c', |
| 118 | '--chromeos_root', |
| 119 | dest='chromeos_root', |
| 120 | help='The ChromeOS root.') |
| 121 | |
| 122 | options, args = parser.parse_args(argv) |
| 123 | |
asharif | 54cf678 | 2013-02-16 02:11:03 +0000 | [diff] [blame] | 124 | return RestartUI(options.remote, options.chromeos_root) |
asharif | d8136e4 | 2013-02-15 23:18:03 +0000 | [diff] [blame] | 125 | |
| 126 | if __name__ == '__main__': |
| 127 | retval = Main(sys.argv) |
asharif | 5ab1c7d | 2013-02-15 23:43:58 +0000 | [diff] [blame] | 128 | sys.exit(retval) |