| Kevin Cheng | 6001db3 | 2018-10-23 12:34:20 -0700 | [diff] [blame] | 1 | # 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. |
| 14 | r"""Acloud metrics functions.""" |
| 15 | |
| 16 | import logging |
| 17 | import os |
| 18 | import subprocess |
| 19 | |
| 20 | from acloud.internal import constants |
| 21 | # pylint: disable=import-error |
| 22 | from 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 | |
| 28 | logger = logging.getLogger(__name__) |
| 29 | |
| 30 | |
| 31 | def LogUsage(): |
| 32 | """Log acloud run.""" |
| 33 | asuite_metrics.log_event(_METRICS_URL, dummy_key_fallback=False, |
| 34 | ldap=_GetLdap()) |
| 35 | |
| 36 | |
| 37 | def _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 |