blob: 87b439eef97c9c4b6305d8a4f2c708608f789577 [file] [log] [blame]
Kevin Cheng6001db32018-10-23 12:34:20 -07001# Copyright 2018 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14r"""Acloud metrics functions."""
15
16import logging
17import os
18import subprocess
19
20from acloud.internal import constants
21# pylint: disable=import-error
22from asuite import asuite_metrics
23
24_METRICS_URL = 'http://asuite-218222.appspot.com/acloud/metrics'
25_VALID_DOMAINS = ["google.com", "android.com"]
26_COMMAND_GIT_CONFIG = ["git", "config", "--get", "user.email"]
27
28logger = logging.getLogger(__name__)
29
30
31def LogUsage():
32 """Log acloud run."""
33 asuite_metrics.log_event(_METRICS_URL, dummy_key_fallback=False,
34 ldap=_GetLdap())
35
36
37def _GetLdap():
38 """Return string email username for valid domains only, None otherwise."""
39 try:
40 acloud_project = os.path.join(
41 os.environ[constants.ENV_ANDROID_BUILD_TOP], "tools", "acloud")
42 email = subprocess.check_output(_COMMAND_GIT_CONFIG,
43 cwd=acloud_project).strip()
44 ldap, domain = email.split("@", 2)
45 if domain in _VALID_DOMAINS:
46 return ldap
47 # pylint: disable=broad-except
48 except Exception as e:
49 logger.debug("error retrieving email: %s", e)
50 return None