File: web2png

package info (click to toggle)
gif2png 2.5.8-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 880 kB
  • ctags: 428
  • sloc: ansic: 2,958; sh: 1,929; xml: 836; python: 248; makefile: 85
file content (334 lines) | stat: -rwxr-xr-x 11,228 bytes parent folder | download
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python
"""web2png -- convert a web hierarchy from using GIFs to using PNGs.

This script is a front end for gif2png that assists you in converting an
entire website.  Requires gif2png 1.0.5 or later.  Requires python 1.5.2
or later. Requires Unix.

by Eric S. Raymond <esr@thyrsus.com>

Slightly modified by Aaron Isotton <aaron@isotton.com> for Debian.
"""

# Future library code -- I'm going to submit this for Python 1.6
import sys, os
from stat import *

if sys.version[:5] < "1.5.2":
    print "This program requires Python 1.5.2 or later."
    sys.exit(1)

FTW_FILE    = 0	# Item is a normal file
FTW_DIR     = 1	# Item is a directory
FTW_CHRDEV  = 2	# Item is a character special device file
FTW_BLKDEV  = 3	# Item is a block special device file
FTW_FIFO    = 4	# Item is a FIFO
FTW_SYMLINK = 5	# Item is a symbolic link
FTW_SOCKET  = 6	# Item is a socket
FTW_NOSTAT = -1	# stat failed on item
FTW_DNR	   = -2	# item was an unreadable directory

class FTWException(Exception):
    pass

def ftw_inner(func, path=".", extra=None):
    if extra is None:
        extra = []
    try:
        st = os.stat(path)
    except OSError:
        return FTW_NOSTAT
    mode = st[ST_MODE]

    if S_ISDIR(mode):
        try:
            subdirs = os.listdir(path)
        except OSError:
            return FTW_DNR

    if S_ISREG(mode):  ftype = FTW_FILE
    if S_ISDIR(mode):  ftype = FTW_DIR
    if S_ISBLK(mode):  ftype = FTW_BLKDEV
    if S_ISCHR(mode):  ftype = FTW_CHRDEV
    if S_ISFIFO(mode): ftype = FTW_FIFO
    if S_ISLNK(mode):  ftype = FTW_SYMLINK
    if S_ISSOCK(mode): ftype = FTW_SOCKET

    terminate = apply(func, tuple([path, st, ftype] + list(extra)))
    if terminate < 0:
        raise FTWException, terminate

    if S_ISDIR(mode) and not terminate:
        for f in subdirs:
            ftw_inner(func, os.path.join(path, f), extra)

def ftw(func, path=".", *extra):
    """Python tree-walker -- like ftw(3), but more capable."""
    try:
        ftw_inner(func, path, extra)
    except FTWException, exval:
        return exval
    return 0

def find(fdir=".", regexp=None):
    """Return a tuple list of entries beneath dir matching a given pattern."""
    def findhook(path, _, ftype, r, fl):
        if not r or r.search(path):
            fl.append((path, ftype))
            return 0
    flist = []
    ftw(findhook, fdir, regexp, flist)
    return flist

def findfiles(fdir=".", regexp=None):
    """Return a list of files beneath dir matching a given pattern."""
    return map(lambda x: os.path.normpath(x[0]),
               filter(lambda t: t[1]==FTW_FILE, find(fdir, regexp)))

def datacmp(f1, f2, blocksize=1):
    # print "Comparing %s with %s" % (f1, f2)
    # st1 = os.stat(f1); print st1
    # st2 = os.stat(f2); print st2
    f1 = open(f1, "r")
    f2 = open(f2, "r")
    blocknum = 0
    try:
        while 1:
            blocknum = blocknum + 1
            s1 = f1.read(blocksize)
            s2 = f2.read(blocksize)
            if not s1 and not s2:
                break
            elif s1 != s2:
                return blocknum
    finally:
        f1.close()
        f2.close()
    return None

# End future library code

import re, getopt, commands, string, urllib

nochange = allopt = None
gifre = re.compile(r"\.gif$", re.IGNORECASE)
pngre = re.compile(r"\.png$", re.IGNORECASE)
pagere = re.compile(r"\.s?html$|\.s?htm$|\.php$|\.inc$|\.css$|\.js$", re.IGNORECASE)
bakre = re.compile(r"\.bak$", re.IGNORECASE)

# Possible left delimiters of these matches are =, ", (, \s
# Possible right delimiters of these matches are ", >, ), \s
# .?"? parts deal with the possibility that the optional " might be backslashed
delim = r'(?:\\?")?'	# Optional double quote, possibly preceded by backslash
gifre = delim + '([^">]*\.gif)' + delim
imgre = re.compile(r'SRC=' + gifre, re.IGNORECASE)
hrefre = re.compile(r'<A HREF=' + gifre + '>', re.IGNORECASE)
backre = re.compile(r'BACKGROUND=' + gifre, re.IGNORECASE)
basere = re.compile(r'<BASE HREF=' + gifre + '>', re.IGNORECASE)
cssurlre = re.compile(r'url\(' + gifre + '\)', re.IGNORECASE)

def version_controlled(page):
    # Is given page under version control?
    return os.path.exists(page + ",v") \
           or os.path.exists(os.path.join("RCS", page) + ",v")

def web2png(directory):
    # Convert a web hierarchy rooted on the given directory
    gifs = findfiles(directory, re.compile(gifre))
    htmls = findfiles(directory, pagere)

    # There's a standard max on the number of arguments we can feed gif2png.
    # if we see more than these, 
    if len(gifs) > 5120:
        print "web2png: Too many GIFs.  Try converting some subtrees first."
        sys.exit(1)

    print "This web subtree has", len(gifs), "GIFs and", len(htmls), "pages."

    # Display information on files we won't convert
    rejects = \
	commands.getoutput("gif2png -w "+string.join(gifs," ")+" >/dev/null")
    rejects = re.sub("gif2png: ", "	", rejects)
    if rejects:
        if allopt:
            print "You are forcing conversion of these GIFs:"
        else:
            print "The following GIFs will not be converted:"
        print rejects

    # Figure out which files are eligible for conversion
    if allopt:
        giflist = gifs
    else:
        giflist = \
                string.split(commands.getoutput("gif2png -w "+string.join(gifs," ")+" 2>/dev/null"))

    if verbose: print "Giflist: " + repr(giflist)

    convert_gifs = []
    for gif in giflist:
        png = re.sub(r"\.gif$", r".png", gif)
        if os.path.exists(png):
            print "\t%s already has a PNG equivalent" % (gif,)
        else:
            convert_gifs.append(gif)

    # Display information on files we will convert
    if convert_gifs:
        print "The following GIFs will be converted:\n\t" + \
		string.join(convert_gifs, "\n\t")
    if not convert_gifs:
        print "All eligible GIFs seem to have been converted already."

    # Create a dictionary mapping pages to sets of references to be mapped
    print "Checking for HTML, PHP JavaScript and include pages that need conversion..."
    pagecount = 0
    page_conversions = {}
    for ifile in htmls:
        fp = open(ifile, "r")
        contents = fp.read()
        fp.close()

        basedir = basere.search(contents)
        if basedir:
            basedir = basedir.group(1)
        else:
            basedir = os.path.dirname(ifile)

        matches = imgre.findall(contents) + hrefre.findall(contents) + backre.findall(contents) + cssurlre.findall(contents)
        if verbose:
            print "Found %d .gif image(s) references in %s." \
                  % (len(matches), file)
        convert_refs = []
        gif_basenames = map(os.path.basename, giflist)
        for ref in matches:
            withbase = os.path.join(basedir, ref)
            # Simple case -- it's a filename, we presume it's local
            if withbase[:5] != "http:" and withbase[1:] != os.sep:
		
                target = os.path.normpath(withbase)
                if not target in giflist:
                    if verbose: print "Won't replace %s. Not on my list." \
                       % target
                    continue
            # Tricky case -- compare by basename and content
            else:
                basename = os.path.basename(withbase[7:])
                if not basename in gif_basenames:
                    print "No local match by basename for %s" % (ref,)
                    continue
                target = giflist[gif_basenames.index(basename)]
                (data, _) = urllib.urlretrieve(withbase)
                not_equal = datacmp(data, target)
                os.remove(data)
                if not_equal:
                    print "Data of %s and %s don't match at offset %d" % (ref, target, not_equal)
                    continue
            ref = ref[:-4]
            convert_refs.append((ref, target))
        if convert_refs:
            print "\tIn %s, I see: %s" % (file, string.join(map(lambda x: x[0]+".gif", convert_refs)," "))
            page_conversions[file] = convert_refs
            pagecount = pagecount + 1
    print "%d HTML or PHP page(s) need conversion." % (pagecount,)

    # Unless user is willing to make changes, we're done now
    if nochange:
        return

    # Convert gifs verbosely
    if convert_gifs:
        print "GIF conversions begin:"
        os.system("gif2png -v -O " + string.join(convert_gifs, " "))
        # print "GIF conversions complete"

    # Now check to see which conversions did not take
    failures = []
    for gif in convert_gifs:
        png = re.sub(r"\.gif$", r".png", gif)
        if not os.path.exists(png):
            failures.append(gif)
    if failures:
        print "Some conversions failed:", string.join(failures)

    # Now hack the references in the web pages
    for page in page_conversions.keys():
        print "Converting %s..." % (page,)
        if version_controlled(page):
            os.system("co -l " + page)
        else:
            os.system("cp -pf " + page + " " + page + ".bak")
        fp = open(page, "r")
        contents = fp.read()
        fp.close()
        basedir = basere.search(contents)
        if basedir:
            basedir = basedir.group(1)
        else:
            basedir = ""
        for (ref, target) in page_conversions[page]:
            if target in giflist and not target in failures:
                source = '(?P<g1>[="(\s])' + ref + r'\.gif(?P<g2>[\s"\\>)])'
                target = r"\g<g1>" + ref + r".png\g<g2>"
                print "Source: %s, target %s" % (source, target)
                contents = re.sub(source, target, contents)
        fp = open(page, "w")
        fp.write(contents)
        fp.close()
    print "Page conversions complete."

def cleanup(directory):
    # Clean up superfluous .gif and .bak files left over after a conversion
    map(os.unlink, findfiles(directory, bakre))
    pnglist = findfiles(directory, pngre)
    for png in pnglist:
        gif = png[:-4] + ".gif"
        if os.path.exists(gif):
            os.remove(gif)

def unconvert(directory):
    # Reverse a conversion    
    pnglist = findfiles(directory, pngre)
    for png in pnglist:
        gif = png[:-4] + ".gif"
        if os.path.exists(gif):
            os.unlink(png)
    htmls = findfiles(directory, pagere)
    for page in htmls:
        if os.path.exists(page + ".bak"):
            os.rename(page + ".bak", page)
        elif version_controlled(page):
            os.system("rcs -u " + page)

if __name__ == '__main__': 

    delete = nochange = reverse = verbose = 0

    (options, arguments) = getopt.getopt(sys.argv[1:], "adnrv")

    if not arguments:
        arguments = ['.']

    for (switch, val) in options:
        if switch == '-a':
            allopt = 1
        elif switch == '-d':
            delete = 1
        elif switch == '-n':
            nochange = 1
        elif switch == '-r':
            reverse = 1
        elif switch == '-v':
            verbose = 1

    if delete:
        map(cleanup, arguments)
    elif reverse:
        map(unconvert, arguments)
    else:
        map(web2png, arguments)

# The following sets edit modes for GNU EMACS
# Local Variables:
# mode:python
# End: