Best Python code snippet using autotest_python
base_classes.py
Source:base_classes.py  
...328            finally:329                del self.RUNNING_LOG_OP330        else:331            op_func()332    def list_files_glob(self, glob):333        """334        Get a list of files on a remote host given a glob pattern path.335        """336        SCRIPT = ("python -c 'import cPickle, glob, sys;"337                  "cPickle.dump(glob.glob(sys.argv[1]), sys.stdout, 0)'")338        output = self.run(SCRIPT, args=(glob,), stdout_tee=None,339                          timeout=60).stdout340        return cPickle.loads(output)341    def symlink_closure(self, paths):342        """343        Given a sequence of path strings, return the set of all paths that344        can be reached from the initial set by following symlinks.345        @param paths: sequence of path strings.346        @return: a sequence of path strings that are all the unique paths that347                can be reached from the given ones after following symlinks.348        """349        SCRIPT = ("python -c 'import cPickle, os, sys\n"350                  "paths = cPickle.load(sys.stdin)\n"351                  "closure = {}\n"352                  "while paths:\n"353                  "    path = paths.keys()[0]\n"354                  "    del paths[path]\n"355                  "    if not os.path.exists(path):\n"356                  "        continue\n"357                  "    closure[path] = None\n"358                  "    if os.path.islink(path):\n"359                  "        link_to = os.path.join(os.path.dirname(path),\n"360                  "                               os.readlink(path))\n"361                  "        if link_to not in closure.keys():\n"362                  "            paths[link_to] = None\n"363                  "cPickle.dump(closure.keys(), sys.stdout, 0)'")364        input_data = cPickle.dumps(dict((path, None) for path in paths), 0)365        output = self.run(SCRIPT, stdout_tee=None, stdin=input_data,366                          timeout=60).stdout367        return cPickle.loads(output)368    def cleanup_kernels(self, boot_dir='/boot'):369        """370        Remove any kernel image and associated files (vmlinux, system.map,371        modules) for any image found in the boot directory that is not372        referenced by entries in the bootloader configuration.373        @param boot_dir: boot directory path string, default '/boot'374        """375        # find all the vmlinuz images referenced by the bootloader376        vmlinuz_prefix = os.path.join(boot_dir, 'vmlinuz-')377        boot_info = self.bootloader.get_entries()378        used_kernver = [boot['kernel'][len(vmlinuz_prefix):]379                        for boot in boot_info.itervalues()]380        # find all the unused vmlinuz images in /boot381        all_vmlinuz = self.list_files_glob(vmlinuz_prefix + '*')382        used_vmlinuz = self.symlink_closure(vmlinuz_prefix + kernver383                                            for kernver in used_kernver)384        unused_vmlinuz = set(all_vmlinuz) - set(used_vmlinuz)385        # find all the unused vmlinux images in /boot386        vmlinux_prefix = os.path.join(boot_dir, 'vmlinux-')387        all_vmlinux = self.list_files_glob(vmlinux_prefix + '*')388        used_vmlinux = self.symlink_closure(vmlinux_prefix + kernver389                                            for kernver in used_kernver)390        unused_vmlinux = set(all_vmlinux) - set(used_vmlinux)391        # find all the unused System.map files in /boot392        systemmap_prefix = os.path.join(boot_dir, 'System.map-')393        all_system_map = self.list_files_glob(systemmap_prefix + '*')394        used_system_map = self.symlink_closure(395            systemmap_prefix + kernver for kernver in used_kernver)396        unused_system_map = set(all_system_map) - set(used_system_map)397        # find all the module directories associated with unused kernels398        modules_prefix = '/lib/modules/'399        all_moddirs = [dir for dir in self.list_files_glob(modules_prefix + '*')400                       if re.match(modules_prefix + r'\d+\.\d+\.\d+.*', dir)]401        used_moddirs = self.symlink_closure(modules_prefix + kernver402                                            for kernver in used_kernver)403        unused_moddirs = set(all_moddirs) - set(used_moddirs)404        # remove all the vmlinuz files we don't use405        # TODO: if needed this should become package manager agnostic406        for vmlinuz in unused_vmlinuz:407            # try and get an rpm package name408            rpm = self.run('rpm -qf', args=(vmlinuz,),409                           ignore_status=True, timeout=120)410            if rpm.exit_status == 0:411                packages = set(line.strip() for line in412                               rpm.stdout.splitlines())413                # if we found some package names, try to remove them...parse-all-files.py
Source:parse-all-files.py  
...25    26    def list_files_folder(self, doesIncludeAbsPath=False):27        return [os.path.abspath(x) if doesIncludeAbsPath else x for x in os.listdir(self.pathName)]28    29    def list_files_glob(self):30        return glob.glob(self.pathName)31    32    def print_line_separator(self,symbol="*",max_length=100):33        print(symbol * max_length)34    35file_ins = ParseAllFiles(None)36# List the file names using the listdir37print(file_ins.list_only_files())38file_ins.print_line_separator()39# List the file names using the walk40print(file_ins.walthrough_all_filesfolders(".py"))41file_ins.print_line_separator()42# List the file names using the glob module43print(file_ins.list_files_glob())44file_ins.print_line_separator()45# List the files and Folder names in the current directory using os Module46print(file_ins.list_files_folder(True))...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!!
