Get boolean input from user. If var_name is not set in env, ask user to enable query_item or not. If the response is empty, use the default. Args: environ_cp: copy of the os.environ. var_name: string for name of environment variable, e.g. "TF_NEED_CUDA". query_item: string for fe
(environ_cp,
var_name,
query_item,
enabled_by_default,
question=None,
yes_reply=None,
no_reply=None)
| 282 | |
| 283 | |
| 284 | def get_var(environ_cp, |
| 285 | var_name, |
| 286 | query_item, |
| 287 | enabled_by_default, |
| 288 | question=None, |
| 289 | yes_reply=None, |
| 290 | no_reply=None): |
| 291 | """Get boolean input from user. |
| 292 | |
| 293 | If var_name is not set in env, ask user to enable query_item or not. If the |
| 294 | response is empty, use the default. |
| 295 | |
| 296 | Args: |
| 297 | environ_cp: copy of the os.environ. |
| 298 | var_name: string for name of environment variable, e.g. "TF_NEED_CUDA". |
| 299 | query_item: string for feature related to the variable, e.g. "CUDA for |
| 300 | Nvidia GPUs". |
| 301 | enabled_by_default: boolean for default behavior. |
| 302 | question: optional string for how to ask for user input. |
| 303 | yes_reply: optional string for reply when feature is enabled. |
| 304 | no_reply: optional string for reply when feature is disabled. |
| 305 | |
| 306 | Returns: |
| 307 | boolean value of the variable. |
| 308 | |
| 309 | Raises: |
| 310 | UserInputError: if an environment variable is set, but it cannot be |
| 311 | interpreted as a boolean indicator, assume that the user has made a |
| 312 | scripting error, and will continue to provide invalid input. |
| 313 | Raise the error to avoid infinitely looping. |
| 314 | """ |
| 315 | if not question: |
| 316 | question = 'Do you wish to build TensorFlow with %s support?' % query_item |
| 317 | if not yes_reply: |
| 318 | yes_reply = '%s support will be enabled for TensorFlow.' % query_item |
| 319 | if not no_reply: |
| 320 | no_reply = 'No %s' % yes_reply |
| 321 | |
| 322 | yes_reply += '\n' |
| 323 | no_reply += '\n' |
| 324 | |
| 325 | if enabled_by_default: |
| 326 | question += ' [Y/n]: ' |
| 327 | else: |
| 328 | question += ' [y/N]: ' |
| 329 | |
| 330 | var = environ_cp.get(var_name) |
| 331 | if var is not None: |
| 332 | var_content = var.strip().lower() |
| 333 | true_strings = ('1', 't', 'true', 'y', 'yes') |
| 334 | false_strings = ('0', 'f', 'false', 'n', 'no') |
| 335 | if var_content in true_strings: |
| 336 | var = True |
| 337 | elif var_content in false_strings: |
| 338 | var = False |
| 339 | else: |
| 340 | raise UserInputError( |
| 341 | 'Environment variable %s must be set as a boolean indicator.\n' |
no test coverage detected