Ejemplo n.º 1
0
    def extract(self, filename, destination_dir):
        """ Extract <filename> from the archive to <destination_dir>. """
        assert isinstance(filename, unicode) and \
                isinstance(destination_dir, unicode)

        if not self._get_executable():
            return

        if not self.filenames_initialized:
            self.list_contents()

        proc = process.Process(
            [self._get_executable()] + self._get_extract_arguments() +
            [self.archive, self._original_filename(filename)])
        fd = proc.spawn()

        if fd:
            # Create new file
            new = self._create_file(os.path.join(destination_dir, filename))
            stdout, stderr = proc.communicate()
            new.write(stdout)
            new.close()

            # Wait for process to finish
            fd.close()
            proc.wait()
Ejemplo n.º 2
0
    def extract(self, filename, destination_path):
        """ Extract <filename> from the archive to <destination_path>.
        This path should include the full filename. """
        assert isinstance(filename, unicode) and isinstance(destination_path, unicode)

        if not self._get_executable():
            return

        if not self.filenames_initialized:
            self.list_contents()

        # Create directory if it doesn't exist
        destination_dir = os.path.split(destination_path)[0]
        self._create_directory(destination_dir)
        proc = process.Process([self._get_executable()] +
            self._get_extract_arguments() +
            [self.archive, self._original_filename(filename)])
        fd = proc.spawn()

        if fd:
            # Create new file
            new = open(destination_path, 'wb')
            stdout, stderr = proc.communicate()
            new.write(stdout)
            new.close()

            # Wait for process to finish
            fd.close()
            proc.wait()
Ejemplo n.º 3
0
 def extract(self, filename, destination_dir):
     self._create_directory(destination_dir)
     destination_path = os.path.join(destination_dir, filename)
     page_num = int(filename[0:-4])
     # Try to find optimal DPI.
     proc = process.Process(['mudraw', '-x', '--', self.pdf, str(page_num)])
     fd = proc.spawn()
     max_size = 0
     max_dpi = PDF_RENDER_DPI_DEF
     if fd is not None:
         for line in fd:
             match = self._fill_image_regex.match(line)
             if not match:
                 continue
             matrix = [float(f) for f in match.group('matrix').split()]
             for size, coeff1, coeff2 in (
                 (int(match.group('width')), matrix[0], matrix[1]),
                 (int(match.group('height')), matrix[2], matrix[3]),
             ):
                 if size < max_size:
                     continue
                 render_size = math.sqrt(coeff1 * coeff1 + coeff2 * coeff2)
                 dpi = int(size * 72 / render_size)
                 if dpi > PDF_RENDER_DPI_MAX:
                     dpi = PDF_RENDER_DPI_MAX
                 max_size = size
                 max_dpi = dpi
         fd.close()
         proc.wait()
     # Render...
     cmd = [
         'mudraw', '-r',
         str(max_dpi), '-o', destination_path, '--', self.pdf,
         str(page_num)
     ]
     log.debug('rendering %s: %s' % (filename, ' '.join(cmd)))
     proc = process.Process(cmd)
     fd = proc.spawn()
     if fd is not None:
         fd.close()
         proc.wait()
Ejemplo n.º 4
0
 def iter_contents(self):
     proc = process.Process(['mutool', 'show', '--', self.pdf, 'pages'])
     fd = proc.spawn()
     if fd is None:
         return
     try:
         for line in fd:
             if line.startswith('page '):
                 yield line.split()[1] + '.png'
     finally:
         fd.close()
         proc.wait()
Ejemplo n.º 5
0
 def is_available():
     global _pdf_possible
     if _pdf_possible is None:
         proc = process.Process(['mudraw'])
         fd = proc.spawn()
         if fd is not None:
             fd.close()
             proc.wait()
             _pdf_possible = True
         else:
             log.info('MuPDF not available.')
             _pdf_possible = False
     return _pdf_possible
Ejemplo n.º 6
0
 def _find_7z_executable():
     """ Tries to start 7z, and returns either '7z' if
     it was started successfully or None otherwise. """
     global _7z_executable
     if _7z_executable != -1:
         return _7z_executable
     else:
         proc = process.Process([u'7z'])
         fd = proc.spawn()
         if fd is not None:
             fd.close()
             _7z_executable = u'7z'
             return u'7z'
         else:
             _7z_executable = None
             return None
Ejemplo n.º 7
0
    def _find_unzip_executable():
        """ Tries to run unzip, and returns 'unzip' on success.
        Returns None on failure. """
        global _zip_executable
        if _zip_executable != -1:
            return _zip_executable
        else:
            proc = process.Process([u'unzip'])
            fd = proc.spawn()
            if fd is not None:
                fd.close()
                _zip_executable = u'unzip'
                return u'unzip'

        _zip_executable = None
        return None
Ejemplo n.º 8
0
    def _find_unrar_executable():
        """ Tries to start rar/unrar, and returns either 'rar' or 'unrar' if
        one of them was started successfully.
        Returns None if neither could be started. """
        global _rar_executable
        if _rar_executable != -1:
            return _rar_executable
        else:
            for command in (u'unrar', u'rar'):
                proc = process.Process([command])
                fd = proc.spawn()
                if fd is not None:
                    fd.close()
                    _rar_executable = command
                    return command

            _rar_executable = None
            return None
Ejemplo n.º 9
0
    def iter_contents(self):
        if not self._get_executable():
            return

        proc = process.Process([self._get_executable()] +
                               self._get_list_arguments() + [self.archive])
        fd = proc.spawn()

        try:
            for line in fd.readlines():
                filename = self._parse_list_output_line(line.rstrip(
                    os.linesep))
                if filename is not None:
                    yield self._unicode_filename(filename)
        finally:
            fd.close()
            proc.wait()

        self.filenames_initialized = True
Ejemplo n.º 10
0
    def list_contents(self):
        if not self._get_executable():
            return []

        proc = process.Process([self._get_executable()] +
            self._get_list_arguments() +
            [self.archive])
        fd = proc.spawn()

        filenames = [ ]
        for line in fd.readlines():
            filename = self._parse_list_output_line(line.rstrip(os.linesep))
            if filename is not None:
                filenames.append(self._unicode_filename(filename))

        fd.close()
        proc.wait()

        self.filenames_initialized = True
        return filenames
Ejemplo n.º 11
0
    def extract(self, filename, destination_dir):
        """ Extract <filename> from the archive to <destination_dir>. """
        assert isinstance(filename, unicode) and \
                isinstance(destination_dir, unicode)

        if not self._get_executable():
            return

        if not self.filenames_initialized:
            self.list_contents()

        tmplistfile = tempfile.NamedTemporaryFile(prefix='mcomix.7z.',
                                                  delete=False)
        try:
            desired_filename = self._original_filename(filename)
            if isinstance(desired_filename, unicode):
                desired_filename = desired_filename.encode('utf-8')

            tmplistfile.write(desired_filename + os.linesep)
            tmplistfile.close()
            proc = process.Process([
                self._get_executable(), u'x', u'-so', u'-p',
                u'-i@' + tmplistfile.name, u'--', self.archive
            ])
            fd = proc.spawn()

            if fd:
                # Create new file
                new = self._create_file(os.path.join(destination_dir,
                                                     filename))
                stdout, stderr = proc.communicate()
                new.write(stdout)
                new.close()

                # Wait for process to finish
                fd.close()
                proc.wait()
        finally:
            os.unlink(tmplistfile.name)
Ejemplo n.º 12
0
    def iter_extract(self, entries, destination_dir):

        if not self._get_executable():
            return

        if not self.filenames_initialized:
            self.list_contents()

        proc = process.Process(
            [self._get_executable(), u'x', u'-so', u'-p', u'--', self.archive])
        fd = proc.spawn(stdin=process.NULL)
        if not fd:
            return

        try:
            wanted = dict([(self._original_filename(unicode_name),
                            unicode_name) for unicode_name in entries])

            for filename, filesize in self._contents:
                data = fd.read(filesize)
                if filename not in wanted:
                    continue
                unicode_name = wanted.get(filename, None)
                if unicode_name is None:
                    continue
                new = self._create_file(
                    os.path.join(destination_dir, unicode_name))
                new.write(data)
                new.close()
                yield unicode_name
                del wanted[filename]
                if 0 == len(wanted):
                    break

        finally:
            # Wait for process to finish
            fd.close()
            proc.wait()