Best Python code snippet using avocado_python
compress_bzip2.py
Source:compress_bzip2.py  
...323      else:324         if self._compressor is None:325            self._compressor = bz2.BZ2Compressor(self.client_max_compress_level)326   def compressMessageData(self, data):327      return self._compressor.compress(data)328   def endCompressMessage(self):329      data = self._compressor.flush()330      ## there seems to be no "flush without close stream", and after331      ## full flush, compressor must not be reused332      self._compressor = None333      return data334   def startDecompressMessage(self):335      if self._decompressor is None:336         self._decompressor = bz2.BZ2Decompressor()337   def decompressMessageData(self, data):338      return self._decompressor.decompress(data)339   def endDecompressMessage(self):...archive_util.py
Source:archive_util.py  
1"""distutils.archive_util2Utility functions for creating archive files (tarballs, zip files,3that sort of thing)."""4__revision__ = "$Id$"5import os6from warnings import warn7import sys8from distutils.errors import DistutilsExecError9from distutils.spawn import spawn10from distutils.dir_util import mkpath11from distutils import log12try:13    from pwd import getpwnam14except ImportError:15    getpwnam = None16try:17    from grp import getgrnam18except ImportError:19    getgrnam = None20def _get_gid(name):21    """Returns a gid, given a group name."""22    if getgrnam is None or name is None:23        return None24    try:25        result = getgrnam(name)26    except KeyError:27        result = None28    if result is not None:29        return result[2]30    return None31def _get_uid(name):32    """Returns an uid, given a user name."""33    if getpwnam is None or name is None:34        return None35    try:36        result = getpwnam(name)37    except KeyError:38        result = None39    if result is not None:40        return result[2]41    return None42def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,43                 owner=None, group=None):44    """Create a (possibly compressed) tar file from all the files under45    'base_dir'.46    'compress' must be "gzip" (the default), "compress", "bzip2", or None.47    (compress will be deprecated in Python 3.2)48    'owner' and 'group' can be used to define an owner and a group for the49    archive that is being built. If not provided, the current owner and group50    will be used.51    The output tar file will be named 'base_dir' +  ".tar", possibly plus52    the appropriate compression extension (".gz", ".bz2" or ".Z").53    Returns the output filename.54    """55    tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: '', 'compress': ''}56    compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'compress': '.Z'}57    # flags for compression program, each element of list will be an argument58    if compress is not None and compress not in compress_ext.keys():59        raise ValueError, \60              ("bad value for 'compress': must be None, 'gzip', 'bzip2' "61               "or 'compress'")62    archive_name = base_name + '.tar'63    if compress != 'compress':64        archive_name += compress_ext.get(compress, '')65    mkpath(os.path.dirname(archive_name), dry_run=dry_run)66    # creating the tarball67    import tarfile  # late import so Python build itself doesn't break68    log.info('Creating tar archive')69    uid = _get_uid(owner)70    gid = _get_gid(group)71    def _set_uid_gid(tarinfo):72        if gid is not None:73            tarinfo.gid = gid74            tarinfo.gname = group75        if uid is not None:76            tarinfo.uid = uid77            tarinfo.uname = owner78        return tarinfo79    if not dry_run:80        tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])81        try:82            tar.add(base_dir, filter=_set_uid_gid)83        finally:84            tar.close()85    # compression using `compress`86    if compress == 'compress':87        warn("'compress' will be deprecated.", PendingDeprecationWarning)88        # the option varies depending on the platform89        compressed_name = archive_name + compress_ext[compress]90        if sys.platform == 'win32':91            cmd = [compress, archive_name, compressed_name]92        else:93            cmd = [compress, '-f', archive_name]94        spawn(cmd, dry_run=dry_run)95        return compressed_name96    return archive_name97def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):98    """Create a zip file from all the files under 'base_dir'.99    The output zip file will be named 'base_name' + ".zip".  Uses either the100    "zipfile" Python module (if available) or the InfoZIP "zip" utility101    (if installed and found on the default search path).  If neither tool is102    available, raises DistutilsExecError.  Returns the name of the output zip103    file.104    """105    try:106        import zipfile107    except ImportError:108        zipfile = None109    zip_filename = base_name + ".zip"110    mkpath(os.path.dirname(zip_filename), dry_run=dry_run)111    # If zipfile module is not available, try spawning an external112    # 'zip' command.113    if zipfile is None:114        if verbose:115            zipoptions = "-r"116        else:117            zipoptions = "-rq"118        try:119            spawn(["zip", zipoptions, zip_filename, base_dir],120                  dry_run=dry_run)121        except DistutilsExecError:122            # XXX really should distinguish between "couldn't find123            # external 'zip' command" and "zip failed".124            raise DistutilsExecError, \125                  ("unable to create zip file '%s': "126                   "could neither import the 'zipfile' module nor "127                   "find a standalone zip utility") % zip_filename128    else:129        log.info("creating '%s' and adding '%s' to it",130                 zip_filename, base_dir)131        if not dry_run:132            zip = zipfile.ZipFile(zip_filename, "w",133                                  compression=zipfile.ZIP_DEFLATED)134            for dirpath, dirnames, filenames in os.walk(base_dir):135                for name in filenames:136                    path = os.path.normpath(os.path.join(dirpath, name))137                    if os.path.isfile(path):138                        zip.write(path, path)139                        log.info("adding '%s'" % path)140            zip.close()141    return zip_filename142ARCHIVE_FORMATS = {143    'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),144    'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),145    'ztar':  (make_tarball, [('compress', 'compress')], "compressed tar file"),146    'tar':   (make_tarball, [('compress', None)], "uncompressed tar file"),147    'zip':   (make_zipfile, [],"ZIP file")148    }149def check_archive_formats(formats):150    """Returns the first format from the 'format' list that is unknown.151    If all formats are known, returns None152    """153    for format in formats:154        if format not in ARCHIVE_FORMATS:155            return format156    return None157def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,158                 dry_run=0, owner=None, group=None):159    """Create an archive file (eg. zip or tar).160    'base_name' is the name of the file to create, minus any format-specific161    extension; 'format' is the archive format: one of "zip", "tar", "ztar",162    or "gztar".163    'root_dir' is a directory that will be the root directory of the164    archive; ie. we typically chdir into 'root_dir' before creating the165    archive.  'base_dir' is the directory where we start archiving from;166    ie. 'base_dir' will be the common prefix of all files and167    directories in the archive.  'root_dir' and 'base_dir' both default168    to the current directory.  Returns the name of the archive file.169    'owner' and 'group' are used when creating a tar archive. By default,170    uses the current owner and group.171    """172    save_cwd = os.getcwd()173    if root_dir is not None:174        log.debug("changing into '%s'", root_dir)175        base_name = os.path.abspath(base_name)176        if not dry_run:177            os.chdir(root_dir)178    if base_dir is None:179        base_dir = os.curdir180    kwargs = {'dry_run': dry_run}181    try:182        format_info = ARCHIVE_FORMATS[format]183    except KeyError:184        raise ValueError, "unknown archive format '%s'" % format185    func = format_info[0]186    for arg, val in format_info[1]:187        kwargs[arg] = val188    if format != 'zip':189        kwargs['owner'] = owner190        kwargs['group'] = group191    try:192        filename = func(base_name, base_dir, **kwargs)193    finally:194        if root_dir is not None:195            log.debug("changing back to '%s'", save_cwd)196            os.chdir(save_cwd)...setup_helper.py
Source:setup_helper.py  
1# Copyright (C) 2003-2007  Robey Pointer <robeypointer@gmail.com>2#3# This file is part of paramiko.4#5# Paramiko is free software; you can redistribute it and/or modify it under the6# terms of the GNU Lesser General Public License as published by the Free7# Software Foundation; either version 2.1 of the License, or (at your option)8# any later version.9#10# Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY11# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR12# A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more13# details.14#15# You should have received a copy of the GNU Lesser General Public License16# along with Paramiko; if not, write to the Free Software Foundation, Inc.,17# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.18# Note: Despite the copyright notice, this was submitted by John19# Arbash Meinel.  Thanks John!20"""A small set of helper functions for dealing with setup issues"""21import os22import tarfile23from distutils import log24import distutils.archive_util25from distutils.dir_util import mkpath26from distutils.spawn import spawn27def make_tarball(base_name, base_dir, compress='gzip',28                 verbose=False, dry_run=False):29    """Create a tar file from all the files under 'base_dir'.30    This file may be compressed.31    :param compress: Compression algorithms. Supported algorithms are:32        'gzip': (the default)33        'compress'34        'bzip2'35        None36    For 'gzip' and 'bzip2' the internal tarfile module will be used.37    For 'compress' the .tar will be created using tarfile, and then38    we will spawn 'compress' afterwards.39    The output tar file will be named 'base_name' + ".tar", 40    possibly plus the appropriate compression extension (".gz",41    ".bz2" or ".Z").  Return the output filename.42    """43    # XXX GNU tar 1.13 has a nifty option to add a prefix directory.44    # It's pretty new, though, so we certainly can't require it --45    # but it would be nice to take advantage of it to skip the46    # "create a tree of hardlinks" step!  (Would also be nice to47    # detect GNU tar to use its 'z' option and save a step.)48    compress_ext = { 'gzip': ".gz",49                     'bzip2': '.bz2',50                     'compress': ".Z" }51    # flags for compression program, each element of list will be an argument52    tarfile_compress_flag = {'gzip':'gz', 'bzip2':'bz2'}53    compress_flags = {'compress': ["-f"]}54    if compress is not None and compress not in compress_ext.keys():55        raise ValueError("bad value for 'compress': must be None, 'gzip',"56                         "'bzip2' or 'compress'")57    archive_name = base_name + ".tar"58    if compress and compress in tarfile_compress_flag:59        archive_name += compress_ext[compress]60    mode = 'w:' + tarfile_compress_flag.get(compress, '')61    mkpath(os.path.dirname(archive_name), dry_run=dry_run)62    log.info('Creating tar file %s with mode %s' % (archive_name, mode))63    if not dry_run:64        tar = tarfile.open(archive_name, mode=mode)65        # This recursively adds everything underneath base_dir66        tar.add(base_dir)67        tar.close()68    if compress and compress not in tarfile_compress_flag:69        spawn([compress] + compress_flags[compress] + [archive_name],70              dry_run=dry_run)71        return archive_name + compress_ext[compress]72    else:73        return archive_name74_custom_formats = {75    'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),76    'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),77    'ztar':  (make_tarball, [('compress', 'compress')], "compressed tar file"),78    'tar':   (make_tarball, [('compress', None)], "uncompressed tar file"),79}80# Hack in and insert ourselves into the distutils code base81def install_custom_make_tarball():...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!!
