/usr/bin/comicthumb is in comix 4.0.4-3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | #!/usr/bin/env python
"""comicthumb - Thumbnailer for comic book archives, bundled with Comix.
comicthumb is dependent on the Python Imaging Library (PIL).
comicthumb was originally written by Christoph Wolk, this version was
re-written from scratch for Comix 4 by Pontus Ekberg.
Supported formats: ZIP, RAR and tar (.cbz, .cbr, .cbt)
Usage: comicthumb INFILE OUTFILE [SIZE]
"""
import os
import sys
import gc
import re
import cStringIO
import zipfile
import tarfile
import subprocess
try:
import Image
except ImportError:
print '! Could not import the Image module (PIL).'
print __doc__
sys.exit(1)
ZIP, RAR, TAR, GZIP, BZIP2 = range(5)
class Process:
"""The subprocess and popen2 modules in Python are broken (see issue
#1336). The problem (i.e. complete crash) they can cause happen fairly
often (once is too often) in Comix when calling "rar" or "unrar" to
extract specific files from archives. We roll our own very simple
process spawning module here instead.
"""
# TODO: I can no longer reproduce the issue. Check if this version of
# process.py still solves it.
def __init__(self, args):
"""Setup a Process where <args> is a sequence of arguments that defines
the process, e.g. ['ls', '-a'].
"""
self._args = args
self._proc = None
def _exec(self):
"""Spawns the process, and returns its stdout.
(NOTE: separate function to make python2.4 exception syntax happy)
"""
try:
self._proc = subprocess.Popen(self._args, stdout=subprocess.PIPE)
return self._proc.stdout
except Exception:
return None
def spawn(self):
"""Spawn the process defined by the args in __init__(). Return a
file-like object linked to the spawned process' stdout.
"""
try:
gc.disable() # Avoid Python issue #1336!
return self._exec()
finally:
gc.enable()
def wait(self):
"""Wait for the process to terminate."""
if self._proc is None:
raise Exception('Process not spawned.')
return self._proc.wait()
class Extractor:
"""Extractor is a class for extracting different archive formats.
This is a much simplified version of the Extractor class from Comix.
"""
def __init__(self, src):
"""Setup the extractor with archive <src>."""
self._src = src
self._type = archive_mime_type(src)
self._files = []
if self._type == ZIP:
self._zfile = zipfile.ZipFile(src, 'r')
self._files = self._zfile.namelist()
elif self._type in [TAR, GZIP, BZIP2]:
self._tfile = tarfile.open(src, 'r')
self._files = self._tfile.getnames()
elif self._type == RAR:
self._rar = None
for command in ('unrar', 'rar'):
if Process([command]).spawn() is not None:
self._rar = command
if self._rar == None:
print '! Could not find the "rar" or "unrar" executable.'
sys.exit(1)
proc = Process([self._rar, 'vb', src])
fobj = proc.spawn()
self._files = fobj.readlines()
proc.wait()
self._files = [name.rstrip('\n') for name in self._files]
def get_files(self):
"""Return a list of the files in the archive."""
return self._files
def extract(self, chosen):
"""Extract the file <chosen> and return it as a cStringIO.StringIO
object. The <chosen> file must be one of the files in the list
returned by the get_files() method.
"""
if self._type == ZIP:
return cStringIO.StringIO(self._zfile.read(chosen))
elif self._type in [TAR, GZIP, BZIP2]:
return cStringIO.StringIO(self._tfile.extractfile(chosen).read())
elif self._type == RAR:
proc = Process([self._rar, 'p', '-inul', '-p-', '--',
self._src, chosen])
fobj = proc.spawn()
return cStringIO.StringIO(fobj.read())
def archive_mime_type(path):
"""Return the archive type of <path> or None for non-archives."""
try:
if os.path.isfile(path):
if not os.access(path, os.R_OK):
return None
if zipfile.is_zipfile(path):
return ZIP
fd = open(path, 'rb')
magic = fd.read(4)
fd.close()
if tarfile.is_tarfile(path) and os.path.getsize(path) > 0:
if magic.startswith('BZh'):
return BZIP2
if magic.startswith('\037\213'):
return GZIP
return TAR
if magic == 'Rar!':
return RAR
except Exception:
print '! Error while reading', path
return None
def guess_cover(files):
"""Return the filename within <files> that is the most likely to be
the cover of an archive.
"""
alphanumeric_sort(files)
ext_re = re.compile(r'\.(jpg|jpeg|png|gif|tif|tiff)\s*$', re.I)
front_re = re.compile('(cover|front)', re.I)
images = filter(ext_re.search, files)
candidates = filter(front_re.search, images)
candidates = [c for c in candidates if not 'back' in c.lower()]
if candidates:
return candidates[0]
if images:
return images[0]
return None
def alphanumeric_sort(filenames):
"""Do an in-place alphanumeric sort of the strings in <filenames>,
such that for an example "1.jpg", "2.jpg", "10.jpg" is a sorted
ordering.
"""
def _format_substring(s):
if s.isdigit():
return int(s)
return s.lower()
rec = re.compile("\d+|\D+")
filenames.sort(key=lambda s: map(_format_substring, rec.findall(s)))
if __name__ == '__main__':
try:
in_path = sys.argv[1]
out_path = sys.argv[2]
if len(sys.argv) == 4:
size = int(sys.argv[3])
else:
size = 128
except:
print __doc__
sys.exit(1)
extractor = Extractor(in_path)
files = extractor.get_files()
chosen = guess_cover(files)
fd = extractor.extract(chosen)
im = Image.open(fd)
if im.size[0] > im.size[1]:
x = size
y = size * im.size[1] / im.size[0]
else:
x = size * im.size[0] / im.size[1]
y = size
x = max(1, x)
y = max(1, y)
im.thumbnail((x, y), Image.ANTIALIAS)
im = im.convert('RGB')
im.save(out_path, 'PNG')
sys.exit(0)
|