Example #1
0
    def create(self, fileList, baseDir='.', dereference=False):
        try:
            fileList = forceUnicodeList(fileList)
            baseDir = os.path.abspath(forceFilename(baseDir))
            dereference = forceBool(dereference)

            if not os.path.isdir(baseDir):
                raise IOError(u"Base dir '%s' not found" % baseDir)

            command = u'%s --create --quiet --verbose --format crc' % System.which(
                'cpio')
            if dereference:
                command += ' --dereference'
            if self._compression == 'gzip':
                if self.pigz_detected:
                    command += ' | %s --rsyncable' % System.which('pigz')
                else:
                    command += ' | %s --rsyncable' % System.which('gzip')
            elif self._compression == 'bzip2':
                command += ' | %s' % System.which('bzip2')
            command += ' > "%s"' % self._filename

            self._create(fileList, baseDir, command)
        except Exception as e:
            raise RuntimeError(u"Failed to create archive '%s': %s" %
                               (self._filename, e))
Example #2
0
    def content(self):
        try:
            if not os.path.exists(self._filename):
                raise IOError(u"Archive file not found: '%s'" % self._filename)

            cat = System.which('cat')
            if self._compression == 'gzip':
                if self.pigz_detected:
                    cat = u'{pigz} --stdout --decompress'.format(
                        pigz=System.which('pigz'))
                else:
                    cat = System.which('zcat')
            elif self._compression == 'bzip2':
                cat = System.which('bzcat')

            return [
                unicode(line) for line in System.execute(
                    u'{cat} "{filename}" | {cpio} --quiet --extract --list'.
                    format(cat=cat,
                           filename=self._filename,
                           cpio=System.which('cpio'))) if line
            ]
        except Exception as e:
            raise RuntimeError(u"Failed to get archive content '%s': %s" %
                               (self._filename, e))
Example #3
0
    def extract(self, targetPath='.', patterns=[]):
        try:
            targetPath = os.path.abspath(forceFilename(targetPath))
            patterns = forceUnicodeList(patterns)
            if not os.path.isdir(targetPath):
                try:
                    os.mkdir(targetPath)
                except Exception as e:
                    raise IOError(u"Failed to create target dir '%s': %s" %
                                  (targetPath, e))

            cat = System.which('cat')
            if self._compression == 'gzip':
                if self.pigz_detected:
                    cat = u'%s --stdout --decompress' % (
                        System.which('pigz'), )
                else:
                    cat = System.which('zcat')
            elif self._compression == 'bzip2':
                cat = System.which('bzcat')

            fileCount = 0
            for filename in self.content():
                match = False
                if not patterns:
                    match = True
                else:
                    for pattern in patterns:
                        try:
                            pattern = pattern.replace('*', '.*')
                            if re.search(pattern, filename):
                                match = True
                                break
                            fileCount += 1
                        except Exception as e:
                            raise ValueError(u"Bad pattern '%s': %s" %
                                             (pattern, e))
                if match:
                    fileCount += 1

            include = ' '.join('"%s"' % pattern for pattern in patterns)

            curDir = os.path.abspath(os.getcwd())
            os.chdir(targetPath)
            try:
                command = u'%s "%s" | %s --quiet --extract --make-directories --unconditional --preserve-modification-time --verbose --no-preserve-owner %s' % (
                    cat, self._filename, System.which('cpio'), include)
                self._extract(command, fileCount)
            finally:
                os.chdir(curDir)
        except Exception as e:
            raise RuntimeError(u"Failed to extract archive '%s': %s" %
                               (self._filename, e))
Example #4
0
    def is_pigz_available():
        def is_correct_pigz_version():
            ver = System.execute('pigz --version')[0][5:]

            logger.debug('Detected pigz version: %s' % (ver, ))
            versionMatches = compareVersions(ver, '>=', '2.2.3')
            logger.debug('pigz version is compatible? %s' % (versionMatches))
            return versionMatches

        if not PIGZ_ENABLED:
            return False

        try:
            System.which('pigz')
            logger.debug(u'Detected "pigz".')

            return is_correct_pigz_version()
        except Exception:
            logger.debug(u'Did not detect "pigz".')
            return False
Example #5
0
    def extract(self, targetPath='.', patterns=[]):
        try:
            targetPath = os.path.abspath(forceFilename(targetPath))
            patterns = forceUnicodeList(patterns)
            if not os.path.isdir(targetPath):
                try:
                    os.mkdir(targetPath)
                except Exception as e:
                    raise IOError(u"Failed to create target dir '%s': %s" %
                                  (targetPath, e))

            options = u''
            if self._compression == 'gzip':
                if self.pigz_detected:
                    options += u'--use-compress-program=pigz'
                else:
                    options += u'--gunzip'
            elif self._compression == 'bzip2':
                options += u'--bzip2'

            fileCount = 0
            for filename in self.content():
                match = False
                if not patterns:
                    match = True
                else:
                    for pattern in patterns:
                        try:
                            pattern = pattern.replace('*', '.*')
                            if re.search(pattern, filename):
                                match = True
                                break
                            fileCount += 1
                        except Exception as e:
                            raise ValueError(u"Bad pattern '%s': %s" %
                                             (pattern, e))

                if match:
                    fileCount += 1
                else:
                    options += u' --exclude="%s"' % filename

            command = u'%s %s --directory "%s" --extract --verbose --file "%s"' % (
                System.which('tar'), options, targetPath, self._filename)
            self._extract(command, fileCount)
        except Exception as e:
            raise RuntimeError(u"Failed to extract archive '%s': %s" %
                               (self._filename, e))
Example #6
0
    def content(self):
        try:
            if not os.path.exists(self._filename):
                raise IOError(u"Archive file not found: '%s'" % self._filename)
            names = []
            options = u''
            if self._compression == 'gzip':
                if self.pigz_detected:
                    options += u'--use-compress-program=pigz'
                else:
                    options += u'--gunzip'
            elif self._compression == 'bzip2':
                options += u'--bzip2'

            for line in System.execute(
                    u'%s %s --list --file "%s"' %
                (System.which('tar'), options, self._filename)):
                if line:
                    names.append(unicode(line))

            return names
        except Exception as e:
            raise RuntimeError(u"Failed to get archive content '%s': %s" %
                               (self._filename, e))