How to use is_lzma_file method in avocado

Best Python code snippet using avocado_python

archive.py

Source:archive.py Github

copy

Full Screen

...47 if not chunk:48 break49 output_file.write(chunk)50 return output_path51def is_lzma_file(path):52 """53 Checks if file given by path has contents that suggests lzma file54 """55 with lzma.LZMAFile(path, 'rb') as lzma_file:56 try:57 _ = lzma_file.read(1)58 except lzma.LZMAError:59 return False60 except EOFError:61 return False62 return True63def lzma_uncompress(path, output_path=None, force=False):64 """65 Extracts a XZ compressed file to the same directory.66 """67 if output_path is None:68 output_path = os.path.splitext(path)[0]69 elif os.path.isdir(output_path):70 basename = os.path.basename(path)71 if basename.endswith('.xz'):72 basename = os.path.splitext(basename)[0]73 output_path = os.path.join(output_path, basename)74 if not force and os.path.exists(output_path):75 return output_path76 with lzma.open(path, 'rb') as file_obj:77 with open(output_path, 'wb') as newfile_obj:78 newfile_obj.write(file_obj.read())79 return output_path80class ArchiveException(Exception):81 """82 Base exception for all archive errors.83 """84class ArchiveFile:85 """86 Class that represents an Archive file.87 Archives are ZIP files or Tarballs.88 """89 # extension info: is_zip, is_tar, zipfile|tarfile, +mode90 _extension_table = {91 '.zip': (True, False, zipfile.ZipFile, ''),92 '.tar': (False, True, tarfile.open, ''),93 '.tar.gz': (False, True, tarfile.open, ':gz'),94 '.tgz': (False, True, tarfile.open, ':gz'),95 '.tar.bz2': (False, True, tarfile.open, ':bz2'),96 '.tbz2': (False, True, tarfile.open, ':bz2'),97 '.xz': (False, True, tarfile.open, ':xz')}98 def __init__(self, filename, mode='r'):99 """100 Creates an instance of :class:`ArchiveFile`.101 :param filename: the archive file name.102 :param mode: file mode, `r` read, `w` write.103 """104 self.filename = filename105 self.mode = mode106 engine = None107 for ext, value in ArchiveFile._extension_table.items():108 if filename.endswith(ext):109 (self.is_zip,110 self.is_tar,111 engine,112 extra_mode) = value113 if engine is not None:114 self.mode += extra_mode115 self._engine = engine(self.filename, self.mode)116 else:117 raise ArchiveException('file is not an archive')118 def __repr__(self):119 return "ArchiveFile('%s', '%s')" % (self.filename, self.mode)120 def __enter__(self):121 return self122 def __exit__(self, exc_type, exc_value, exc_traceback):123 if self._engine is not None:124 self.close()125 @classmethod126 def open(cls, filename, mode='r'):127 """128 Creates an instance of :class:`ArchiveFile`.129 :param filename: the archive file name.130 :param mode: file mode, `r` read, `w` write.131 """132 return cls(filename, mode)133 def add(self, filename, arcname=None):134 """135 Add file to the archive.136 :param filename: file to archive.137 :param arcname: alternative name for the file in the archive.138 """139 if self.is_zip:140 self._engine.write(filename, arcname, zipfile.ZIP_DEFLATED)141 else:142 self._engine.add(filename, arcname)143 def list(self):144 """145 List files to the standard output.146 """147 if self.is_zip:148 self._engine.printdir()149 else:150 self._engine.list()151 def extract(self, path='.'):152 """153 Extract all files from the archive.154 :param path: destination path.155 :return: the first member of the archive, a file or directory or None156 if the archive is empty157 """158 self._engine.extractall(path)159 if self.is_zip:160 self._update_zip_extra_attrs(path)161 files = self._engine.namelist()162 if files:163 return files[0].strip(os.sep)164 else:165 return None166 files = self._engine.getnames()167 if files:168 return files[0]169 return None170 def _update_zip_extra_attrs(self, dst_dir):171 if platform.system() != "Linux":172 LOG.warning("Attr handling in zip files only supported on Linux.")173 return174 # Walk all files and re-create files as symlinks175 for path, info in self._engine.NameToInfo.items():176 dst = os.path.join(dst_dir, path)177 if not os.path.exists(dst):178 LOG.warning("One or more files in the ZIP archive '%s' could "179 "not be found after extraction. Their paths are "180 "probably stored in unsupported format and their "181 "attributes are not going to be updated",182 self.filename)183 return184 attr = info.external_attr >> 16185 if attr & stat.S_IFLNK == stat.S_IFLNK:186 dst = os.path.join(dst_dir, path)187 if not os.path.islink(dst):188 # Link created as an ordinary file containing the dst path189 with open(dst, 'r') as dst_path: # pylint: disable=W1514190 src = dst_path.read()191 else:192 # Link is already there and could be outdated. Let's read193 # the original destination from the zip file.194 src = self._engine.read(path)195 os.remove(dst)196 os.symlink(src, dst)197 continue # Don't override any other attributes on links198 mode = attr & 511 # Mask only permissions199 if mode and mode != 436: # If mode is stored and is not default200 os.chmod(dst, mode)201 def close(self):202 """203 Close archive.204 """205 self._engine.close()206def is_archive(filename):207 """208 Test if a given file is an archive.209 :param filename: file to test.210 :return: `True` if it is an archive.211 """212 return (zipfile.is_zipfile(filename) or tarfile.is_tarfile(filename) or213 is_gzip_file(filename) or is_lzma_file(filename))214def compress(filename, path):215 """216 Compress files in an archive.217 :param filename: archive file name.218 :param path: origin directory path to files to compress. No219 individual files allowed.220 """221 with ArchiveFile.open(filename, 'w') as x:222 if os.path.isdir(path):223 for root, _, files in os.walk(path):224 for name in files:225 newroot = root.replace(path, '')226 x.add(os.path.join(root, name),227 os.path.join(newroot, name))228 elif os.path.isfile(path):229 x.add(path, os.path.basename(path))230def uncompress(filename, path):231 """232 Extract files from an archive.233 :param filename: archive file name.234 :param path: destination path to extract to.235 """236 is_tar = tarfile.is_tarfile(filename)237 if is_gzip_file(filename) and not is_tar:238 return gzip_uncompress(filename, path)239 elif is_lzma_file(filename) and not is_tar:240 return lzma_uncompress(filename, path)241 else:242 with ArchiveFile.open(filename) as x:243 return x.extract(path)244# Some aliases245create = compress...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful