1# 2# CDDL HEADER START 3# 4# The contents of this file are subject to the terms of the 5# Common Development and Distribution License (the "License"). 6# You may not use this file except in compliance with the License. 7# 8# See LICENSE.txt included in this distribution for the specific 9# language governing permissions and limitations under the License. 10# 11# When distributing Covered Code, include this CDDL HEADER in each 12# file and include the License file at LICENSE.txt. 13# If applicable, add the following below this CDDL HEADER, with the 14# fields enclosed by brackets "[]" replaced with your own identifying 15# information: Portions Copyright [yyyy] [name of copyright owner] 16# 17# CDDL HEADER END 18# 19 20# 21# Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. 22# 23 24import os 25from shutil import which 26from logging import log 27import logging 28import sys 29from distutils import util 30from .exitvals import ( 31 FAILURE_EXITVAL, 32) 33 34 35def is_exe(fpath): 36 return os.path.isfile(fpath) and os.access(fpath, os.X_OK) 37 38 39def check_create_dir(logger, path): 40 """ 41 Make sure the directory specified by the path exists. If unsuccessful, 42 exit the program. 43 """ 44 if not os.path.isdir(path): 45 try: 46 os.makedirs(path) 47 except OSError: 48 logger.error("cannot create {} directory".format(path)) 49 sys.exit(FAILURE_EXITVAL) 50 51 52def get_command(logger, path, name, level=logging.ERROR): 53 """ 54 Get the path to the command specified by path and name. 55 If the path does not contain executable, search for the command 56 according to name in OS environment and/or dirname. 57 58 The logging level can be used to set different log level to error messages. 59 This is handy when trying to determine optional command. 60 61 Return path to the command or None. 62 """ 63 64 cmd_file = None 65 if path: 66 cmd_file = which(path) 67 if not is_exe(cmd_file): 68 log(level, "file {} is not executable file". 69 format(path)) 70 return None 71 else: 72 cmd_file = which(name) 73 if not cmd_file: 74 # try to search within dirname() 75 cmd_file = which(name, 76 path=os.path.dirname(sys.argv[0])) 77 if not cmd_file: 78 log(level, "cannot determine path to the {} command". 79 format(name)) 80 return None 81 logger.debug("{} = {}".format(name, cmd_file)) 82 83 return cmd_file 84 85 86def get_int(logger, name, value): 87 """ 88 If the supplied value is integer, return it. Otherwise return None. 89 """ 90 if not value: 91 return None 92 93 try: 94 return int(value) 95 except ValueError: 96 logger.error("'{}' is not a number: {}".format(name, value)) 97 return None 98 99 100def get_bool(logger, name, value): 101 """ 102 If the supplied value is bool or its representation, return the bool value. 103 Otherwise return None. 104 """ 105 if value is None: 106 return None 107 108 if type(value) is bool: 109 return value 110 111 try: 112 return bool(util.strtobool(value)) 113 except ValueError: 114 logger.error("'{}' is not a number: {}".format(name, value)) 115 return None 116