Zhuoyao Zhang | 3ca7cef | 2024-10-31 22:07:31 +0000 | [diff] [blame] | 1 | # Copyright 2024, 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 | |
| 15 | import hashlib |
| 16 | import logging |
| 17 | import os |
| 18 | |
| 19 | |
| 20 | def is_feature_enabled( |
| 21 | feature_name: str, |
| 22 | user_name: str, |
| 23 | enable_flag: str = None, |
Zhuoyao Zhang | a5bec26 | 2024-11-05 19:28:23 +0000 | [diff] [blame] | 24 | rollout_percent: int = 100, |
Zhuoyao Zhang | 3ca7cef | 2024-10-31 22:07:31 +0000 | [diff] [blame] | 25 | ) -> bool: |
| 26 | """Determine whether the given feature is enabled. |
| 27 | |
| 28 | Whether a given feature is enabled or not depends on two flags: 1) the |
| 29 | enable_flag that explicitly enable/disable the feature and 2) the rollout_flag |
| 30 | that controls the rollout percentage. |
| 31 | |
| 32 | Args: |
| 33 | feature_name: name of the feature. |
| 34 | user_name: system user name. |
| 35 | enable_flag: name of the env var that enables/disables the feature |
| 36 | explicitly. |
| 37 | rollout_flg: name of the env var that controls the rollout percentage, the |
| 38 | value stored in the env var should be an int between 0 and 100 string |
| 39 | """ |
| 40 | if enable_flag: |
| 41 | if os.environ.get(enable_flag, "") == "false": |
| 42 | logging.info("feature: %s is disabled", feature_name) |
| 43 | return False |
| 44 | |
| 45 | if os.environ.get(enable_flag, "") == "true": |
| 46 | logging.info("feature: %s is enabled", feature_name) |
| 47 | return True |
| 48 | |
Zhuoyao Zhang | 3ca7cef | 2024-10-31 22:07:31 +0000 | [diff] [blame] | 49 | hash_object = hashlib.sha256() |
| 50 | hash_object.update((user_name + feature_name).encode("utf-8")) |
| 51 | hash_number = int(hash_object.hexdigest(), 16) % 100 |
| 52 | |
Zhuoyao Zhang | a5bec26 | 2024-11-05 19:28:23 +0000 | [diff] [blame] | 53 | return hash_number < rollout_percent |