Best Python code snippet using autotest_python
os_dep.py
Source:os_dep.py
...79 :return: true if the path is a file and R_OK80 :rtype: bool81 """82 return os.path.isfile(pth) and os.access(pth, os.R_OK)83def make_path_searcher(path_generator, target_predicate, target_normalizer, extra_paths, **kwargs):84 """85 Universal search function generator using lazy evaluation.86 Generate a function that will iterate over all the paths from path_generator using87 target_predicate to filter matching paths. Each matching path is then noramlized by target_predicate.88 Only the first match is returned.89 :param path_generator: all paths to test with target_predicate90 :type path_generator: iterator91 :param target_predicate: boolean function that tests a given path92 :type target_predicate: function93 :param target_normalizer: function that transforms a matching path to some noramlized form94 :type target_normalizer: function95 :param extra_paths: extra paths to pass to the path_generator96 :type extra_paths: iterator97 :return: the path searching function98 :rtype: function99 """100 def path_searcher(target, extra_dirs=extra_paths):101 matches = itertools.ifilter(102 target_predicate, path_generator(target, extra_dirs, **kwargs))103 paths = itertools.imap(target_normalizer, matches)104 return next(paths, '')105 return path_searcher106def unique_not_false_list(arg_paths):107 # probably better than an ordered dict or ordered set108 included = set()109 # preserve ordering while filtering out duplicates110 search_paths = []111 for p in arg_paths:112 # remove any empty paths113 if p and p not in included:114 included.add(p)115 search_paths.append(p)116 return search_paths117def generate_bin_search_paths(program, extra_dirs):118 """119 Generate full paths of potential locations of a given binary file based on120 COMMON_BIN_PATHS.121 Use the enviroment variable $PATH seed the list of search directories.122 :param program: library filename to join with all search directories123 :type program: str124 :param extra_dirs: extra directories to append to the directory search list125 :type extra_dirs: str126 :return: iterator over all generated paths127 :rtype: iter128 """129 # relative paths are accepted so don't use os.path.isabs()130 if os.sep in program:131 # if program already contains path then only check that path132 # e.g. `which bin/more` will succeed from /133 paths = [program]134 else:135 # `which` fails if PATH is empty, replicate this by returning '' when PATH is empty136 # such that ''.split(os.pathsep) == [''] which is filtered out137 arg_paths = itertools.chain(138 os.environ.get('PATH', '').split(os.pathsep), extra_dirs)139 # remove any empty paths and duplicates140 search_paths = unique_not_false_list(arg_paths)141 paths = path_joiner(program, search_paths)142 return paths143which = make_path_searcher(144 generate_bin_search_paths, is_file_and_rx, os.path.abspath, COMMON_BIN_PATHS)145which.__name__ = "which"146which.__doc__ = """147Find a program by searching in the environment path and in common binary paths.148check both if it is a file and executable149`which` always returns the abspath150return '' if failure because '' is well-defined NULL path, so it is151better than None or ValueError152:param program: command name or path to command153:type program: str154:param extra_dirs: iterable of extra paths to search155:type extra_dirs: iterble156:return: abspath of command if found, else ''157:rtype: str158"""159command = exception_when_false_wrapper(160 which, ValueError, 'Missing command: %s')161command.__name__ = "command"162command.__doc__ = """163Find a program by searching in the environment path and in common binary paths.164check both if it is a file and executable165`which` always returns the abspath166return '' if failure because '' is well-defined NULL path, so it is167better than None or ValueError168:param program: command name or path to command169:type program: str170:param extra_dirs: iterable of extra paths to search171:type extra_dirs: iterable172:return: abspath of command if found173:rtype: str174:exception ValueError: when program not found175"""176def commands(*cmds):177 return [command(c) for c in cmds]178# Don't be smart and try to guess the architecture because we could be179# on a multi-arch system180COMMON_LIB_BASE_PATHS = ['/lib', '/usr/lib', '/lib64', '/usr/lib64']181# the TLS hwcap is always added by ldconfig, so pre-generate the tls paths182# ldconfig.c:1276: hwcap_extra[63 - _DL_FIRST_EXTRA] = "tls";183COMMON_LIB_TLS_PATHS = [os.path.join(p, 'tls') for p in COMMON_LIB_BASE_PATHS]184# convert to tuple so when used as default argument it is not mutable185COMMON_LIB_PATHS = tuple(COMMON_LIB_BASE_PATHS + COMMON_LIB_TLS_PATHS)186class Ldconfig(object):187 LD_SO_CONF = "/etc/ld.so.conf"188 MAX_RECURSION_DEPTH = 20189 class DirEntry(object):190 def __init__(self, path, flag, ino, dev):191 """192 Replica of ldconfig.c struct dir_entry. Meant to hold ldconfig directories.193 In order to detect duplicates the inode and device number are compared on insert.194 /* List of directories to handle. */195 struct dir_entry196 {197 char *path;198 int flag;199 ino64_t ino;200 dev_t dev;201 struct dir_entry *next;202 };203 :param path: library path204 :type path: str205 :param flag: string like 'libc4','libc5', 'libc6', 'glibc2'206 :type flag: str207 :param ino: inode number208 :type ino: int209 :param dev: id of device containing file210 :type dev: long211 """212 self.path = path213 self.flag = flag214 self.ino = ino215 self.dev = dev216 def __eq__(self, other):217 """218 Compare DirEntry based only on inode and device number219 :param other: other DirEntry220 :type other: DirEntry221 :return: True iff ino and dev are equal222 :rtype: bool223 """224 return self.ino == other.ino and self.dev == other.dev225 def __ne__(self, other):226 return not self == other227 def __repr__(self):228 return self.__class__.__name__ + "(%(path)r, %(flag)r, %(ino)r, %(dev)r)" % vars(self)229 def __init__(self):230 """231 This class is meant duplicate the behaviour of ldconfig and parse232 /etc/ld.so.conf and all the related config files Since the only specification233 for ldconfig is in the source code, this class is as much as possible a234 line-by-line direct translation from the C to Python.235 Currently we attempt to preserve the following behaviours, with caveats236 * include parsing is recursive, included files can include /etc/ld.so.conf,237 ldconfig.c has it's recursion depth limited by the process max file open238 limit. We artifically limit the recursion depth to MAX_RECURSION_DEPTH239 * The library type suffix, .e.g. '/usr/lib/foo=glibc2' is correctly parsed and240 stored. There can be any amount of whitespace between the end of the path241 and the type suffix. We do not overwrite duplicate paths with new flag242 information.243 * hwcap is currently ignored. Ideally we would parse the hwcap and add those244 directories to the search path based on runtime parsing of the HW245 capabilities set in /proc/cpuinfo, but that is not implemented.246 * The hardcoded hwcap of 'tls' is added to COMMON_LIB_PATHS during runtime247 constant definition. This means we search /usr/lib64/tls by default.248 This current translation is based on elf/ldconfig.c from glibc-2.16-34.fc18.src.rpm249 """250 self.lddirs = []251 def _parse_config_line(self, config_file, filename, recursion):252 for line in config_file:253 line = line.strip()254 line = line.split('#')[0]255 if not line:256 continue257 # hardcoded to 'include' + space258 if line.startswith('include '):259 glob_patterns = line.split('include ')[1]260 # include supports multiple files split by whitespace261 for glob_pattern in glob_patterns.split():262 self._parse_conf_include(263 filename, glob_pattern, recursion)264 # hardcoded to 'hwcap' + space265 elif line.startswith("hwcap "):266 # ignore hwcap lines, but they do point to alternate directories267 # based on runtime processor capabilities in /proc/cpuinfo268 # .e.g. hwcap 0 nosegneg would add /lib/i686/nosegneg/libc.so.6269 continue270 else:271 self._add_dir(line)272 def parse_conf(self, filename=LD_SO_CONF, recursion=0):273 # print filename274 if recursion < self.MAX_RECURSION_DEPTH:275 # read lddirs from main ld.so.conf file276 try:277 config_file = open(filename, 'r')278 except IOError:279 return280 try:281 self._parse_config_line(config_file, filename, recursion)282 finally:283 config_file.close()284 def _parse_conf_include(self, filename, glob_pattern, recursion):285 # os.path.dirname will succeed if os.sep is in filename286 if not os.path.isabs(glob_pattern) and os.sep in filename:287 # prepend with dirname of filename, e.g. /etc if /etc/ld.so.conf288 glob_pattern = os.path.join(289 os.path.dirname(filename), glob_pattern)290 glob_result = glob(glob_pattern)291 for conf_file in glob_result:292 # increment recusion so can limit depth293 self.parse_conf(conf_file, recursion + 1)294 def _add_single_dir(self, new_dir_entry):295 if new_dir_entry in self.lddirs:296 logging.debug("Path %s given more than once", new_dir_entry.path)297 else:298 self.lddirs.append(new_dir_entry)299 def _add_dir(self, line):300 # extract lib_type suffix, e.g. 'dirname=TYPE' where TYPE is in 'libc4',301 # 'libc5', 'libc6', 'glibc2'302 if '=' in line:303 path, flag = line.split('=', 1)304 else:305 path = line306 flag = ''307 path = path.rstrip()308 path = path.rstrip(os.sep)309 try:310 stat = os.stat(path)311 de = Ldconfig.DirEntry(path, flag, stat.st_ino, stat.st_dev)312 self._add_single_dir(de)313 except (IOError, OSError):314 logging.debug("Can't stat %s", path)315 def ldconfig(self, ld_so_conf_filename=LD_SO_CONF, extra_dirs=COMMON_LIB_PATHS):316 """317 Read and parse /etc/ld.so.conf to generate a list of directories that ldconfig would search.318 Pre-seed the search directory list with ('/lib', '/usr/lib', '/lib64', '/usr/lib64')319 :param ld_so_conf_filename: path to /etc/ld.so.conf320 :type ld_so_conf_filename: str321 :param extra_dirs:322 :type extra_dirs: iterable323 :return: iterator over the directories found324 :rtype: iterable325 """326 self.lddirs = []327 for d in extra_dirs:328 self._add_dir(d)329 self.parse_conf(ld_so_conf_filename)330 # only return the paths331 return (ld.path for ld in self.lddirs)332def generate_library_search_paths(lib, extra_dirs=COMMON_LIB_PATHS, ld_so_conf_filename=Ldconfig.LD_SO_CONF):333 """334 Generate full paths of potential locations of a given library file based on335 COMMON_LIB_PATHS.336 :param lib: library filename to join with all search directories337 :type lib: str338 :param extra_dirs: extra directories to append to the directory search list339 :type extra_dirs: iterable340 :param ld_so_conf_filename: location of /etc/ld.so.conf to parse to find all system library locations341 :type ld_so_conf_filename: str342 :return: iterator over all generated paths343 :rtype: iterable344 """345 if os.sep in lib:346 # is program already contains path then only check that path347 paths = [lib]348 else:349 l = Ldconfig()350 search_paths = l.ldconfig(ld_so_conf_filename, extra_dirs)351 paths = path_joiner(lib, search_paths)352 return paths353which_library = make_path_searcher(354 generate_library_search_paths, is_file_and_readable, os.path.abspath, COMMON_LIB_PATHS)355which_library.__name__ = "which_library"356which_library.__doc__ = """357Find a library file by parsing /etc/ld.so.conf and also searcing in the common library search paths, %s358Check both if the library is a file and readable.359:param lib: library file or path to library file, e.g. libc.so.6360:type lib: str361:param extra_dirs: iterable of extra paths to search362:type extra_dirs: iterable363:return: abspath of library if found, else ''364:rtype: str365""" % str(COMMON_LIB_PATHS)366library = exception_when_false_wrapper(367 which_library, ValueError, 'Missing library: %s')368library.__name__ = "library"369library.__doc__ = """370Find a library file by parsing /etc/ld.so.conf and also searcing in the common library search paths, %s371Check both if the library is a file and readable.372:param lib: library file or path to library file, e.g. libc.so.6373:type lib: str374:param extra_dirs: iterable of extra paths to search375:type extra_dirs: iterable376:return: abspath of library if found377:rtype: str378:exception ValueError: when library is not found379""" % str(COMMON_LIB_PATHS)380def libraries(*libs):381 return [library(l) for l in libs]382COMMON_HEADER_PATHS = ('/usr/include', '/usr/local/include')383def generate_include_search_paths(hdr, extra_dirs):384 """385 Generate full paths of potential locations of a given header file based on386 COMMON_HEADER_PATHS.387 :param hdr: header filename to join with all search directories388 :type hdr: str389 :param extra_dirs: extra directories to append to the directory search list390 :type extra_dirs: iterable391 :return: iterator over all generated paths392 :rtype: iterable393 """394 if os.sep in hdr:395 # is program already contains path then only check that path396 paths = [hdr]397 else:398 # `which` fails if PATH is empty, replicate this by returning '' when PATH is empty399 # such that ''.split(os.pathsep) == [''] which is filtered out400 arg_paths = itertools.chain(COMMON_HEADER_PATHS, extra_dirs)401 # remove any empty paths and duplicates402 search_paths = unique_not_false_list(arg_paths)403 paths = path_joiner(hdr, search_paths)404 # `which` always returns the abspath405 return paths406which_header = make_path_searcher(407 generate_include_search_paths, is_file_and_readable, os.path.abspath, frozenset([]))408which_header.__name__ = "which_header"409which_header.__doc__ = """410Find a header file by searching in the common include search paths, %s411Check both if the header is a file and readable.412:param hdr: header file or path to header file, e.g. stdio.h413:type hdr: str414:param extra_dirs: iterable of extra paths to search415:type extra_dirs: iterable416:return: abspath of header if found, else ''417:rtype: str418""" % str(COMMON_HEADER_PATHS)419header = exception_when_false_wrapper(420 which_header, ValueError, 'Missing header: %s')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!