Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 | |
| 7 import getpass | |
| 8 import os | |
| 9 import re | |
| 10 import subprocess | |
| 11 import sys | |
| 12 import tempfile | |
| 13 import urllib | |
| 14 | |
| 15 | |
| 16 def Authenticate(): | |
| 17 # Authenticate. | |
| 18 default_username = os.environ.get('LOGNAME', '') | |
|
jrbarnette
2010/08/04 01:31:40
What's wrong with getpass.getuser()?
| |
| 19 username = os.environ.get('GSDCURL_USERNAME') | |
| 20 if username is None: | |
| 21 sys.stderr.write('Username [' + default_username + ']: ') | |
| 22 username = raw_input() | |
| 23 if username == '': | |
| 24 username = default_username + '@google.com' | |
| 25 elif '@' not in username: | |
| 26 username = username + '@google.com' | |
| 27 passwd = os.environ.get('GSDCURL_PASSWORD') | |
| 28 if passwd is None: | |
| 29 sys.stderr.write('Password: ') | |
| 30 passwd = urllib.quote_plus(getpass.getpass(prompt='')) | |
| 31 cmd = [ | |
| 32 'curl', '--silent', 'https://www.google.com/accounts/ClientLogin', | |
| 33 '-d', 'Email=' + username, | |
| 34 '-d', 'Passwd=' + passwd, | |
| 35 '-d', 'accountType=GOOGLE', | |
| 36 '-d', 'source=Google-gsdcurl-ver1', | |
| 37 '-d', 'service=cds', | |
| 38 ] | |
| 39 p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
| 40 (p_stdout, _) = p.communicate() | |
| 41 assert p.returncode == 0 | |
| 42 m = re.search('\nAuth=([^\n]+)\n', p_stdout) | |
| 43 if not m: | |
| 44 sys.stderr.write('BAD LOGIN\n') | |
| 45 sys.exit(1) | |
| 46 auth = m.group(1) | |
| 47 return auth | |
| 48 | |
| 49 | |
| 50 def DoCurl(auth, argv): | |
| 51 (_, cookies) = tempfile.mkstemp(prefix='gsdcookie') | |
| 52 cmd = [ | |
| 53 'curl', '-L', | |
| 54 '-b', cookies, '-c', cookies, | |
| 55 '--header', 'Authorization: GoogleLogin auth=' + auth, | |
| 56 ] + argv[1:] | |
| 57 try: | |
| 58 p = subprocess.Popen(cmd) | |
| 59 return p.wait() | |
| 60 finally: | |
| 61 os.remove(cookies) | |
| 62 | |
| 63 | |
| 64 def main(argv): | |
| 65 auth = Authenticate() | |
| 66 return DoCurl(auth, argv) | |
| 67 | |
| 68 | |
| 69 if __name__ == '__main__': | |
| 70 sys.exit(main(sys.argv)) | |
| OLD | NEW |