google_api_keys.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 The Chromium 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. """Python API for retrieving API keys.
  6. Note that this does not have the exact same semantics as the C++ API
  7. in google_api_keys.h, since it does not have access to gyp variables
  8. or preprocessor defines.
  9. """
  10. from __future__ import print_function
  11. import os
  12. import re
  13. import sys
  14. # The token returned when an API key is unset.
  15. DUMMY_TOKEN = 'dummytoken'
  16. def _GetTokenFromOfficialFile(token_name):
  17. """Parses the token from the official file if it exists, else returns None."""
  18. official_path = os.path.join(os.path.dirname(__file__),
  19. 'internal/google_chrome_api_keys.h')
  20. if not os.path.isfile(official_path):
  21. return None
  22. line_regexp = '^#define\s*%s\s*"([^"]+)"' % token_name
  23. line_pattern = re.compile(line_regexp)
  24. def ParseLine(current_line):
  25. result = line_pattern.match(current_line)
  26. if result:
  27. return result.group(1)
  28. else:
  29. return None
  30. with open(official_path) as f:
  31. current_line = ''
  32. for line in f:
  33. if line.endswith('\\\n'):
  34. current_line += line[:-2]
  35. else:
  36. # Last line in multi-line #define, or a line that is not a
  37. # continuation line.
  38. current_line += line
  39. token = ParseLine(current_line)
  40. if token:
  41. if current_line.count('"') != 2:
  42. raise Exception(
  43. 'Embedded quotes and multi-line strings are not supported.')
  44. return token
  45. current_line = ''
  46. return None
  47. def _GetToken(token_name):
  48. """Returns the API token with the given name, or DUMMY_TOKEN by default."""
  49. if token_name in os.environ:
  50. return os.environ[token_name]
  51. token = _GetTokenFromOfficialFile(token_name)
  52. if token:
  53. return token
  54. else:
  55. return DUMMY_TOKEN
  56. def GetAPIKey():
  57. """Returns the simple API key."""
  58. return _GetToken('GOOGLE_API_KEY')
  59. def GetAPIKeyAndroidNonStable():
  60. """Returns the API key for android non-stable channel."""
  61. return _GetToken('GOOGLE_API_KEY_ANDROID_NON_STABLE')
  62. def GetClientID(client_name):
  63. """Returns the OAuth 2.0 client ID for the client of the given name."""
  64. return _GetToken('GOOGLE_CLIENT_ID_%s' % client_name)
  65. def GetClientSecret(client_name):
  66. """Returns the OAuth 2.0 client secret for the client of the given name."""
  67. return _GetToken('GOOGLE_CLIENT_SECRET_%s' % client_name)
  68. if __name__ == "__main__":
  69. print('GOOGLE_API_KEY=%s' % GetAPIKey())
  70. print('GOOGLE_CLIENT_ID_MAIN=%s' % GetClientID('MAIN'))
  71. print('GOOGLE_CLIENT_SECRET_MAIN=%s' % GetClientSecret('MAIN'))
  72. print('GOOGLE_CLIENT_ID_REMOTING=%s' % GetClientID('REMOTING'))
  73. print('GOOGLE_CLIENT_SECRET_REMOTING=%s' % GetClientSecret('REMOTING'))
  74. print('GOOGLE_CLIENT_ID_REMOTING_HOST=%s' % GetClientID('REMOTING_HOST'))
  75. print('GOOGLE_CLIENT_SECRET_REMOTING_HOST=%s' % GetClientSecret(
  76. 'REMOTING_HOST'))
  77. print('GOOGLE_CLIENT_ID_REMOTING_IDENTITY_API=%s' %GetClientID(
  78. 'REMOTING_IDENTITY_API'))